Skip to content

Commit 8b2570e

Browse files
committed
Promote warnings to exceptions in *scanf() functions
1 parent 9563449 commit 8b2570e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+926
-1890
lines changed

ext/standard/scanf.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
387387
gotSequential = 1;
388388
if (gotXpg) {
389389
mixedXPG:
390-
php_error_docref(NULL, E_WARNING, "%s", "cannot mix \"%\" and \"%n$\" conversion specifiers");
390+
zend_value_error("%s", "cannot mix \"%\" and \"%n$\" conversion specifiers");
391391
goto error;
392392
}
393393

@@ -469,11 +469,11 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
469469
}
470470
break;
471471
badSet:
472-
php_error_docref(NULL, E_WARNING, "Unmatched [ in format string");
472+
zend_value_error("Unmatched [ in format string");
473473
goto error;
474474

475475
default: {
476-
php_error_docref(NULL, E_WARNING, "Bad scan conversion character \"%c\"", *ch);
476+
zend_value_error("Bad scan conversion character \"%c\"", *ch);
477477
goto error;
478478
}
479479
}
@@ -523,14 +523,14 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
523523
}
524524
for (i = 0; i < numVars; i++) {
525525
if (nassign[i] > 1) {
526-
php_error_docref(NULL, E_WARNING, "%s", "Variable is assigned by multiple \"%n$\" conversion specifiers");
526+
zend_value_error("%s", "Variable is assigned by multiple \"%n$\" conversion specifiers");
527527
goto error;
528528
} else if (!xpgSize && (nassign[i] == 0)) {
529529
/*
530530
* If the space is empty, and xpgSize is 0 (means XPG wasn't
531531
* used, and/or numVars != 0), then too many vars were given
532532
*/
533-
php_error_docref(NULL, E_WARNING, "Variable is not assigned by any conversion specifiers");
533+
zend_value_error("Variable is not assigned by any conversion specifiers");
534534
goto error;
535535
}
536536
}
@@ -542,9 +542,9 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
542542

543543
badIndex:
544544
if (gotXpg) {
545-
php_error_docref(NULL, E_WARNING, "%s", "\"%n$\" argument index out of range");
545+
zend_value_error("%s", "\"%n$\" argument index out of range");
546546
} else {
547-
php_error_docref(NULL, E_WARNING, "Different numbers of variable names and field specifiers");
547+
zend_value_error("Different numbers of variable names and field specifiers");
548548
}
549549

550550
error:
@@ -598,10 +598,6 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
598598
numVars = 0;
599599
}
600600

601-
#if 0
602-
zend_printf("<br>in sscanf_internal : <br> string is \"%s\", format = \"%s\"<br> NumVars = %d. VarStart = %d<br>-------------------------<br>",
603-
string, format, numVars, varStart);
604-
#endif
605601
/*
606602
* Check for errors in the format string.
607603
*/
@@ -617,11 +613,7 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
617613
*/
618614
if (numVars) {
619615
for (i = varStart;i < argCount;i++){
620-
if ( ! Z_ISREF(args[ i ] ) ) {
621-
php_error_docref(NULL, E_WARNING, "Parameter %d must be passed by reference", i);
622-
scan_set_error_return(numVars, return_value);
623-
return SCAN_ERROR_VAR_PASSED_BYVAL;
624-
}
616+
ZEND_ASSERT(Z_ISREF(args[i]) && "Parameter must be passed by reference");
625617
}
626618
}
627619

ext/standard/scanf.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828
/* can be caused by bad parameters or format*/
2929
/* string. */
3030
#define SCAN_ERROR_INVALID_FORMAT (SCAN_ERROR_EOF - 1)
31-
#define SCAN_ERROR_VAR_PASSED_BYVAL (SCAN_ERROR_INVALID_FORMAT - 1)
32-
#define SCAN_ERROR_WRONG_PARAM_COUNT (SCAN_ERROR_VAR_PASSED_BYVAL - 1)
33-
#define SCAN_ERROR_INTERNAL (SCAN_ERROR_WRONG_PARAM_COUNT - 1)
34-
31+
#define SCAN_ERROR_WRONG_PARAM_COUNT (SCAN_ERROR_INVALID_FORMAT - 1)
3532

3633
/*
3734
* The following are here solely for the benefit of the scanf type functions

ext/standard/tests/file/fscanf.phpt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,35 @@ var_dump($v);
1818
fclose($fp);
1919

2020
$fp = fopen($filename, "rt");
21-
var_dump(fscanf($fp, "%s", $v, $v1));
21+
try {
22+
fscanf($fp, "%s", $v, $v1);
23+
} catch (ValueError $exception) {
24+
echo $exception->getMessage() . "\n";
25+
}
2226
var_dump($v);
2327
var_dump($v1);
2428
fclose($fp);
2529

2630
$v = array();
2731
$v1 = array();
2832
$fp = fopen($filename, "rt");
29-
var_dump(fscanf($fp, "", $v, $v1));
33+
try {
34+
fscanf($fp, "", $v, $v1);
35+
} catch (ValueError $exception) {
36+
echo $exception->getMessage() . "\n";
37+
}
3038
var_dump($v);
3139
var_dump($v1);
3240
fclose($fp);
3341

3442
$v = array();
3543
$v1 = array();
3644
$fp = fopen($filename, "rt");
37-
var_dump(fscanf($fp, "%.a", $v, $v1));
45+
try {
46+
fscanf($fp, "%.a", $v, $v1);
47+
} catch (ValueError $exception) {
48+
echo $exception->getMessage() . "\n";
49+
}
3850
var_dump($v);
3951
var_dump($v1);
4052
fclose($fp);
@@ -50,7 +62,11 @@ fclose($fp);
5062
file_put_contents($filename, "data");
5163

5264
$fp = fopen($filename, "rt");
53-
var_dump(fscanf($fp, "%s%d", $v));
65+
try {
66+
var_dump(fscanf($fp, "%s%d", $v));
67+
} catch (ValueError $exception) {
68+
echo $exception->getMessage() . "\n";
69+
}
5470

5571
echo "Done\n";
5672
?>
@@ -64,29 +80,21 @@ int(0)
6480
NULL
6581
int(1)
6682
string(4) "data"
67-
68-
Warning: fscanf(): Variable is not assigned by any conversion specifiers in %s on line %d
69-
int(-1)
83+
Variable is not assigned by any conversion specifiers
7084
string(4) "data"
7185
NULL
72-
73-
Warning: fscanf(): Variable is not assigned by any conversion specifiers in %s on line %d
74-
int(-1)
86+
Variable is not assigned by any conversion specifiers
7587
array(0) {
7688
}
7789
array(0) {
7890
}
79-
80-
Warning: fscanf(): Bad scan conversion character "." in %s on line %d
81-
int(-1)
91+
Bad scan conversion character "."
8292
array(0) {
8393
}
8494
array(0) {
8595
}
8696
bool(false)
8797
array(0) {
8898
}
89-
90-
Warning: fscanf(): Different numbers of variable names and field specifiers in %s on line %d
91-
int(-1)
99+
Different numbers of variable names and field specifiers
92100
Done

ext/standard/tests/file/fscanf_error.phpt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fclose($file_handle);
1919

2020
// invalid file handle
2121
try {
22-
var_dump( fscanf($file_handle, "%s") );
22+
fscanf($file_handle, "%s");
2323
} catch (TypeError $e) {
2424
echo $e->getMessage(), "\n";
2525
}
@@ -28,7 +28,11 @@ try {
2828
$file_handle = fopen($filename, 'r');
2929
if ($file_handle == false)
3030
exit("Error:failed to open file $filename");
31-
var_dump( fscanf($file_handle, "%d%s%f", $int_var, $string_var) );
31+
try {
32+
fscanf($file_handle, "%d%s%f", $int_var, $string_var);
33+
} catch (ValueError $exception) {
34+
echo $exception->getMessage() . "\n";
35+
}
3236
fclose($file_handle);
3337

3438
// different invalid format strings
@@ -42,7 +46,11 @@ foreach($invalid_formats as $format) {
4246
$file_handle = fopen($filename, 'r');
4347
if ($file_handle == false)
4448
exit("Error:failed to open file $filename");
45-
var_dump( fscanf($file_handle, $format) );
49+
try {
50+
var_dump(fscanf($file_handle, $format));
51+
} catch (ValueError $exception) {
52+
echo $exception->getMessage() . "\n";
53+
}
4654
fclose($file_handle);
4755
}
4856

@@ -57,24 +65,14 @@ unlink($filename);
5765
--EXPECTF--
5866
*** Testing fscanf() for error conditions ***
5967
fscanf(): supplied resource is not a valid File-Handle resource
60-
61-
Warning: fscanf(): Different numbers of variable names and field specifiers in %s on line %d
62-
int(-1)
68+
Different numbers of variable names and field specifiers
6369

6470
Warning: Undefined variable: undefined_var in %s on line %d
6571
array(0) {
6672
}
67-
68-
Warning: fscanf(): Bad scan conversion character " in %s on line %d
69-
NULL
70-
71-
Warning: fscanf(): Bad scan conversion character " in %s on line %d
72-
NULL
73-
74-
Warning: fscanf(): Bad scan conversion character "." in %s on line %d
75-
NULL
76-
77-
Warning: fscanf(): Bad scan conversion character "m" in %s on line %d
78-
NULL
73+
Bad scan conversion character "
74+
Bad scan conversion character "
75+
Bad scan conversion character "."
76+
Bad scan conversion character "m"
7977

8078
*** Done ***

ext/standard/tests/file/fscanf_variation10.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ foreach($float_formats as $float_format) {
6161
rewind($file_handle);
6262
echo "\n-- iteration $counter --\n";
6363
while( !feof($file_handle) ) {
64-
var_dump( fscanf($file_handle,$float_format) );
64+
try {
65+
var_dump( fscanf($file_handle,$float_format) );
66+
} catch (ValueError $exception) {
67+
echo $exception->getMessage() . "\n";
68+
}
6569
}
6670
$counter++;
6771
}
@@ -148,12 +152,8 @@ array(1) {
148152
bool(false)
149153

150154
-- iteration 7 --
151-
152-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
153-
NULL
154-
155-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
156-
NULL
155+
Bad scan conversion character " "
156+
Bad scan conversion character " "
157157
bool(false)
158158

159159
-- iteration 8 --

ext/standard/tests/file/fscanf_variation11.phpt

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ foreach($float_formats as $float_format) {
6666
rewind($file_handle);
6767
echo "\n-- iteration $counter --\n";
6868
while( !feof($file_handle) ) {
69-
var_dump( fscanf($file_handle,$float_format) );
69+
try {
70+
var_dump(fscanf($file_handle,$float_format));
71+
} catch (ValueError $exception) {
72+
echo $exception->getMessage() . "\n";
73+
}
7074
}
7175
$counter++;
7276
}
@@ -79,7 +83,7 @@ $file_path = __DIR__;
7983
$filename = "$file_path/fscanf_variation11.tmp";
8084
unlink($filename);
8185
?>
82-
--EXPECTF--
86+
--EXPECT--
8387
*** Test fscanf(): different float format types with arrays ***
8488

8589
-- iteration 1 --
@@ -389,42 +393,18 @@ array(1) {
389393
bool(false)
390394

391395
-- iteration 7 --
392-
393-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
394-
NULL
395-
396-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
397-
NULL
398-
399-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
400-
NULL
401-
402-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
403-
NULL
404-
405-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
406-
NULL
407-
408-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
409-
NULL
410-
411-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
412-
NULL
413-
414-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
415-
NULL
416-
417-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
418-
NULL
419-
420-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
421-
NULL
422-
423-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
424-
NULL
425-
426-
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
427-
NULL
396+
Bad scan conversion character " "
397+
Bad scan conversion character " "
398+
Bad scan conversion character " "
399+
Bad scan conversion character " "
400+
Bad scan conversion character " "
401+
Bad scan conversion character " "
402+
Bad scan conversion character " "
403+
Bad scan conversion character " "
404+
Bad scan conversion character " "
405+
Bad scan conversion character " "
406+
Bad scan conversion character " "
407+
Bad scan conversion character " "
428408
bool(false)
429409

430410
-- iteration 8 --

0 commit comments

Comments
 (0)