public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org, Paul Eggert <eggert@cs.ucla.edu>,
	"H . J . Lu" <hjl.tools@gmail.com>
Subject: [PATCH v3] locale: Fix localedata/sort-test undefined behavior
Date: Mon,  8 Nov 2021 15:29:52 -0300	[thread overview]
Message-ID: <20211108182952.1004707-1-adhemerval.zanella@linaro.org> (raw)

The collate-test.c triggers UB with an signed integer overflow,
which results in an error on some architectures (powerpc32).

Checked on x86_64, i686, and powerpc.
---
 localedata/collate-test.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/localedata/collate-test.c b/localedata/collate-test.c
index 46b91ec57f..f962332517 100644
--- a/localedata/collate-test.c
+++ b/localedata/collate-test.c
@@ -32,6 +32,12 @@ struct lines
 
 static int xstrcoll (const void *, const void *);
 
+static int
+signum (int n)
+{
+  return (0 < n) - (n < 0);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -86,7 +92,7 @@ main (int argc, char *argv[])
   srandom (atoi (argv[1]));
   for (n = 0; n < 10 * nstrings; ++n)
     {
-      int r1, r2, r;
+      int r1, r2;
       size_t idx1 = random () % nstrings;
       size_t idx2 = random () % nstrings;
       struct lines tmp = strings[idx1];
@@ -96,9 +102,8 @@ main (int argc, char *argv[])
       /* While we are at it a first little test.  */
       r1 = strcoll (strings[idx1].key, strings[idx2].key);
       r2 = strcoll (strings[idx2].key, strings[idx1].key);
-      r = r1 * r2;
 
-      if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
+      if (signum (r1) != - signum (r2))
 	printf ("`%s' and `%s' collate wrong: %d vs. %d\n",
 		strings[idx1].key, strings[idx2].key, r1, r2);
     }
-- 
2.32.0


             reply	other threads:[~2021-11-08 18:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-08 18:29 Adhemerval Zanella [this message]
2021-11-08 18:46 ` Paul Eggert

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=20211108182952.1004707-1-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=eggert@cs.ucla.edu \
    --cc=hjl.tools@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /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).