Skip to content

Promote warnings to exceptions in *scanf() functions #4993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions ext/standard/scanf.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
gotSequential = 1;
if (gotXpg) {
mixedXPG:
php_error_docref(NULL, E_WARNING, "%s", "cannot mix \"%\" and \"%n$\" conversion specifiers");
zend_value_error("%s", "cannot mix \"%\" and \"%n$\" conversion specifiers");
goto error;
}

Expand Down Expand Up @@ -469,11 +469,11 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
}
break;
badSet:
php_error_docref(NULL, E_WARNING, "Unmatched [ in format string");
zend_value_error("Unmatched [ in format string");
goto error;

default: {
php_error_docref(NULL, E_WARNING, "Bad scan conversion character \"%c\"", *ch);
zend_value_error("Bad scan conversion character \"%c\"", *ch);
goto error;
}
}
Expand Down Expand Up @@ -523,14 +523,14 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)
}
for (i = 0; i < numVars; i++) {
if (nassign[i] > 1) {
php_error_docref(NULL, E_WARNING, "%s", "Variable is assigned by multiple \"%n$\" conversion specifiers");
zend_value_error("%s", "Variable is assigned by multiple \"%n$\" conversion specifiers");
goto error;
} else if (!xpgSize && (nassign[i] == 0)) {
/*
* If the space is empty, and xpgSize is 0 (means XPG wasn't
* used, and/or numVars != 0), then too many vars were given
*/
php_error_docref(NULL, E_WARNING, "Variable is not assigned by any conversion specifiers");
zend_value_error("Variable is not assigned by any conversion specifiers");
goto error;
}
}
Expand All @@ -542,9 +542,9 @@ PHPAPI int ValidateFormat(char *format, int numVars, int *totalSubs)

badIndex:
if (gotXpg) {
php_error_docref(NULL, E_WARNING, "%s", "\"%n$\" argument index out of range");
zend_value_error("%s", "\"%n$\" argument index out of range");
} else {
php_error_docref(NULL, E_WARNING, "Different numbers of variable names and field specifiers");
zend_value_error("Different numbers of variable names and field specifiers");
}

error:
Expand Down Expand Up @@ -598,10 +598,6 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
numVars = 0;
}

#if 0
zend_printf("<br>in sscanf_internal : <br> string is \"%s\", format = \"%s\"<br> NumVars = %d. VarStart = %d<br>-------------------------<br>",
string, format, numVars, varStart);
#endif
/*
* Check for errors in the format string.
*/
Expand All @@ -617,11 +613,7 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
*/
if (numVars) {
for (i = varStart;i < argCount;i++){
if ( ! Z_ISREF(args[ i ] ) ) {
php_error_docref(NULL, E_WARNING, "Parameter %d must be passed by reference", i);
scan_set_error_return(numVars, return_value);
return SCAN_ERROR_VAR_PASSED_BYVAL;
}
ZEND_ASSERT(Z_ISREF(args[i]) && "Parameter must be passed by reference");
}
}

Expand Down
5 changes: 1 addition & 4 deletions ext/standard/scanf.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
/* can be caused by bad parameters or format*/
/* string. */
#define SCAN_ERROR_INVALID_FORMAT (SCAN_ERROR_EOF - 1)
#define SCAN_ERROR_VAR_PASSED_BYVAL (SCAN_ERROR_INVALID_FORMAT - 1)
#define SCAN_ERROR_WRONG_PARAM_COUNT (SCAN_ERROR_VAR_PASSED_BYVAL - 1)
#define SCAN_ERROR_INTERNAL (SCAN_ERROR_WRONG_PARAM_COUNT - 1)

#define SCAN_ERROR_WRONG_PARAM_COUNT (SCAN_ERROR_INVALID_FORMAT - 1)

/*
* The following are here solely for the benefit of the scanf type functions
Expand Down
40 changes: 24 additions & 16 deletions ext/standard/tests/file/fscanf.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,35 @@ var_dump($v);
fclose($fp);

$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "%s", $v, $v1));
try {
fscanf($fp, "%s", $v, $v1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
var_dump($v);
var_dump($v1);
fclose($fp);

$v = array();
$v1 = array();
$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "", $v, $v1));
try {
fscanf($fp, "", $v, $v1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
var_dump($v);
var_dump($v1);
fclose($fp);

$v = array();
$v1 = array();
$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "%.a", $v, $v1));
try {
fscanf($fp, "%.a", $v, $v1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
var_dump($v);
var_dump($v1);
fclose($fp);
Expand All @@ -50,7 +62,11 @@ fclose($fp);
file_put_contents($filename, "data");

$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "%s%d", $v));
try {
var_dump(fscanf($fp, "%s%d", $v));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

echo "Done\n";
?>
Expand All @@ -64,29 +80,21 @@ int(0)
NULL
int(1)
string(4) "data"

Warning: fscanf(): Variable is not assigned by any conversion specifiers in %s on line %d
int(-1)
Variable is not assigned by any conversion specifiers
string(4) "data"
NULL

Warning: fscanf(): Variable is not assigned by any conversion specifiers in %s on line %d
int(-1)
Variable is not assigned by any conversion specifiers
array(0) {
}
array(0) {
}

Warning: fscanf(): Bad scan conversion character "." in %s on line %d
int(-1)
Bad scan conversion character "."
array(0) {
}
array(0) {
}
bool(false)
array(0) {
}

Warning: fscanf(): Different numbers of variable names and field specifiers in %s on line %d
int(-1)
Different numbers of variable names and field specifiers
Done
34 changes: 16 additions & 18 deletions ext/standard/tests/file/fscanf_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fclose($file_handle);

// invalid file handle
try {
var_dump( fscanf($file_handle, "%s") );
fscanf($file_handle, "%s");
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
Expand All @@ -28,7 +28,11 @@ try {
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
exit("Error:failed to open file $filename");
var_dump( fscanf($file_handle, "%d%s%f", $int_var, $string_var) );
try {
fscanf($file_handle, "%d%s%f", $int_var, $string_var);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
fclose($file_handle);

// different invalid format strings
Expand All @@ -42,7 +46,11 @@ foreach($invalid_formats as $format) {
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
exit("Error:failed to open file $filename");
var_dump( fscanf($file_handle, $format) );
try {
var_dump(fscanf($file_handle, $format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
fclose($file_handle);
}

Expand All @@ -57,24 +65,14 @@ unlink($filename);
--EXPECTF--
*** Testing fscanf() for error conditions ***
fscanf(): supplied resource is not a valid File-Handle resource

Warning: fscanf(): Different numbers of variable names and field specifiers in %s on line %d
int(-1)
Different numbers of variable names and field specifiers

Warning: Undefined variable: undefined_var in %s on line %d
array(0) {
}

Warning: fscanf(): Bad scan conversion character " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character "." in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character "m" in %s on line %d
NULL
Bad scan conversion character "
Bad scan conversion character "
Bad scan conversion character "."
Bad scan conversion character "m"

*** Done ***
14 changes: 7 additions & 7 deletions ext/standard/tests/file/fscanf_variation10.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ foreach($float_formats as $float_format) {
rewind($file_handle);
echo "\n-- iteration $counter --\n";
while( !feof($file_handle) ) {
var_dump( fscanf($file_handle,$float_format) );
try {
var_dump( fscanf($file_handle,$float_format) );
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
}
$counter++;
}
Expand Down Expand Up @@ -148,12 +152,8 @@ array(1) {
bool(false)

-- iteration 7 --

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL
Bad scan conversion character " "
Bad scan conversion character " "
bool(false)

-- iteration 8 --
Expand Down
56 changes: 18 additions & 38 deletions ext/standard/tests/file/fscanf_variation11.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ foreach($float_formats as $float_format) {
rewind($file_handle);
echo "\n-- iteration $counter --\n";
while( !feof($file_handle) ) {
var_dump( fscanf($file_handle,$float_format) );
try {
var_dump(fscanf($file_handle,$float_format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
}
$counter++;
}
Expand All @@ -79,7 +83,7 @@ $file_path = __DIR__;
$filename = "$file_path/fscanf_variation11.tmp";
unlink($filename);
?>
--EXPECTF--
--EXPECT--
*** Test fscanf(): different float format types with arrays ***

-- iteration 1 --
Expand Down Expand Up @@ -389,42 +393,18 @@ array(1) {
bool(false)

-- iteration 7 --

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL

Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
Bad scan conversion character " "
bool(false)

-- iteration 8 --
Expand Down
Loading