summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2023-03-12 12:28:00 +0100
committerBruno Haible <bruno@clisp.org>2023-03-14 02:57:28 +0100
commitc8bf9978901a484516a0181c581bfe8915defbbc (patch)
tree9ab8f54bf934fde819d2dd164ee1745eaf1db82a
parenta03afb970c6abea8ac22b16df3cfc3419fdc795f (diff)
downloadgettext-c8bf9978901a484516a0181c581bfe8915defbbc.tar.gz
Fix python-brace-format: Disallow empty precision.
* gettext-tools/src/format-python-brace.c (parse_directive): After '.', expect at least one digit. * gettext-tools/tests/format-python-brace-1: Add a test case.
-rw-r--r--gettext-tools/src/format-python-brace.c18
-rwxr-xr-xgettext-tools/tests/format-python-brace-12
2 files changed, 18 insertions, 2 deletions
diff --git a/gettext-tools/src/format-python-brace.c b/gettext-tools/src/format-python-brace.c
index 229917952..c5c0a076e 100644
--- a/gettext-tools/src/format-python-brace.c
+++ b/gettext-tools/src/format-python-brace.c
@@ -34,6 +34,9 @@
/* Python brace format strings are defined by PEP3101 together with the
'format' method of the string class.
+ Documentation:
+ https://peps.python.org/pep-3101/
+ https://docs.python.org/3/library/string.html#formatstrings
A format string directive here consists of
- an opening brace '{',
- an identifier [_A-Za-z][_0-9A-Za-z]*|[0-9]+,
@@ -262,20 +265,30 @@ parse_directive (struct spec *spec,
format += 2;
else if (c1 == '<' || c1 == '>' || c1 == '=' || c1 == '^')
format++;
+
if (*format == '+' || *format == '-' || *format == ' ')
format++;
if (*format == '#')
format++;
if (*format == '0')
format++;
+
+ /* Parse the optional minimumwidth. */
while (c_isdigit (*format))
format++;
+
+ /* Parse the optional .precision. */
if (*format == '.')
{
format++;
- while (c_isdigit (*format))
- format++;
+ if (c_isdigit (*format))
+ do
+ format++;
+ while (c_isdigit (*format));
+ else
+ format--;
}
+
switch (*format)
{
case 'b': case 'c': case 'd': case 'o': case 'x': case 'X':
@@ -287,6 +300,7 @@ parse_directive (struct spec *spec,
default:
break;
}
+
if (*format != '}')
{
*invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
diff --git a/gettext-tools/tests/format-python-brace-1 b/gettext-tools/tests/format-python-brace-1
index 593e3a652..4c7b1b35d 100755
--- a/gettext-tools/tests/format-python-brace-1
+++ b/gettext-tools/tests/format-python-brace-1
@@ -36,6 +36,8 @@ cat <<\EOF > f-pyb-1.data
"abc{value:0}"
# Valid: standard format specifier
"abc{value:<<-#012.34e}"
+# Invalid: empty precision
+"abc{value:8.}"
# Invalid: non-standard format specifier
"abc{value:<c>}"
# Valid: nested format specifier