From: Bruno Haible Date: Mon, 10 Jun 2024 15:39:28 +0000 (+0200) Subject: Fix undefined behaviour according to ISO C 23 § 6.5.6.(9). X-Git-Tag: v0.23~256 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3cf8c3276a088311b28100febe51a90f5e3fe68c;p=thirdparty%2Fgettext.git Fix undefined behaviour according to ISO C 23 § 6.5.6.(9). Found by clang's undefined-behaviour sanitizer. * gettext-tools/src/msgl-fsearch.c (mult_index_list_accumulate): If len1 == 0, skip two loops. --- diff --git a/gettext-tools/src/msgl-fsearch.c b/gettext-tools/src/msgl-fsearch.c index 08543514a..c9ba623f0 100644 --- a/gettext-tools/src/msgl-fsearch.c +++ b/gettext-tools/src/msgl-fsearch.c @@ -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 , 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) {