From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 19187 invoked from network); 13 Jan 2022 04:06:48 -0000 Received: from bsd.lv (HELO mandoc.bsd.lv) (66.111.2.12) by inbox.vuxu.org with ESMTPUTF8; 13 Jan 2022 04:06:48 -0000 Received: from fantadrom.bsd.lv (localhost [127.0.0.1]) by mandoc.bsd.lv (OpenSMTPD) with ESMTP id 09717b7b for ; Wed, 12 Jan 2022 23:06:46 -0500 (EST) Received: from localhost (mandoc.bsd.lv [local]) by mandoc.bsd.lv (OpenSMTPD) with ESMTPA id e435c72d for ; Wed, 12 Jan 2022 23:06:46 -0500 (EST) Date: Wed, 12 Jan 2022 23:06:46 -0500 (EST) X-Mailinglist: mandoc-source Reply-To: source@mandoc.bsd.lv MIME-Version: 1.0 From: schwarze@mandoc.bsd.lv To: source@mandoc.bsd.lv Subject: mandoc: Only sort the result array if it contains more than one element, X-Mailer: activitymail 1.26, http://search.cpan.org/dist/activitymail/ Content-Type: text/plain; charset=utf-8 Message-ID: <33640171db9cdae6@mandoc.bsd.lv> Log Message: ----------- 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. Modified Files: -------------- mandoc: mansearch.c Revision Data ------------- Index: mansearch.c =================================================================== RCS file: /home/cvs/mandoc/mandoc/mansearch.c,v retrieving revision 1.82 retrieving revision 1.83 diff -Lmansearch.c -Lmansearch.c -u -p -r1.82 -r1.83 --- mansearch.c +++ mansearch.c @@ -1,4 +1,4 @@ -/* $Id$ */ +/* $Id$ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013-2018 Ingo Schwarze @@ -220,7 +220,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); -- To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv