diff mbox series

[09/11] locale: Fix UB on add_locale_uint32_array

Message ID 20250507142110.3452012-10-adhemerval.zanella@linaro.org
State New
Headers show
Series Add initial support for --enable-ubsan | expand

Commit Message

Adhemerval Zanella Netto May 7, 2025, 2:17 p.m. UTC
The ubsan triggers:

UBSAN: Undefined behaviour in programs/locfile.c:644:3 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0

The obstack_grow is only required if there is extra elements to be
inserted (n_elems > 0).
---
 locale/programs/locfile.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Florian Weimer May 20, 2025, 12:49 p.m. UTC | #1
* Adhemerval Zanella:

> The ubsan triggers:
>
> UBSAN: Undefined behaviour in programs/locfile.c:644:3 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0
>
> The obstack_grow is only required if there is extra elements to be
> inserted (n_elems > 0).
> ---
>  locale/programs/locfile.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/locale/programs/locfile.c b/locale/programs/locfile.c
> index b54fcbbceb..7907c949ea 100644
> --- a/locale/programs/locfile.c
> +++ b/locale/programs/locfile.c
> @@ -641,6 +641,8 @@ add_locale_uint32_array (struct locale_file *file,
>  {
>    align_locale_data (file, LOCFILE_ALIGN);
>    record_offset (file);
> +  if (n_elems == 0)
> +    return;
>    obstack_grow (&file->data, data, n_elems * sizeof (uint32_t));
>    maybe_swap_uint32_obstack (&file->data, n_elems);
>  }

We should fix the declaration of obstack_grow instead, to align with the
new declarations for memcpy et al.

Thanks,
Florian
diff mbox series

Patch

diff --git a/locale/programs/locfile.c b/locale/programs/locfile.c
index b54fcbbceb..7907c949ea 100644
--- a/locale/programs/locfile.c
+++ b/locale/programs/locfile.c
@@ -641,6 +641,8 @@  add_locale_uint32_array (struct locale_file *file,
 {
   align_locale_data (file, LOCFILE_ALIGN);
   record_offset (file);
+  if (n_elems == 0)
+    return;
   obstack_grow (&file->data, data, n_elems * sizeof (uint32_t));
   maybe_swap_uint32_obstack (&file->data, n_elems);
 }