Skip to content

Commit cbf86ef

Browse files
committed
Fix ZPP of v*printf()
1 parent 43095a5 commit cbf86ef

File tree

7 files changed

+66
-77
lines changed

7 files changed

+66
-77
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ PHP 8.0 UPGRADE NOTES
386386
. The length argument for array_splice() can now be null. Null values will
387387
behave identically to omitting the argument, thus removing everything from
388388
the 'offset' to the end of the array.
389+
. The args argument of vsprintf(), vfprintf(), and vprintf() must now be an
390+
array. Previously any type was accepted.
389391
. The 'salt' option of password_hash() is no longer supported. If the 'salt'
390392
option is used a warning is generated, the provided salt is ignored, and a
391393
generated salt is used instead.

ext/standard/basic_functions.stub.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -966,23 +966,18 @@ function sprintf(string $format, ...$args): string {}
966966
/** @param mixed ...$args */
967967
function printf(string $format, ...$args): int {}
968968

969-
/** @param mixed $args */
970-
function vprintf(string $format, $args): int {}
969+
function vprintf(string $format, array $args): int {}
971970

972-
/** @param mixed $args */
973-
function vsprintf(string $format, $args): string {}
971+
function vsprintf(string $format, array $args): string {}
974972

975973
/**
976974
* @param resource $handle
977975
* @param mixed ...$args
978976
*/
979977
function fprintf($handle, string $format, ...$args): int {}
980978

981-
/**
982-
* @param resource $handle
983-
* @param mixed $args
984-
*/
985-
function vfprintf($handle, string $format, $args): int {}
979+
/** @param resource $handle */
980+
function vfprintf($handle, string $format, array $args): int {}
986981

987982
/* fsock.c */
988983

ext/standard/basic_functions_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,12 +1480,12 @@ ZEND_END_ARG_INFO()
14801480

14811481
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_vprintf, 0, 2, IS_LONG, 0)
14821482
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
1483-
ZEND_ARG_INFO(0, args)
1483+
ZEND_ARG_TYPE_INFO(0, args, IS_ARRAY, 0)
14841484
ZEND_END_ARG_INFO()
14851485

14861486
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_vsprintf, 0, 2, IS_STRING, 0)
14871487
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
1488-
ZEND_ARG_INFO(0, args)
1488+
ZEND_ARG_TYPE_INFO(0, args, IS_ARRAY, 0)
14891489
ZEND_END_ARG_INFO()
14901490

14911491
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_fprintf, 0, 2, IS_LONG, 0)
@@ -1497,7 +1497,7 @@ ZEND_END_ARG_INFO()
14971497
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_vfprintf, 0, 3, IS_LONG, 0)
14981498
ZEND_ARG_INFO(0, handle)
14991499
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
1500-
ZEND_ARG_INFO(0, args)
1500+
ZEND_ARG_TYPE_INFO(0, args, IS_ARRAY, 0)
15011501
ZEND_END_ARG_INFO()
15021502

15031503
ZEND_BEGIN_ARG_INFO_EX(arginfo_fsockopen, 0, 0, 1)

ext/standard/formatted_print.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -651,20 +651,15 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
651651
/* }}} */
652652

653653
/* php_formatted_print_get_array() {{{ */
654-
static zval*
655-
php_formatted_print_get_array(zval *array, int *argc)
654+
static zval *php_formatted_print_get_array(zend_array *array, int *argc)
656655
{
657656
zval *args, *zv;
658657
int n;
659658

660-
if (Z_TYPE_P(array) != IS_ARRAY) {
661-
convert_to_array(array);
662-
}
663-
664-
n = zend_hash_num_elements(Z_ARRVAL_P(array));
659+
n = zend_hash_num_elements(array);
665660
args = (zval *)safe_emalloc(n, sizeof(zval), 0);
666661
n = 0;
667-
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(array), zv) {
662+
ZEND_HASH_FOREACH_VAL(array, zv) {
668663
ZVAL_COPY_VALUE(&args[n], zv);
669664
n++;
670665
} ZEND_HASH_FOREACH_END();
@@ -704,12 +699,13 @@ PHP_FUNCTION(vsprintf)
704699
zend_string *result;
705700
char *format;
706701
size_t format_len;
707-
zval *array, *args;
702+
zval *args;
703+
zend_array *array;
708704
int argc;
709705

710706
ZEND_PARSE_PARAMETERS_START(2, 2)
711707
Z_PARAM_STRING(format, format_len)
712-
Z_PARAM_ZVAL(array)
708+
Z_PARAM_ARRAY_HT(array)
713709
ZEND_PARSE_PARAMETERS_END();
714710

715711
args = php_formatted_print_get_array(array, &argc);
@@ -757,12 +753,13 @@ PHP_FUNCTION(vprintf)
757753
size_t rlen;
758754
char *format;
759755
size_t format_len;
760-
zval *array, *args;
756+
zval *args;
757+
zend_array *array;
761758
int argc;
762759

763760
ZEND_PARSE_PARAMETERS_START(2, 2)
764761
Z_PARAM_STRING(format, format_len)
765-
Z_PARAM_ZVAL(array)
762+
Z_PARAM_ARRAY_HT(array)
766763
ZEND_PARSE_PARAMETERS_END();
767764

768765
args = php_formatted_print_get_array(array, &argc);
@@ -820,7 +817,8 @@ PHP_FUNCTION(vfprintf)
820817
php_stream *stream;
821818
char *format;
822819
size_t format_len;
823-
zval *arg1, *array, *args;
820+
zval *arg1, *args;
821+
zend_array *array;
824822
int argc;
825823
zend_string *result;
826824

@@ -831,7 +829,7 @@ PHP_FUNCTION(vfprintf)
831829
ZEND_PARSE_PARAMETERS_START(3, 3)
832830
Z_PARAM_RESOURCE(arg1)
833831
Z_PARAM_STRING(format, format_len)
834-
Z_PARAM_ZVAL(array)
832+
Z_PARAM_ARRAY_HT(array)
835833
ZEND_PARSE_PARAMETERS_END();
836834

837835
php_stream_from_zval(stream, arg1);

ext/standard/tests/strings/vfprintf_error3.phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ try {
2424
}
2525

2626
try {
27-
var_dump( vfprintf( $fp, "Foo %y fake", "not available" ) );
27+
vfprintf($fp, "Foo: %s", "not available");
28+
} catch (TypeError $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
try {
33+
vfprintf($fp, "Foo %y fake", ["not available"]);
2834
} catch (ValueError $e) {
2935
echo $e->getMessage(), "\n";
3036
}
@@ -48,5 +54,6 @@ unlink( $file );
4854
--EXPECT--
4955
-- Testing vfprintf() function with wrong variable types as argument --
5056
vfprintf(): Argument #2 ($format) must be of type string, array given
57+
vfprintf(): Argument #3 ($args) must be of type array, string given
5158
Unknown format specifier 'y'
5259
string(0) ""

ext/standard/tests/strings/vfprintf_variation1.phpt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ class FooClass
3232
// Output facilitating function
3333
function writeAndDump($fp, $format, $args)
3434
{
35-
ftruncate( $fp, 0 );
36-
$length = vfprintf( $fp, $format, $args );
37-
rewind( $fp );
38-
$content = stream_get_contents( $fp );
39-
var_dump( $content );
40-
var_dump( $length );
35+
try {
36+
ftruncate( $fp, 0 );
37+
$length = vfprintf( $fp, $format, $args );
38+
rewind( $fp );
39+
$content = stream_get_contents( $fp );
40+
var_dump( $content );
41+
var_dump( $length );
42+
} catch (TypeError $exception) {
43+
echo $exception->getMessage() . "\n";
44+
}
4145
}
4246

4347
// Test vfprintf()
@@ -62,8 +66,7 @@ unlink( $file );
6266
?>
6367
--EXPECT--
6468
*** Testing vfprintf() : variation functionality ***
65-
string(6) "format"
66-
int(6)
69+
vfprintf(): Argument #3 ($args) must be of type array, null given
6770
string(17) "Foo is 30 and bar"
6871
int(17)
6972
string(14) "Foobar testing"

ext/standard/tests/strings/vprintf_variation2.phpt

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ foreach($values as $value) {
8888
$result = vprintf($format,$value);
8989
echo "\n";
9090
var_dump($result);
91+
} catch (\TypeError $e) {
92+
echo $e->getMessage(), "\n";
9193
} catch (\ValueError $e) {
9294
echo $e->getMessage(), "\n";
9395
}
@@ -98,92 +100,74 @@ foreach($values as $value) {
98100
fclose($file_handle);
99101

100102
?>
101-
--EXPECTF--
103+
--EXPECT--
102104
*** Testing vprintf() : with unexpected values for args argument ***
103105

104106
-- Iteration 1 --
105-
0
106-
int(1)
107+
vprintf(): Argument #2 ($args) must be of type array, int given
107108

108109
-- Iteration 2 --
109-
1
110-
int(1)
110+
vprintf(): Argument #2 ($args) must be of type array, int given
111111

112112
-- Iteration 3 --
113-
12345
114-
int(5)
113+
vprintf(): Argument #2 ($args) must be of type array, int given
115114

116115
-- Iteration 4 --
117-
-2345
118-
int(5)
116+
vprintf(): Argument #2 ($args) must be of type array, int given
119117

120118
-- Iteration 5 --
121-
10.5
122-
int(4)
119+
vprintf(): Argument #2 ($args) must be of type array, float given
123120

124121
-- Iteration 6 --
125-
-10.5
126-
int(5)
122+
vprintf(): Argument #2 ($args) must be of type array, float given
127123

128124
-- Iteration 7 --
129-
101234567000
130-
int(12)
125+
vprintf(): Argument #2 ($args) must be of type array, float given
131126

132127
-- Iteration 8 --
133-
1.07654321E-9
134-
int(13)
128+
vprintf(): Argument #2 ($args) must be of type array, float given
135129

136130
-- Iteration 9 --
137-
0.5
138-
int(3)
131+
vprintf(): Argument #2 ($args) must be of type array, float given
139132

140133
-- Iteration 10 --
141-
The arguments array must contain 1 items, 0 given
134+
vprintf(): Argument #2 ($args) must be of type array, null given
142135

143136
-- Iteration 11 --
144-
The arguments array must contain 1 items, 0 given
137+
vprintf(): Argument #2 ($args) must be of type array, null given
145138

146139
-- Iteration 12 --
147-
1
148-
int(1)
140+
vprintf(): Argument #2 ($args) must be of type array, bool given
149141

150142
-- Iteration 13 --
151-
152-
int(0)
143+
vprintf(): Argument #2 ($args) must be of type array, bool given
153144

154145
-- Iteration 14 --
155-
1
156-
int(1)
146+
vprintf(): Argument #2 ($args) must be of type array, bool given
157147

158148
-- Iteration 15 --
159-
160-
int(0)
149+
vprintf(): Argument #2 ($args) must be of type array, bool given
161150

162151
-- Iteration 16 --
163-
164-
int(0)
152+
vprintf(): Argument #2 ($args) must be of type array, string given
165153

166154
-- Iteration 17 --
167-
168-
int(0)
155+
vprintf(): Argument #2 ($args) must be of type array, string given
169156

170157
-- Iteration 18 --
171-
string
172-
int(6)
158+
vprintf(): Argument #2 ($args) must be of type array, string given
173159

174160
-- Iteration 19 --
175-
string
176-
int(6)
161+
vprintf(): Argument #2 ($args) must be of type array, string given
177162

178163
-- Iteration 20 --
179-
The arguments array must contain 1 items, 0 given
164+
vprintf(): Argument #2 ($args) must be of type array, object given
180165

181166
-- Iteration 21 --
182-
The arguments array must contain 1 items, 0 given
167+
vprintf(): Argument #2 ($args) must be of type array, null given
183168

184169
-- Iteration 22 --
185-
The arguments array must contain 1 items, 0 given
170+
vprintf(): Argument #2 ($args) must be of type array, null given
186171

187172
-- Iteration 23 --
188-
Resource id #%d
189-
int(%d)
173+
vprintf(): Argument #2 ($args) must be of type array, resource given

0 commit comments

Comments
 (0)