]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
locale: Fix UB on insert_weights
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 18 Apr 2025 12:27:29 +0000 (09:27 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 13:41:34 +0000 (10:41 -0300)
The ubsan triggers:

UBSAN: Undefined behaviour in programs/ld-collate.c:862:5 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0,

The memcpy is only requires if current 'weights' is nonnull, so
check it before calling it.

locale/programs/ld-collate.c

index 7de3ba064d8bb6ecba10c574c5478610cb18bb1f..4fa08bd273d5736366bac43d75f3874871a1fd3f 100644 (file)
@@ -858,7 +858,8 @@ insert_weights (struct linereader *ldfile, struct element_t *elem,
                  max += 10;
                  newp = (struct element_t **)
                    alloca (max * sizeof (struct element_t *));
-                 memcpy (newp, weights, cnt * sizeof (struct element_t *));
+                 if (weights != NULL)
+                   memcpy (newp, weights, cnt * sizeof (struct element_t *));
                  weights = newp;
                }
              weights[cnt++] = charelem;