From 3cf8c3276a088311b28100febe51a90f5e3fe68c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 10 Jun 2024 17:39:28 +0200 Subject: [PATCH] =?utf8?q?Fix=20undefined=20behaviour=20according=20to=20I?= =?utf8?q?SO=20C=2023=20=C2=A7=206.5.6.(9).?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 56 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 26 deletions(-) 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) { -- 2.47.3