Skip to content

Commit b2dc833

Browse files
committed
Only accept string as the format parameter of *printf() functions
1 parent a74484c commit b2dc833

24 files changed

+174
-246
lines changed

ext/standard/formatted_print.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -392,22 +392,15 @@ php_sprintf_getnumber(char **buffer, size_t *len)
392392
* - 0 or more: ArgumentCountError is thrown
393393
*/
394394
static zend_string *
395-
php_formatted_print(zval *z_format, zval *args, int argc, int nb_additional_parameters)
395+
php_formatted_print(char *format, size_t format_len, zval *args, int argc, int nb_additional_parameters)
396396
{
397397
size_t size = 240, outpos = 0;
398398
int alignment, currarg, adjusting, argnum, width, precision;
399-
char *format, *temppos, padding;
399+
char *temppos, padding;
400400
zend_string *result;
401401
int always_sign;
402-
size_t format_len;
403402
int bad_arg_number = 0;
404403

405-
if (!try_convert_to_string(z_format)) {
406-
return NULL;
407-
}
408-
409-
format = Z_STRVAL_P(z_format);
410-
format_len = Z_STRLEN_P(z_format);
411404
result = zend_string_alloc(size, 0);
412405

413406
currarg = 0;
@@ -680,15 +673,17 @@ php_formatted_print_get_array(zval *array, int *argc)
680673
PHP_FUNCTION(user_sprintf)
681674
{
682675
zend_string *result;
683-
zval *format, *args;
676+
char *format;
677+
size_t format_len;
678+
zval *args;
684679
int argc;
685680

686681
ZEND_PARSE_PARAMETERS_START(1, -1)
687-
Z_PARAM_ZVAL(format)
682+
Z_PARAM_STRING(format, format_len)
688683
Z_PARAM_VARIADIC('*', args, argc)
689684
ZEND_PARSE_PARAMETERS_END();
690685

691-
result = php_formatted_print(format, args, argc, 1);
686+
result = php_formatted_print(format, format_len, args, argc, 1);
692687
if (result == NULL) {
693688
return;
694689
}
@@ -701,17 +696,19 @@ PHP_FUNCTION(user_sprintf)
701696
PHP_FUNCTION(vsprintf)
702697
{
703698
zend_string *result;
704-
zval *format, *array, *args;
699+
char *format;
700+
size_t format_len;
701+
zval *array, *args;
705702
int argc;
706703

707704
ZEND_PARSE_PARAMETERS_START(2, 2)
708-
Z_PARAM_ZVAL(format)
705+
Z_PARAM_STRING(format, format_len)
709706
Z_PARAM_ZVAL(array)
710707
ZEND_PARSE_PARAMETERS_END();
711708

712709
args = php_formatted_print_get_array(array, &argc);
713710

714-
result = php_formatted_print(format, args, argc, -1);
711+
result = php_formatted_print(format, format_len, args, argc, -1);
715712
efree(args);
716713
if (result == NULL) {
717714
return;
@@ -726,15 +723,17 @@ PHP_FUNCTION(user_printf)
726723
{
727724
zend_string *result;
728725
size_t rlen;
729-
zval *format, *args;
726+
char *format;
727+
size_t format_len;
728+
zval *args;
730729
int argc;
731730

732731
ZEND_PARSE_PARAMETERS_START(1, -1)
733-
Z_PARAM_ZVAL(format)
732+
Z_PARAM_STRING(format, format_len)
734733
Z_PARAM_VARIADIC('*', args, argc)
735734
ZEND_PARSE_PARAMETERS_END();
736735

737-
result = php_formatted_print(format, args, argc, 1);
736+
result = php_formatted_print(format, format_len, args, argc, 1);
738737
if (result == NULL) {
739738
return;
740739
}
@@ -750,17 +749,19 @@ PHP_FUNCTION(vprintf)
750749
{
751750
zend_string *result;
752751
size_t rlen;
753-
zval *format, *array, *args;
752+
char *format;
753+
size_t format_len;
754+
zval *array, *args;
754755
int argc;
755756

756757
ZEND_PARSE_PARAMETERS_START(2, 2)
757-
Z_PARAM_ZVAL(format)
758+
Z_PARAM_STRING(format, format_len)
758759
Z_PARAM_ZVAL(array)
759760
ZEND_PARSE_PARAMETERS_END();
760761

761762
args = php_formatted_print_get_array(array, &argc);
762763

763-
result = php_formatted_print(format, args, argc, -1);
764+
result = php_formatted_print(format, format_len, args, argc, -1);
764765
efree(args);
765766
if (result == NULL) {
766767
return;
@@ -776,7 +777,9 @@ PHP_FUNCTION(vprintf)
776777
PHP_FUNCTION(fprintf)
777778
{
778779
php_stream *stream;
779-
zval *arg1, *format, *args;
780+
char *format;
781+
size_t format_len;
782+
zval *arg1, *args;
780783
int argc;
781784
zend_string *result;
782785

@@ -786,13 +789,13 @@ PHP_FUNCTION(fprintf)
786789

787790
ZEND_PARSE_PARAMETERS_START(2, -1)
788791
Z_PARAM_RESOURCE(arg1)
789-
Z_PARAM_ZVAL(format)
792+
Z_PARAM_STRING(format, format_len)
790793
Z_PARAM_VARIADIC('*', args, argc)
791794
ZEND_PARSE_PARAMETERS_END();
792795

793796
php_stream_from_zval(stream, arg1);
794797

795-
result = php_formatted_print(format, args, argc, 2);
798+
result = php_formatted_print(format, format_len, args, argc, 2);
796799
if (result == NULL) {
797800
return;
798801
}
@@ -809,7 +812,9 @@ PHP_FUNCTION(fprintf)
809812
PHP_FUNCTION(vfprintf)
810813
{
811814
php_stream *stream;
812-
zval *arg1, *format, *array, *args;
815+
char *format;
816+
size_t format_len;
817+
zval *arg1, *array, *args;
813818
int argc;
814819
zend_string *result;
815820

@@ -819,15 +824,15 @@ PHP_FUNCTION(vfprintf)
819824

820825
ZEND_PARSE_PARAMETERS_START(3, 3)
821826
Z_PARAM_RESOURCE(arg1)
822-
Z_PARAM_ZVAL(format)
827+
Z_PARAM_STRING(format, format_len)
823828
Z_PARAM_ZVAL(array)
824829
ZEND_PARSE_PARAMETERS_END();
825830

826831
php_stream_from_zval(stream, arg1);
827832

828833
args = php_formatted_print_get_array(array, &argc);
829834

830-
result = php_formatted_print(format, args, argc, -1);
835+
result = php_formatted_print(format, format_len, args, argc, -1);
831836
efree(args);
832837
if (result == NULL) {
833838
return;

ext/standard/tests/file/fscanf_variation10.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $counter = 1;
4242

4343
// writing to the file
4444
foreach($resource_types as $value) {
45-
@fprintf($file_handle, $value);
45+
@fprintf($file_handle, "%s", $value);
4646
@fprintf($file_handle, "\n");
4747
}
4848
// closing the file

ext/standard/tests/file/fscanf_variation11.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $counter = 1;
4747

4848
// writing to the file
4949
foreach($array_types as $value) {
50-
@fprintf($file_handle, $value);
50+
@fprintf($file_handle, "%s", $value);
5151
@fprintf($file_handle, "\n");
5252
}
5353
// closing the file

ext/standard/tests/file/fscanf_variation16.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $counter = 1;
4141

4242
// writing to the file
4343
foreach($resource_types as $value) {
44-
@fprintf($file_handle, $value);
44+
@fprintf($file_handle, "%s", $value);
4545
@fprintf($file_handle, "\n");
4646
}
4747
// closing the file

ext/standard/tests/file/fscanf_variation17.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $counter = 1;
4646

4747
// writing to the file
4848
foreach($array_types as $value) {
49-
@fprintf($file_handle, $value);
49+
@fprintf($file_handle, "%s", $value);
5050
@fprintf($file_handle, "\n");
5151
}
5252
// closing the file

ext/standard/tests/file/fscanf_variation22.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $counter = 1;
4141

4242
// writing to the file
4343
foreach($resource_types as $value) {
44-
@fprintf($file_handle, $value);
44+
@fprintf($file_handle, "%s", $value);
4545
@fprintf($file_handle, "\n");
4646
}
4747
// closing the file

ext/standard/tests/file/fscanf_variation23.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $counter = 1;
4646

4747
// writing to the file
4848
foreach($array_types as $value) {
49-
@fprintf($file_handle, $value);
49+
@fprintf($file_handle, "%s", $value);
5050
@fprintf($file_handle, "\n");
5151
}
5252
// closing the file

ext/standard/tests/file/fscanf_variation29.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $counter = 1;
4242

4343
// writing to the file
4444
foreach($resource_types as $value) {
45-
@fprintf($file_handle, $value);
45+
@fprintf($file_handle, "%s", $value);
4646
@fprintf($file_handle, "\n");
4747
}
4848
// closing the file

ext/standard/tests/file/fscanf_variation30.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $counter = 1;
4747

4848
// writing to the file
4949
foreach($array_types as $value) {
50-
@fprintf($file_handle, $value);
50+
@fprintf($file_handle, "%s", $value);
5151
@fprintf($file_handle, "\n");
5252
}
5353
// closing the file

ext/standard/tests/file/fscanf_variation35.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $counter = 1;
3737

3838
// writing to the file
3939
foreach($resource_types as $value) {
40-
@fprintf($file_handle, $value);
40+
@fprintf($file_handle, "%s", $value);
4141
@fprintf($file_handle, "\n");
4242
}
4343
// closing the file

ext/standard/tests/file/fscanf_variation36.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $counter = 1;
4242

4343
// writing to the file
4444
foreach($array_types as $value) {
45-
@fprintf($file_handle, $value);
45+
@fprintf($file_handle, "%s", $value);
4646
@fprintf($file_handle, "\n");
4747
}
4848
// closing the file

ext/standard/tests/file/fscanf_variation4.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $counter = 1;
3838

3939
// writing to the file
4040
foreach($resource_types as $value) {
41-
@fprintf($file_handle, $value);
41+
@fprintf($file_handle, "%s", $value);
4242
@fprintf($file_handle, "\n");
4343
}
4444
// closing the file

ext/standard/tests/file/fscanf_variation41.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $counter = 1;
3737

3838
// writing to the file
3939
foreach($resource_types as $value) {
40-
@fprintf($file_handle, $value);
40+
@fprintf($file_handle, "%s", $value);
4141
@fprintf($file_handle, "\n");
4242
}
4343
// closing the file

ext/standard/tests/file/fscanf_variation42.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $counter = 1;
4242

4343
// writing to the file
4444
foreach($array_types as $value) {
45-
@fprintf($file_handle, $value);
45+
@fprintf($file_handle, "%s", $value);
4646
@fprintf($file_handle, "\n");
4747
}
4848
// closing the file

ext/standard/tests/file/fscanf_variation47.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $counter = 1;
3737

3838
// writing to the file
3939
foreach($resource_types as $value) {
40-
@fprintf($file_handle, $value);
40+
@fprintf($file_handle, "%s", $value);
4141
@fprintf($file_handle, "\n");
4242
}
4343
// closing the file

ext/standard/tests/file/fscanf_variation48.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $counter = 1;
4242

4343
// writing to the file
4444
foreach($array_types as $value) {
45-
@fprintf($file_handle, $value);
45+
@fprintf($file_handle, "%s", $value);
4646
@fprintf($file_handle, "\n");
4747
}
4848
// closing the file

ext/standard/tests/file/fscanf_variation5.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $counter = 1;
4242

4343
// writing to the file
4444
foreach($array_types as $value) {
45-
@fprintf($file_handle, $value);
45+
@fprintf($file_handle, "%s", $value);
4646
@fprintf($file_handle, "\n");
4747
}
4848
// closing the file

0 commit comments

Comments
 (0)