]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Fix python-brace-format: Disallow empty precision.
authorBruno Haible <bruno@clisp.org>
Sun, 12 Mar 2023 11:28:00 +0000 (12:28 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 14 Mar 2023 01:57:28 +0000 (02:57 +0100)
* 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.

gettext-tools/src/format-python-brace.c
gettext-tools/tests/format-python-brace-1

index 229917952a301e10583f753ae96a025f1103c3ba..c5c0a076e412cf476d3960cd5e93fdd07a35cd5a 100644 (file)
@@ -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 ();
index 593e3a65209a466eb11e7e72f359ee504dfeddb0..4c7b1b35df089ae9020749d46b4a53e4c239ee8b 100755 (executable)
@@ -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