From: cheloha Date: Thu, 2 Dec 2021 20:58:01 +0000 (+0000) Subject: bsearch(3): support arrays with more than INT_MAX elements X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=d10db4f7696863b7a3e27722ecb8083ffe76d3c4;p=openbsd bsearch(3): support arrays with more than INT_MAX elements The "lim" variable needs to be a size_t to match nmemb, otherwise we get undefined behavior when nmemb exceeds INT_MAX. Prompted by a blog post by Joshua Bloch: https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html Fixed by Chris Torek a long time ago: https://svnweb.freebsd.org/csrg/lib/libc/stdlib/bsearch.c?revision=51742&view=markup ok millert@ --- diff --git a/lib/libc/stdlib/bsearch.c b/lib/libc/stdlib/bsearch.c index 59d478e7ebc..6df44d6b4fd 100644 --- a/lib/libc/stdlib/bsearch.c +++ b/lib/libc/stdlib/bsearch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bsearch.c,v 1.8 2016/10/22 19:19:34 tb Exp $ */ +/* $OpenBSD: bsearch.c,v 1.9 2021/12/02 20:58:01 cheloha Exp $ */ /* * Copyright (c) 1990 Regents of the University of California. * All rights reserved. @@ -51,8 +51,9 @@ bsearch(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { const char *base = base0; - int lim, cmp; const void *p; + size_t lim; + int cmp; for (lim = nmemb; lim != 0; lim >>= 1) { p = base + (lim >> 1) * size;