[PATCH v2 0/2] intl: Import plural expression hardening from GNU gettext

Avinal Kumar avinal.xlvii@gmail.com
Fri May 1 11:24:03 GMT 2026


Changes from v1:

- Added adapted tests for plural expression hardening from gettext.

I ran the tests against a non-patched glibc and they crash as expected:

  Didn't expect signal from child: got `Floating point exception'
  ...
  FAIL: intl/tst-plural-eval
                  === Summary of results ===
        1 FAIL
       19 PASS

Adaptation choices:

- gettext embeds the ~20KB nested expression as a literal string.
  I wrote an AWK script (plural-depth.awk) to generate it instead,
  following glibc's convention of using AWK for test data
  generation (cf. po2test.awk).  Much easier on the eyes.

- The tests are split into a C program (tst-plural-eval.c) and a shell
  script (tst-plural-eval.sh) that creates the .mo test data.  gettext
  uses shell-only tests with its $NGETTEXT binary.

- The division-by-zero trigger value needs to be > 1000 since msgfmt -c
  only validates plural expressions for 0 <= n <= 1000.  gettext uses
  1666; I took the creative liberty of using 1729 — the Hardy-Ramanujan
  number, because if a number is going to crash your program, it might
  as well be an interesting one :)

Used AI for debugging, learning glibc testing styles, prettyfying commits and comments. 

Avinal Kumar (2):
  intl: Import plural expression hardening from GNU gettext
  intl: Add tests for plural expression hardening

 intl/Makefile           |  11 +++-
 intl/dcigettext.c       |  27 +++-----
 intl/eval-plural.h      | 139 ++++++++++++++++++++++++++++++----------
 intl/plural-depth.awk   |  54 ++++++++++++++++
 intl/plural-exp.h       |  21 +++++-
 intl/tst-plural-eval.c  |  75 ++++++++++++++++++++++
 intl/tst-plural-eval.sh |  67 +++++++++++++++++++
 7 files changed, 339 insertions(+), 55 deletions(-)
 create mode 100644 intl/plural-depth.awk
 create mode 100644 intl/tst-plural-eval.c
 create mode 100644 intl/tst-plural-eval.sh

-- 
2.54.0



More information about the Libc-alpha mailing list