public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Vincent Lefevre <vincent@vinc17.net>,
	Xi Ruoyao <xry111@xry111.site>,
	Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>,
	Turritopsis Dohrnii Teo En Ming <teo.en.ming@protonmail.com>,
	"libc-alpha@sourceware.org" <libc-alpha@sourceware.org>,
	"ceo@teo-en-ming-corp.com" <ceo@teo-en-ming-corp.com>
Subject: Re: New GNU C Library (glibc) security flaw reported on 30 Jan 2024
Date: Thu, 1 Feb 2024 11:55:22 -0800	[thread overview]
Message-ID: <5ea9eabb-f047-490f-abe9-43630d79c395@cs.ucla.edu> (raw)
In-Reply-To: <20240201090721.GH3044@qaa.vinc17.org>

[-- Attachment #1: Type: text/plain, Size: 134 bytes --]

No matter what we do to the qsort implementation, the manual should 
avoid any confusion about qsorting NaNs. Proposed patch attached.

[-- Attachment #2: 0001-stdlib-fix-qsort-example-in-manual.patch --]
[-- Type: text/x-patch, Size: 2107 bytes --]

From 8ff74efcc11a77e221808293ed390faf6dea86fe Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 1 Feb 2024 11:52:46 -0800
Subject: [PATCH] stdlib: fix qsort example in manual

* manual/search.texi (Comparison Functions, Array Sort Function):
Sort an array of long ints, not doubles, to avoid hassles
with NaNs.
---
 manual/search.texi | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/manual/search.texi b/manual/search.texi
index ffaadc46f5..db577a5332 100644
--- a/manual/search.texi
+++ b/manual/search.texi
@@ -35,19 +35,22 @@ second, zero if they are ``equal'', and positive if the first argument
 is ``greater''.
 
 Here is an example of a comparison function which works with an array of
-numbers of type @code{double}:
+numbers of type @code{long int}:
 
 @smallexample
 int
-compare_doubles (const void *a, const void *b)
+compare_long_ints (const void *a, const void *b)
 @{
-  const double *da = (const double *) a;
-  const double *db = (const double *) b;
+  const long int *la = a;
+  const long int *lb = b;
 
-  return (*da > *db) - (*da < *db);
+  return (*la > *lb) - (*la < *lb);
 @}
 @end smallexample
 
+(The code would have to be more complicated for an array of @code{double},
+to handle NaNs correctly.)
+
 The header file @file{stdlib.h} defines a name for the data type of
 comparison functions.  This type is a GNU extension.
 
@@ -183,16 +186,16 @@ in the array before making some comparisons.  The only way to perform
 a stable sort with @code{qsort} is to first augment the objects with a
 monotonic counter of some kind.
 
-Here is a simple example of sorting an array of doubles in numerical
+Here is a simple example of sorting an array of @code{long int} in numerical
 order, using the comparison function defined above (@pxref{Comparison
 Functions}):
 
 @smallexample
 @{
-  double *array;
-  int size;
+  long int *array;
+  size_t nmemb;
   @dots{}
-  qsort (array, size, sizeof (double), compare_doubles);
+  qsort (array, nmemb, sizeof *array, compare_long_ints);
 @}
 @end smallexample
 
-- 
2.40.1


  reply	other threads:[~2024-02-01 19:55 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-31 14:08 Turritopsis Dohrnii Teo En Ming
2024-01-31 14:23 ` Xi Ruoyao
2024-01-31 14:55   ` Vincent Lefevre
2024-01-31 15:52     ` Adhemerval Zanella Netto
2024-01-31 16:23       ` Vincent Lefevre
2024-01-31 16:44         ` Siddhesh Poyarekar
2024-01-31 18:47       ` Xi Ruoyao
2024-02-01  0:51         ` Vincent Lefevre
2024-02-01  1:03           ` Vincent Lefevre
2024-02-01  6:41           ` Xi Ruoyao
2024-02-01  9:07             ` Vincent Lefevre
2024-02-01 19:55               ` Paul Eggert [this message]
2024-02-01 21:11                 ` Siddhesh Poyarekar
2024-02-05  0:58                   ` Paul Eggert
2024-02-06 15:00                     ` Zack Weinberg
2024-02-06 21:30                       ` Paul Eggert
2024-02-06 22:04                         ` Xi Ruoyao
2024-02-07 17:07                         ` Zack Weinberg
2024-02-07 19:55                           ` Alexander Monakov
2024-02-07 20:45                             ` Zack Weinberg
2024-02-07 21:53                               ` Alexander Monakov
2024-02-07 22:56                               ` Paul Eggert
2024-04-06 17:17                           ` Paul Eggert
2024-04-08  8:28                             ` Florian Weimer
2024-04-22 14:39                               ` Zack Weinberg
2024-04-23 18:09                                 ` Paul Eggert
2024-04-23 18:26                                   ` Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5ea9eabb-f047-490f-abe9-43630d79c395@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=adhemerval.zanella@linaro.org \
    --cc=ceo@teo-en-ming-corp.com \
    --cc=libc-alpha@sourceware.org \
    --cc=teo.en.ming@protonmail.com \
    --cc=vincent@vinc17.net \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).