[glibc/azanella/ubsan-undef] locale: Fix UB on insert_weights

Adhemerval Zanella azanella@sourceware.org
Fri Apr 25 19:51:14 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=29e9b1c6fdfe2bca4ad418fcdea546a2d90081c4

commit 29e9b1c6fdfe2bca4ad418fcdea546a2d90081c4
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Fri Apr 18 09:27:29 2025 -0300

    locale: Fix UB on insert_weights
    
    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.

Diff:
---
 locale/programs/ld-collate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/locale/programs/ld-collate.c b/locale/programs/ld-collate.c
index fa1ec1cf35..5ed03f4cbf 100644
--- a/locale/programs/ld-collate.c
+++ b/locale/programs/ld-collate.c
@@ -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;


More information about the Glibc-cvs mailing list