summary refs log tree commit diff
path: root/localedata/collate-test.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-11-03 16:20:05 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-11-08 15:28:48 -0300
commit3a523ccd78de1a7eff5acf6850ecae47a78cc611 (patch)
treedd634f9bbea4a27065b988082abd38ee9e16658d /localedata/collate-test.c
parenta6a9c1a36b48f304c8a2138c147d88a448161366 (diff)
downloadglibc-3a523ccd78de1a7eff5acf6850ecae47a78cc611.tar.gz
glibc-3a523ccd78de1a7eff5acf6850ecae47a78cc611.tar.xz
glibc-3a523ccd78de1a7eff5acf6850ecae47a78cc611.zip
locale: Fix localedata/sort-test undefined behavior
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.
Diffstat (limited to 'localedata/collate-test.c')
-rw-r--r--localedata/collate-test.c11
1 files 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);
     }