]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
javascript-format: Reject null precision.
authorBruno Haible <bruno@clisp.org>
Sat, 21 Jun 2025 23:20:32 +0000 (01:20 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 21 Jun 2025 23:20:32 +0000 (01:20 +0200)
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
gettext-tools/src/format-java-printf.c
gettext-tools/src/format-javascript.c
gettext-tools/tests/format-javascript-1

index d127103fff922857392287a1d3a6681fb56e9e0d..763ca5988845f55c6f0039f0800a1c072e8739b3 100644 (file)
@@ -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 <bruno@clisp.org>, 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) \
index 82283cd6957a079f949a969dc71d6c8860663bca..a22bbf50e543bc2ebf510374e5dd2b79b7b1a4e8 100644 (file)
@@ -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)
 
index d237bf61fe8ef849dc0df86227818990c478e097..01dc384d1daf32891ef1183c6dd9628d80bb3c81 100644 (file)
@@ -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)
index c0a4aa3c46e78102fa4e4315076b928be70c750a..beef9b9bd38a056d9b23cb6de5f1aa1b972f9c41 100755 (executable)
@@ -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