From 5a22e428a6a566097e2f5e2f7d0891509841c51a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 22 Jun 2025 01:20:32 +0200 Subject: [PATCH] javascript-format: Reject null precision. Proof that it's invalid: ------------------------------- foo.js ------------------------------- const Format = imports.format; String.prototype.format = Format.format; print("%.f".format(3.1415916535)); ---------------------------------------------------------------------- $ gjs foo.js * gettext-tools/src/format-invalid.h (INVALID_PRECISION_MISSING): New macro. * gettext-tools/src/format-java-printf.c (INVALID_PRECISION_MISSING): Remove macro. * gettext-tools/src/format-javascript.c: Fix comment regarding the precision. (format_parse): Report an error if the precision is null (empty). * gettext-tools/tests/format-javascript-1: Add a test case with null precision. --- gettext-tools/src/format-invalid.h | 5 ++++- gettext-tools/src/format-java-printf.c | 3 --- gettext-tools/src/format-javascript.c | 19 +++++++++++++++++-- gettext-tools/tests/format-javascript-1 | 2 ++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/gettext-tools/src/format-invalid.h b/gettext-tools/src/format-invalid.h index d127103ff..763ca5988 100644 --- a/gettext-tools/src/format-invalid.h +++ b/gettext-tools/src/format-invalid.h @@ -1,5 +1,5 @@ /* Common reasons that make a format string invalid. - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003-2025 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software: you can redistribute it and/or modify @@ -24,6 +24,9 @@ #define INVALID_MIXES_NUMBERED_UNNUMBERED() \ xstrdup (_("The string refers to arguments both through absolute argument numbers and through unnumbered argument specifications.")) +#define INVALID_PRECISION_MISSING(directive_number) \ + xasprintf (_("In the directive number %u, the precision is missing."), directive_number) + #define INVALID_ARGNO_0(directive_number) \ xasprintf (_("In the directive number %u, the argument number 0 is not a positive integer."), directive_number) #define INVALID_WIDTH_ARGNO_0(directive_number) \ diff --git a/gettext-tools/src/format-java-printf.c b/gettext-tools/src/format-java-printf.c index 82283cd69..a22bbf50e 100644 --- a/gettext-tools/src/format-java-printf.c +++ b/gettext-tools/src/format-java-printf.c @@ -131,9 +131,6 @@ numbered_arg_compare (const void *p1, const void *p2) #define INVALID_LAST_ARG(directive_number) \ xasprintf (_("In the directive number %u, the reference to the argument of the previous directive is invalid."), directive_number) -#define INVALID_PRECISION_MISSING(directive_number) \ - xasprintf (_("In the directive number %u, the precision is missing."), directive_number) - #define INVALID_FLAG_FOR(directive_number,flag_char,conv_char) \ xasprintf (_("In the directive number %u, the flag '%c' is invalid for the conversion '%c'."), directive_number, flag_char, conv_char) diff --git a/gettext-tools/src/format-javascript.c b/gettext-tools/src/format-javascript.c index d237bf61f..01dc384d1 100644 --- a/gettext-tools/src/format-javascript.c +++ b/gettext-tools/src/format-javascript.c @@ -159,12 +159,27 @@ format_parse (const char *format, bool translated, char *fdi, while (c_isdigit (*format)) format++; + /* Parse precision. */ if (*format == '.') { format++; - while (c_isdigit (*format)) - format++; + if (!c_isdigit (*format)) + { + if (*format == '\0') + { + *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE (); + FDI_SET (format - 1, FMTDIR_ERROR); + } + else + { + *invalid_reason = INVALID_PRECISION_MISSING (spec.directives); + FDI_SET (format, FMTDIR_ERROR); + } + goto bad_format; + } + + do format++; while (c_isdigit (*format)); } switch (*format) diff --git a/gettext-tools/tests/format-javascript-1 b/gettext-tools/tests/format-javascript-1 index c0a4aa3c4..beef9b9bd 100755 --- a/gettext-tools/tests/format-javascript-1 +++ b/gettext-tools/tests/format-javascript-1 @@ -38,6 +38,8 @@ cat <<\EOF > f-js-1.data "abc%y" # Invalid: flags after width "abc%1Ig" +# Invalid: null precision +"abc%.f" # Invalid: twice precision "abc%.4.2f" # Valid: three arguments