From: schwarze Date: Thu, 13 Jan 2022 04:06:10 +0000 (+0000) Subject: Only sort the result array if it contains more than one element, X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=14baa4a2caafd4727e8809e25e9f5614039eafa9;p=openbsd Only sort the result array if it contains more than one element, making the mansearch() function easier to read for human auditors. No functional change on OpenBSD. As observed by Mark Millard , neither the latest version of POSIX 2008 nor C11 defines what qsort(3) should do for base == NULL && nmemb == 0. My impression is it is indeed undefined behaviour because the standards say that base shall point to an array, NULL does not point to an array, and while there is special wording saying that compar() shall not be called if nmemb == 0, i fail to see any similar wording stating that base shall not be accessed if nmemb == 0. Consequently, this patch is also likely to improve standard conformance and portability. Minor issue found by Stefan Esser with UBSAN. He sent a patch to bugs@, but my patch differs in a minor way. --- diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c index b60f6229a2c..1ade9b170a8 100644 --- a/usr.bin/mandoc/mansearch.c +++ b/usr.bin/mandoc/mansearch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mansearch.c,v 1.65 2019/07/01 22:43:03 schwarze Exp $ */ +/* $OpenBSD: mansearch.c,v 1.66 2022/01/13 04:06:10 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013-2018 Ingo Schwarze @@ -217,7 +217,7 @@ mansearch(const struct mansearch *search, if (cur && search->firstmatch) break; } - if (res != NULL) + if (res != NULL && cur > 1) qsort(*res, cur, sizeof(struct manpage), manpage_compare); if (chdir_status && getcwd_status && chdir(buf) == -1) warn("%s", buf);