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.
This commit is contained in:
Bruno Haible 2025-06-22 01:20:32 +02:00
parent 96d16fc051
commit 5a22e428a6
4 changed files with 23 additions and 6 deletions

View 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) \

View 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)

View 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)

View File

@ -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