]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Fix undefined behaviour according to ISO C 23 ยง 6.5.6.(9).
authorBruno Haible <bruno@clisp.org>
Mon, 10 Jun 2024 15:39:28 +0000 (17:39 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 10 Jun 2024 15:39:28 +0000 (17:39 +0200)
Found by clang's undefined-behaviour sanitizer.

* gettext-tools/src/msgl-fsearch.c (mult_index_list_accumulate): If len1 == 0,
skip two loops.

gettext-tools/src/msgl-fsearch.c

index 08543514aa9f97865c2947f26faa0938874cc505..c9ba623f0f20dfa958839f48d2f29b603823e3c1 100644 (file)
@@ -1,5 +1,5 @@
 /* Fast fuzzy searching among messages.
-   Copyright (C) 2006, 2008, 2011, 2013, 2023 Free Software Foundation, Inc.
+   Copyright (C) 2006-2024 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -375,8 +375,6 @@ mult_index_list_accumulate (struct mult_index_list *accu, index_list_ty list)
   size_t len1 = accu->nitems;
   size_t len2 = list[IL_LENGTH];
   size_t need = len1 + len2;
-  struct mult_index *ptr1;
-  struct mult_index *ptr1_end;
   index_ty *ptr2;
   index_ty *ptr2_end;
   struct mult_index *destptr;
@@ -395,38 +393,44 @@ mult_index_list_accumulate (struct mult_index_list *accu, index_list_ty list)
     }
 
   /* Make a linear pass through accu and list simultaneously.  */
-  ptr1 = accu->item;
-  ptr1_end = ptr1 + len1;
   ptr2 = list + 2;
   ptr2_end = ptr2 + len2;
   destptr = accu->item2;
-  while (ptr1 < ptr1_end && ptr2 < ptr2_end)
+  if (len1 > 0)
     {
-      if (ptr1->index < *ptr2)
-        {
-          *destptr = *ptr1;
-          ptr1++;
-        }
-      else if (ptr1->index > *ptr2)
+      struct mult_index *ptr1;
+      struct mult_index *ptr1_end;
+
+      ptr1 = accu->item;
+      ptr1_end = ptr1 + len1;
+      while (ptr1 < ptr1_end && ptr2 < ptr2_end)
         {
-          destptr->index = *ptr2;
-          destptr->count = 1;
-          ptr2++;
+          if (ptr1->index < *ptr2)
+            {
+              *destptr = *ptr1;
+              ptr1++;
+            }
+          else if (ptr1->index > *ptr2)
+            {
+              destptr->index = *ptr2;
+              destptr->count = 1;
+              ptr2++;
+            }
+          else /* ptr1->index == list[2 + i2] */
+            {
+              destptr->index = ptr1->index;
+              destptr->count = ptr1->count + 1;
+              ptr1++;
+              ptr2++;
+            }
+          destptr++;
         }
-      else /* ptr1->index == list[2 + i2] */
+      while (ptr1 < ptr1_end)
         {
-          destptr->index = ptr1->index;
-          destptr->count = ptr1->count + 1;
+          *destptr = *ptr1;
           ptr1++;
-          ptr2++;
+          destptr++;
         }
-      destptr++;
-    }
-  while (ptr1 < ptr1_end)
-    {
-      *destptr = *ptr1;
-      ptr1++;
-      destptr++;
     }
   while (ptr2 < ptr2_end)
     {