From: Bruno Haible Date: Sun, 12 Mar 2023 11:28:00 +0000 (+0100) Subject: Fix python-brace-format: Disallow empty precision. X-Git-Tag: v0.22~117 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c8bf9978901a484516a0181c581bfe8915defbbc;p=thirdparty%2Fgettext.git 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. --- 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:}" # Valid: nested format specifier