X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdlib%2Fbsearch.c;h=ab93b94782284e7a1414aa9b5176a50203965a3c;hp=e8e4f3125f53ef61677defaf38787103269c50b4;hb=393020b6e48719d27699dea6b29e53025bbd5123;hpb=f408c1fd633015089d2a0fc6bc31c9f61eeae0a9 diff --git a/functions/stdlib/bsearch.c b/functions/stdlib/bsearch.c index e8e4f31..ab93b94 100644 --- a/functions/stdlib/bsearch.c +++ b/functions/stdlib/bsearch.c @@ -14,18 +14,19 @@ void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, { const void * pivot; int rc; - int corr; + size_t corr; while ( nmemb ) { /* algorithm needs -1 correction if remaining elements are an even number. */ - corr = ( nmemb % 2 ) - 1; + corr = nmemb % 2; nmemb /= 2; pivot = (const char *)base + (nmemb * size); rc = compar( key, pivot ); if ( rc > 0 ) { base = (const char *)pivot + size; - nmemb += corr; + /* applying correction */ + nmemb -= ( 1 - corr ); } else if ( rc == 0 ) {