coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[INSTALLED 2/5] sort: pacify GCC 12 false positive


From: Paul Eggert
Subject: [INSTALLED 2/5] sort: pacify GCC 12 false positive
Date: Tue, 17 May 2022 19:32:22 -0700

* src/sort.c (keycompare): Rework to pacify a GCC 12
-Wmaybe-uninitialized false positive, by coalescing some minor
duplicate code and eliminating a branch.  This should execute an
insn or two less in the usual case.
---
 src/sort.c | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/src/sort.c b/src/sort.c
index 8af356c66..72debe0ca 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2634,14 +2634,15 @@ keycompare (struct line const *a, struct line const *b)
       if (hard_LC_COLLATE || key_numeric (key)
           || key->month || key->random || key->version)
         {
-          char *ta;
-          char *tb;
-          size_t tlena;
-          size_t tlenb;
-
-          char enda;
-          char endb;
-          void *allocated;
+          /* Ordinarily use the keys in-place, temporarily null-terminated.  */
+          char *ta = texta;
+          char *tb = textb;
+          size_t tlena = lena;
+          size_t tlenb = lenb;
+          char enda = ta[tlena];
+          char endb = tb[tlenb];
+
+          void *allocated = NULL;
           char stackbuf[4000];
 
           if (ignore || translate)
@@ -2655,7 +2656,7 @@ keycompare (struct line const *a, struct line const *b)
               /* Allocate space for copies.  */
               size_t size = lena + 1 + lenb + 1;
               if (size <= sizeof stackbuf)
-                ta = stackbuf, allocated = NULL;
+                ta = stackbuf;
               else
                 ta = allocated = xmalloc (size);
               tb = ta + lena + 1;
@@ -2667,22 +2668,17 @@ keycompare (struct line const *a, struct line const *b)
                   ta[tlena++] = (translate
                                  ? translate[to_uchar (texta[i])]
                                  : texta[i]);
-              ta[tlena] = '\0';
 
               for (tlenb = i = 0; i < lenb; i++)
                 if (! (ignore && ignore[to_uchar (textb[i])]))
                   tb[tlenb++] = (translate
                                  ? translate[to_uchar (textb[i])]
                                  : textb[i]);
-              tb[tlenb] = '\0';
-            }
-          else
-            {
-              /* Use the keys in-place, temporarily null-terminated.  */
-              ta = texta; tlena = lena; enda = ta[tlena]; ta[tlena] = '\0';
-              tb = textb; tlenb = lenb; endb = tb[tlenb]; tb[tlenb] = '\0';
             }
 
+          ta[tlena] = '\0';
+          tb[tlenb] = '\0';
+
           if (key->numeric)
             diff = numcompare (ta, tb);
           else if (key->general_numeric)
@@ -2707,13 +2703,11 @@ keycompare (struct line const *a, struct line const *b)
                 diff = xmemcoll0 (ta, tlena + 1, tb, tlenb + 1);
             }
 
-          if (ignore || translate)
+          ta[tlena] = enda;
+          tb[tlenb] = endb;
+
+          if (allocated)
             free (allocated);
-          else
-            {
-              ta[tlena] = enda;
-              tb[tlenb] = endb;
-            }
         }
       else if (ignore)
         {
-- 
2.36.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]