diff mbox series

[03/11] locale: Fix --enable-ubsan build failure on some ABIs

Message ID 20250507142110.3452012-4-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
On mips, arc, powerpc, and s390 gcc 14 triggers the warning

In function ‘charmap_new_char’,
    inlined from ‘parse_charmap.isra’ at ../locale/programs/charmap.c:570:6:
../locale/programs/charmap.c:1017:32: error: ‘strncmp’ specified bound [2147483649, 4294967295] exceeds maximum object size 2147483647 [-Werror=stringop-overread]
 1017 |   if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../locale/programs/charmap.c:1017:32: error: ‘strncmp’ specified bound [2147483649, 4294967295] exceeds maximum object size 2147483647 [-Werror=stringop-overread]
cc1: all warnings being treated as errors

So move the case to an special function and disable the sanitizer.
---
 locale/programs/charmap.c    | 2 +-
 locale/programs/charmap.h    | 7 +++++++
 locale/programs/repertoire.c | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/locale/programs/charmap.c b/locale/programs/charmap.c
index 7768a7a7fc..58433c8c5b 100644
--- a/locale/programs/charmap.c
+++ b/locale/programs/charmap.c
@@ -1014,7 +1014,7 @@  hexadecimal range format should use only capital characters"));
 
   prefix_len = (cp - from) + 1;
 
-  if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
+  if (check_illegal_range (cp, from, len1, to, prefix_len))
     goto illegal_range;
 
   errno = 0;
diff --git a/locale/programs/charmap.h b/locale/programs/charmap.h
index dcdbfe1828..b4aa8d6b6c 100644
--- a/locale/programs/charmap.h
+++ b/locale/programs/charmap.h
@@ -62,6 +62,13 @@  struct charseq
   unsigned char bytes[];
 };
 
+static inline bool
+__attribute_disable_ubsan__
+check_illegal_range (const char *cp, const char *from, size_t len1,
+		     const char *to, size_t prefix_len)
+{
+  return cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0;
+}
 
 /* True if the encoding is not ASCII compatible.  */
 extern bool enc_not_ascii_compatible;
diff --git a/locale/programs/repertoire.c b/locale/programs/repertoire.c
index 7ed8c915dd..99f560fc45 100644
--- a/locale/programs/repertoire.c
+++ b/locale/programs/repertoire.c
@@ -433,7 +433,7 @@  hexadecimal range format should use only capital characters"));
 
   prefix_len = (cp - from) + 1;
 
-  if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
+  if (check_illegal_range (cp, from, len1, to, prefix_len))
     goto invalid_range;
 
   errno = 0;