diff mbox series

[07/11] localte: Fix UB on collate_finish

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

Commit Message

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

UBSAN: Undefined behaviour in programs/ld-collate.c:1557:7 variable length array bound evaluates to non-positive value 0

The VLA is allocated with nrules being 0.  To simplify the fix,
just allocate one for this case.
---
 locale/programs/ld-collate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andreas Schwab May 7, 2025, 2:40 p.m. UTC | #1
On Mai 07 2025, Adhemerval Zanella wrote:

> diff --git a/locale/programs/ld-collate.c b/locale/programs/ld-collate.c
> index 4fa08bd273..5ed03f4cbf 100644
> --- a/locale/programs/ld-collate.c
> +++ b/locale/programs/ld-collate.c
> @@ -1554,7 +1554,7 @@ collate_finish (struct localedef_t *locale, const struct charmap_t *charmap)
>       The multibyte case is easy.  We simply sort into an array with
>       256 elements.  */
>    struct locale_collate_t *collate = locale->categories[LC_COLLATE].collate;
> -  int mbact[nrules];
> +  int mbact[nrules == 0 ? 1 : nrules];

nrules is guaranteed to be at most sizeof (((struct element_t *)
0)->used_in_level) * 8, so this can use a fixed array.
Florian Weimer May 7, 2025, 7:20 p.m. UTC | #2
* Adhemerval Zanella:

> The ubsan triggers:
>
> UBSAN: Undefined behaviour in programs/ld-collate.c:1557:7 variable length array bound evaluates to non-positive value 0
>
> The VLA is allocated with nrules being 0.  To simplify the fix,
> just allocate one for this case.

Typo in commit subject: local[]e
diff mbox series

Patch

diff --git a/locale/programs/ld-collate.c b/locale/programs/ld-collate.c
index 4fa08bd273..5ed03f4cbf 100644
--- a/locale/programs/ld-collate.c
+++ b/locale/programs/ld-collate.c
@@ -1554,7 +1554,7 @@  collate_finish (struct localedef_t *locale, const struct charmap_t *charmap)
      The multibyte case is easy.  We simply sort into an array with
      256 elements.  */
   struct locale_collate_t *collate = locale->categories[LC_COLLATE].collate;
-  int mbact[nrules];
+  int mbact[nrules == 0 ? 1 : nrules];
   int wcact;
   int mbseqact;
   int wcseqact;