[PATCH 06/11] locale: Fix UB on insert_weights

Florian Weimer fweimer@redhat.com
Tue May 20 12:45:37 GMT 2025


* Adhemerval Zanella:

> 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 | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/locale/programs/ld-collate.c b/locale/programs/ld-collate.c
> index 7de3ba064d..4fa08bd273 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;

I don't think we should make this change, considering that memcpy is
going to accept null arguments for length zero soon.

Thanks,
Florian



More information about the Libc-alpha mailing list