From: Nikita Popov Date: Wed, 22 Apr 2020 10:53:34 +0000 (+0200) Subject: Diagnose missing format specifier at end of string X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=427cc4f496f2fb1007dff8d05fc77adc1dd54dcf;p=php Diagnose missing format specifier at end of string --- diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 5c67776f5b..3448b86bc2 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -616,9 +616,10 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n case '\0': if (!format_len) { - goto exit; + zend_value_error("Missing format specifier at end of string"); + goto fail; } - break; + /* break missing intentionally */ default: zend_value_error("Unknown format specifier '%c'", *format); @@ -638,7 +639,6 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n goto fail; } -exit: /* possibly, we have to make sure we have room for the terminating null? */ ZSTR_VAL(result)[outpos]=0; ZSTR_LEN(result) = outpos; diff --git a/ext/standard/tests/strings/sprintf_error.phpt b/ext/standard/tests/strings/sprintf_error.phpt index bc34b1d0f3..a65bf318d3 100644 --- a/ext/standard/tests/strings/sprintf_error.phpt +++ b/ext/standard/tests/strings/sprintf_error.phpt @@ -66,6 +66,12 @@ try { echo $e->getMessage(), "\n"; } +try { + var_dump(sprintf("foo %", 42)); +} catch (ValueError $e) { + echo $e->getMessage(), "\n"; +} + echo "Done"; ?> --EXPECTF-- @@ -82,4 +88,5 @@ sprintf() expects at least %d parameter, %d given 4 parameters are required, 2 given 4 parameters are required, 1 given 101 parameters are required, 1 given +Missing format specifier at end of string Done diff --git a/ext/standard/tests/strings/vprintf_variation11.phpt b/ext/standard/tests/strings/vprintf_variation11.phpt index b05d8ac681..5022b3d5c6 100644 --- a/ext/standard/tests/strings/vprintf_variation11.phpt +++ b/ext/standard/tests/strings/vprintf_variation11.phpt @@ -26,7 +26,7 @@ $formats = array( "%10.4o %-10.4o %04o %04.4o", "%'#2o %'2o %'$2o %'_2o", "%o %o %o %o", - "%% %%o %10", + "%% %%o", '%3$o %4$o %1$o %2$o' ); @@ -39,7 +39,7 @@ $args_array = array( array(0123456, 01234567, -01234567, 01234567), array(0111, 02222, -0333333, -044444444), array(0x123b, 0xfAb, 0123, 012), - array(01234, 0567, -01234), + array(01234, 0567), array(03, 04, 01, 02) ); @@ -84,8 +84,8 @@ int(32) int(17) -- Iteration 7 -- -% %o -int(5) +% %o +int(4) -- Iteration 8 -- 1 2 3 4 diff --git a/ext/standard/tests/strings/vprintf_variation11_64bit.phpt b/ext/standard/tests/strings/vprintf_variation11_64bit.phpt index 46800e820a..e183dcd838 100644 --- a/ext/standard/tests/strings/vprintf_variation11_64bit.phpt +++ b/ext/standard/tests/strings/vprintf_variation11_64bit.phpt @@ -26,7 +26,7 @@ $formats = array( "%10.4o %-10.4o %04o %04.4o", "%'#2o %'2o %'$2o %'_2o", "%o %o %o %o", - "%% %%o %10", + "%% %%o", '%3$o %4$o %1$o %2$o' ); @@ -39,7 +39,7 @@ $args_array = array( array(0123456, 01234567, -01234567, 01234567), array(0111, 02222, -0333333, -044444444), array(0x123b, 0xfAb, 0123, 012), - array(01234, 0567, -01234), + array(01234, 0567), array(03, 04, 01, 02) ); @@ -84,8 +84,8 @@ int(54) int(17) -- Iteration 7 -- -% %o -int(5) +% %o +int(4) -- Iteration 8 -- 1 2 3 4 diff --git a/ext/standard/tests/strings/vprintf_variation13.phpt b/ext/standard/tests/strings/vprintf_variation13.phpt index 25968538c8..39c6963689 100644 --- a/ext/standard/tests/strings/vprintf_variation13.phpt +++ b/ext/standard/tests/strings/vprintf_variation13.phpt @@ -26,7 +26,7 @@ $formats = array( "%10.4x %-10.4x %04x %04.4x", "%'#2x %'2x %'$2x %'_2x", "%x %x %x %x", - "% %%x x%", + "% %%x", '%3$x %4$x %1$x %2$x' ); @@ -56,7 +56,7 @@ foreach($formats as $format) { } ?> ---EXPECTF-- +--EXPECT-- *** Testing vprintf() : hexa formats with hexa values *** -- Iteration 1 -- @@ -84,8 +84,8 @@ int(22) int(12) -- Iteration 7 -- -%34 x -int(5) +%34 +int(3) -- Iteration 8 -- 1 2 3 4 diff --git a/ext/standard/tests/strings/vprintf_variation13_64bit.phpt b/ext/standard/tests/strings/vprintf_variation13_64bit.phpt index 261d10fe4e..c9af50fc94 100644 --- a/ext/standard/tests/strings/vprintf_variation13_64bit.phpt +++ b/ext/standard/tests/strings/vprintf_variation13_64bit.phpt @@ -26,7 +26,7 @@ $formats = array( "%10.4x %-10.4x %04x %04.4x", "%'#2x %'2x %'$2x %'_2x", "%x %x %x %x", - "% %%x x%", + "% %%x", '%3$x %4$x %1$x %2$x' ); @@ -39,7 +39,7 @@ $args_array = array( array(123456, 12345678, -1234567, 1234567), array(1, 0x2222, 0333333, -0x44444444), array(0x123b, 0xfAb, "0xaxz", 012), - array(0x1234, 0x34, 0x2ff), + array(0x1234, 0x34), array(0x3, 0x4, 0x1, 0x2) ); @@ -56,7 +56,7 @@ foreach($formats as $format) { } ?> ---EXPECTF-- +--EXPECT-- *** Testing vprintf() : hexa formats with hexa values *** -- Iteration 1 -- @@ -84,8 +84,8 @@ int(30) int(12) -- Iteration 7 -- -%34 x -int(5) +%34 +int(3) -- Iteration 8 -- 1 2 3 4 diff --git a/ext/standard/tests/strings/vprintf_variation3.phpt b/ext/standard/tests/strings/vprintf_variation3.phpt index c665eb024d..e3350e70d7 100644 --- a/ext/standard/tests/strings/vprintf_variation3.phpt +++ b/ext/standard/tests/strings/vprintf_variation3.phpt @@ -23,7 +23,7 @@ $formats = array( "%10.4d %-10.4d %04d %04.4d", "%'#2d %'2d %'$2d %'_2d", "%d %d %d %d", - "% %%d d%", + "% %%d", '%3$d %4$d %1$d %2$d' ); @@ -36,7 +36,7 @@ $args_array = array( array(123456, 12345678, -1234567, 1234567), array(111, 2222, 333333, 44444444), array(0x123b, 0xfAb, 0123, 012), - array(1234, -5678, 2345), + array(1234, -5678), array(3, 4, 1, 2) ); @@ -81,8 +81,8 @@ int(24) int(15) -- Iteration 7 -- -%-5678 d -int(8) +%-5678 +int(6) -- Iteration 8 -- 1 2 3 4 diff --git a/ext/standard/tests/strings/vprintf_variation5.phpt b/ext/standard/tests/strings/vprintf_variation5.phpt index 52d2424353..bdc41718f0 100644 --- a/ext/standard/tests/strings/vprintf_variation5.phpt +++ b/ext/standard/tests/strings/vprintf_variation5.phpt @@ -23,7 +23,7 @@ $formats = array( "%10.4f %-10.4F %04f %04.4f", "%'#2f %'2f %'$2f %'_2f", "%f %f %f %f", - "% %%f f%", + "% %%f", '%3$f %4$f %1$f %2$f' ); @@ -36,7 +36,7 @@ $args_array = array( array(2e5, 2e-5, -2e5, -2e-5), array(0.2E5, -0.2e40, 0.2E-20, 0.2E+20), array(0x123b, 0xfAb, 0123, 012), - array(1234.1234, -5678.5678, 2345.2345), + array(1234.1234, -5678.5678), array(3.33, 4.44, 1.11, 2.22) ); @@ -81,8 +81,8 @@ int(98) int(43) -- Iteration 7 -- -%-5678.567800 f -int(15) +%-5678.567800 +int(13) -- Iteration 8 -- 1.110000 2.220000 3.330000 4.440000 diff --git a/ext/standard/tests/strings/vprintf_variation7.phpt b/ext/standard/tests/strings/vprintf_variation7.phpt index e2af17feb9..6e66d9b182 100644 Binary files a/ext/standard/tests/strings/vprintf_variation7.phpt and b/ext/standard/tests/strings/vprintf_variation7.phpt differ diff --git a/ext/standard/tests/strings/vprintf_variation9.phpt b/ext/standard/tests/strings/vprintf_variation9.phpt index 83b6f21a4d..adc35c6b54 100644 Binary files a/ext/standard/tests/strings/vprintf_variation9.phpt and b/ext/standard/tests/strings/vprintf_variation9.phpt differ