1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 17:22:15 +01:00

Diagnose missing format specifier at end of string

This commit is contained in:
Nikita Popov
2020-04-22 12:53:34 +02:00
parent 572b799b52
commit 427cc4f496
10 changed files with 35 additions and 28 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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