This avoids issues on misconfigured systems like with GLIBC:
  https://sourceware.org/PR35092
This unlikely case can break various assumptions that users expect.
E.g. it would trigger buffer overflows in `numfmt -d` or `paste -d`,
or mbs_endswidth (numfmt -s).  By returning an error, users can
reject the input or fall back to byte oriented processing.

* lib/mcel.h: Return an error when decoded more than MB_CUR_MAX.
* modules/mcel: Add stdib-h for MB_CUR_MAX definition.
* m4/locale-yi.m4: New test case.
* modules/mcel-tests: Likewise.
* tests/test-mcel-6.sh: Likewise.
* tests/test-mcel.c: Likewise.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-6NCJ14Q1.
---
 ChangeLog            | 10 ++++++++++
 lib/mcel.h           |  6 ++++++
 m4/locale-yi.m4      | 38 ++++++++++++++++++++++++++++++++++++++
 modules/mcel         |  1 +
 modules/mcel-tests   |  7 ++++++-
 tests/test-mcel-6.sh | 15 +++++++++++++++
 tests/test-mcel.c    | 11 +++++++++++
 7 files changed, 87 insertions(+), 1 deletion(-)
 create mode 100644 m4/locale-yi.m4
 create mode 100755 tests/test-mcel-6.sh

diff --git a/ChangeLog b/ChangeLog
index 3e80ab4f47..4b5c91414a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2026-12-02  Pádraig Brady  <[email protected]>
+
+       mcel: treat any decoded length > MB_CUR_MAX as an error
+       * lib/mcel.h: Return an error when decoded more than MB_CUR_MAX.
+       * modules/mcel: Add stdib-h for MB_CUR_MAX definition.
+       * m4/locale-yi.m4: New test case.
+       * modules/mcel-tests: Likewise.
+       * tests/test-mcel-6.sh: Likewise.
+       * tests/test-mcel.c: Likewise.
+
 2026-12-02  Bruno Haible  <[email protected]>
 
        Fix link error when libunistring 1.5 is preinstalled (regr. 2026-11-17).
diff --git a/lib/mcel.h b/lib/mcel.h
index 5eedd5b610..29753c86a5 100644
--- a/lib/mcel.h
+++ b/lib/mcel.h
@@ -101,6 +101,7 @@
 
 #include <limits.h>
 #include <stddef.h>
+#include <stdlib.h>
 #include <uchar.h>
 
 _GL_INLINE_HEADER_BEGIN
@@ -267,6 +268,11 @@ mcel_scan (char const *p, char const *lim)
   if (_GL_UNLIKELY ((size_t) -1 / 2 < len))
     return mcel_err (c);
 
+  /* Any LEN > MB_CUR_MAX can break assumptions.
+     This avoids https://sourceware.org/PR35092  */
+  if (_GL_UNLIKELY (MB_CUR_MAX < len))
+    return mcel_err (c);
+
   /* A multi-byte character.  LEN must be positive,
      as *P != '\0' and shift sequences are not supported.  */
   return mcel_ch (ch, len);
diff --git a/m4/locale-yi.m4 b/m4/locale-yi.m4
new file mode 100644
index 0000000000..8467bfa5c4
--- /dev/null
+++ b/m4/locale-yi.m4
@@ -0,0 +1,38 @@
+# locale-yi.m4
+# serial 1
+dnl Copyright (C) 2026 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+dnl This file is offered as-is, without any warranty.
+
+dnl Determine the name of a Yiddish locale with CP1255 encoding.
+AC_DEFUN_ONCE([gt_LOCALE_YI_US],
+[
+  AC_CACHE_CHECK([for a Yiddish locale with CP1255 encoding],
+    [gt_cv_locale_yi_US], [
+      gt_cv_locale_yi_US=none
+      for gt_locale in yi_US.CP1255 yi_US; do
+        AC_RUN_IFELSE(
+          [AC_LANG_PROGRAM([[
+#include <locale.h>
+#include <stdlib.h>
+             ]], [[
+               wchar_t wc;
+               /* In CP1255, byte 0xA4 denotes the new sheqel sign.  */
+               return !(setlocale (LC_ALL, "$gt_locale") != NULL
+                        && mbtowc (&wc, "\244", 1) == 1 && wc == 0x20aa);
+             ]])],
+          [gt_cv_locale_yi_US=$gt_locale], [], [break])
+        test "$gt_cv_locale_yi_US" = none || break
+      done
+    ])
+  LOCALE_YI_US=$gt_cv_locale_yi_US
+  case $LOCALE_YI_US in #(
+    '' | *[[[:space:]\"\$\'*@<:@]]*)
+      dnl This locale name might cause trouble with sh or make.
+      AC_MSG_WARN([invalid locale "$LOCALE_YI_US"; assuming "none"])
+      LOCALE_YI_US=none;;
+  esac
+  AC_SUBST([LOCALE_YI_US])
+])
diff --git a/modules/mcel b/modules/mcel
index d77b7d9345..8dcdd9f6f5 100644
--- a/modules/mcel
+++ b/modules/mcel
@@ -12,6 +12,7 @@ extern-inline
 limits-h
 mbrtoc32
 bool
+stdlib-h
 uchar-h
 verify
 
diff --git a/modules/mcel-tests b/modules/mcel-tests
index 9cc6913252..fc8d382c40 100644
--- a/modules/mcel-tests
+++ b/modules/mcel-tests
@@ -4,11 +4,13 @@ tests/test-mcel-2.sh
 tests/test-mcel-3.sh
 tests/test-mcel-4.sh
 tests/test-mcel-5.sh
+tests/test-mcel-6.sh
 tests/test-mcel.c
 tests/macros.h
 m4/locale-en.m4
 m4/locale-fr.m4
 m4/locale-ja.m4
+m4/locale-yi.m4
 m4/locale-zh.m4
 m4/codeset.m4
 
@@ -20,16 +22,19 @@ gt_LOCALE_EN_UTF8
 gt_LOCALE_FR
 gt_LOCALE_FR_UTF8
 gt_LOCALE_JA
+gt_LOCALE_YI_US
 gt_LOCALE_ZH_CN
 
 Makefile.am:
 TESTS += \
-  test-mcel-1.sh test-mcel-2.sh test-mcel-3.sh test-mcel-4.sh test-mcel-5.sh
+  test-mcel-1.sh test-mcel-2.sh test-mcel-3.sh test-mcel-4.sh test-mcel-5.sh \
+  test-mcel-6.sh
 TESTS_ENVIRONMENT += \
   LOCALE_EN_UTF8='@LOCALE_EN_UTF8@' \
   LOCALE_FR='@LOCALE_FR@' \
   LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
   LOCALE_JA='@LOCALE_JA@' \
+  LOCALE_YI_US='@LOCALE_YI_US@' \
   LOCALE_ZH_CN='@LOCALE_ZH_CN@'
 check_PROGRAMS += test-mcel
 test_mcel_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(MBRTOWC_LIB) 
$(LIBC32CONV)
diff --git a/tests/test-mcel-6.sh b/tests/test-mcel-6.sh
new file mode 100755
index 0000000000..3bf4db08dc
--- /dev/null
+++ b/tests/test-mcel-6.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific CP1255 locale is installed.
+: "${LOCALE_YI_US=yi_US.CP1255}"
+if test "$LOCALE_YI_US" = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no Yiddish CP1255 locale is installed"
+  else
+    echo "Skipping test: no Yiddish CP1255 locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_YI_US \
+${CHECKER} ./test-mcel${EXEEXT} 6
diff --git a/tests/test-mcel.c b/tests/test-mcel.c
index cdecc3cc75..9916a33be7 100644
--- a/tests/test-mcel.c
+++ b/tests/test-mcel.c
@@ -19,6 +19,7 @@
 #include <mcel.h>
 
 #include <locale.h>
+#include <stdlib.h>
 
 #include "macros.h"
 
@@ -50,6 +51,16 @@ main (int argc, char *argv[])
   if (setlocale (LC_ALL, "") == NULL)
     return 1;
 
+  if (argc > 1 && argv[1][0] == '6')
+    {
+      /* In CP1255, glibc can consume both bytes of yod-hiriq even
+         when MB_CUR_MAX is 1.  See <https://sourceware.org/PR35092>.  */
+      static char const s[] = "\351\304";
+      mcel_t g = mcel_scan (s, s + sizeof s - 1);
+      ASSERT (g.len <= MB_CUR_MAX);
+      return test_exit_status;
+    }
+
   mcel_t prev;
   for (int ch = 0; ch < 0x80; ch++)
     {
-- 
2.56.0


Reply via email to