Skip to content
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

Change E_WARNING to ValueError in sprintf functions #4837

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions ext/standard/formatted_print.c
Expand Up @@ -450,7 +450,7 @@ php_formatted_print(zval *z_format, zval *args, int argc)

if (argnum <= 0) {
zend_string_efree(result);
php_error_docref(NULL, E_WARNING, "Argument number must be greater than zero");
zend_throw_error(NULL, "Argument number must be greater than zero");
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
argnum--;
Expand Down Expand Up @@ -491,7 +491,7 @@ php_formatted_print(zval *z_format, zval *args, int argc)
PRINTF_DEBUG(("sprintf: getting width\n"));
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
efree(result);
php_error_docref(NULL, E_WARNING, "Width must be greater than zero and less than %d", INT_MAX);
zend_throw_error(NULL, "Width must be greater than zero and less than %d", INT_MAX);
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
adjusting |= ADJ_WIDTH;
Expand All @@ -508,7 +508,7 @@ php_formatted_print(zval *z_format, zval *args, int argc)
if (isdigit((int)*format)) {
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
efree(result);
php_error_docref(NULL, E_WARNING, "Precision must be greater than zero and less than %d", INT_MAX);
zend_throw_error(NULL, "Precision must be greater than zero and less than %d", INT_MAX);
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
adjusting |= ADJ_PRECISION;
Expand All @@ -524,7 +524,7 @@ php_formatted_print(zval *z_format, zval *args, int argc)

if (argnum >= argc) {
efree(result);
php_error_docref(NULL, E_WARNING, "Too few arguments");
zend_throw_error(NULL, "Too few arguments");
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}

Expand Down
7 changes: 5 additions & 2 deletions ext/standard/tests/file/fscanf_variation14.phpt
Expand Up @@ -76,8 +76,11 @@ $counter = 1;

// writing to the file
foreach($valid_strings as $string) {
@fprintf($file_handle, $string);
@fprintf($file_handle, "\n");
try {
fprintf($file_handle, $string);
} catch (\Error $e) {
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
}
fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
Expand Down
10 changes: 6 additions & 4 deletions ext/standard/tests/strings/printf_64bit.phpt
Expand Up @@ -39,7 +39,11 @@ echo "\n*** Output for insufficient number of arguments ***\n";
$string = "dingy%sflem%dwombat";
$nbr = 5;
$name = "voudras";
printf("%d $string %s", $nbr, $name);
try {
printf("%d $string %s", $nbr, $name);
} catch (\Error $e) {
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
print('Error found: '.$e->getMessage());
}


/* Scalar argument */
Expand Down Expand Up @@ -233,9 +237,7 @@ printf("%d", $tempstring);
printf() expects at least 1 parameter, 0 given

*** Output for insufficient number of arguments ***

Warning: printf(): Too few arguments in %s on line %d

Error found: Too few arguments
*** Output for scalar argument ***
3
*** Output for NULL as argument ***
Expand Down
60 changes: 36 additions & 24 deletions ext/standard/tests/strings/printf_error.phpt
Expand Up @@ -25,16 +25,40 @@ $arg1 = 'one';
$arg2 = 'two';

echo "\n-- Call printf with one argument less than expected --\n";
var_dump( printf($format1) );
var_dump( printf($format2,$arg1) );
var_dump( printf($format3,$arg1,$arg2) );
try {
var_dump( printf($format1) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( printf($format2,$arg1) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( printf($format3,$arg1,$arg2) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}

echo "\n-- Call printf with two argument less than expected --\n";
var_dump( printf($format2) );
var_dump( printf($format3,$arg1) );
try {
var_dump( printf($format2) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( printf($format3,$arg1) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}

echo "\n-- Call printf with three argument less than expected --\n";
var_dump( printf($format3) );
try {
var_dump( printf($format3) );
} catch (\Error $e) {
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
echo $e->getMessage(), "\n";
}

?>
===DONE===
Expand All @@ -47,26 +71,14 @@ printf() expects at least 1 parameter, 0 given
-- Testing printf() function with less than expected no. of arguments --

-- Call printf with one argument less than expected --

Warning: printf(): Too few arguments in %s on line %d
bool(false)

Warning: printf(): Too few arguments in %s on line %d
bool(false)

Warning: printf(): Too few arguments in %s on line %d
bool(false)
Too few arguments
Too few arguments
Too few arguments

-- Call printf with two argument less than expected --

Warning: printf(): Too few arguments in %s on line %d
bool(false)

Warning: printf(): Too few arguments in %s on line %d
bool(false)
Too few arguments
Too few arguments

-- Call printf with three argument less than expected --

Warning: printf(): Too few arguments in %s on line %d
bool(false)
Too few arguments
===DONE===
60 changes: 36 additions & 24 deletions ext/standard/tests/strings/sprintf_error.phpt
Expand Up @@ -25,16 +25,40 @@ $arg1 = 'one';
$arg2 = 'two';

// with one argument less than expected
var_dump( sprintf($format1) );
var_dump( sprintf($format2,$arg1) );
var_dump( sprintf($format3,$arg1,$arg2) );
try {
var_dump( sprintf($format1) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( sprintf($format2,$arg1) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( sprintf($format3,$arg1,$arg2) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}

// with two argument less than expected
var_dump( sprintf($format2) );
var_dump( sprintf($format3,$arg1) );
try {
var_dump( sprintf($format2) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( sprintf($format3,$arg1) );
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}

// with three argument less than expected
var_dump( sprintf($format3) );
try {
var_dump( sprintf($format3) );
} catch (\Error $e) {
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
echo $e->getMessage(), "\n";
}

echo "Done";
?>
Expand All @@ -45,22 +69,10 @@ echo "Done";
sprintf() expects at least %d parameter, %d given

-- Testing sprintf() function with less than expected no. of arguments --

Warning: sprintf(): Too few arguments in %s on line %d
bool(false)

Warning: sprintf(): Too few arguments in %s on line %d
bool(false)

Warning: sprintf(): Too few arguments in %s on line %d
bool(false)

Warning: sprintf(): Too few arguments in %s on line %d
bool(false)

Warning: sprintf(): Too few arguments in %s on line %d
bool(false)

Warning: sprintf(): Too few arguments in %s on line %d
bool(false)
Too few arguments
Too few arguments
Too few arguments
Too few arguments
Too few arguments
Too few arguments
Done
11 changes: 6 additions & 5 deletions ext/standard/tests/strings/vfprintf_error4.phpt
Expand Up @@ -22,8 +22,11 @@ try {
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
var_dump( vfprintf( $fp, 'Foo %$c-0202Sd', array( 2 ) ) );

try {
var_dump( vfprintf( $fp, 'Foo %$c-0202Sd', array( 2 ) ) );
} catch(\Error $e) {
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
print('Error found: '.$e->getMessage().".\n");
}
// Close handle
fclose( $fp );

Expand All @@ -39,7 +42,5 @@ unlink( $file );
--EXPECTF--
-- Testing vfprintf() function with other strangeties --
vfprintf() expects parameter 1 to be resource, string given

Warning: vfprintf(): Argument number must be greater than zero in %s on line %d
bool(false)
Error found: Argument number must be greater than zero.
===DONE===
21 changes: 10 additions & 11 deletions ext/standard/tests/strings/vfprintf_variation21.phpt
Expand Up @@ -90,7 +90,11 @@ fprintf($fp, "\n*** Testing vprintf() with unexpected values for args argument *
$counter = 1;
foreach( $values as $value ) {
fprintf($fp, "\n-- Iteration %d --\n",$counter);
vfprintf($fp, $format, $value);
try {
vfprintf($fp, $format, $value);
} catch (\Error $e) {
moufmouf marked this conversation as resolved.
Show resolved Hide resolved
fwrite($fp, $e->getMessage() . "\n");
}
$counter++;
}

Expand All @@ -106,16 +110,6 @@ unlink($data_file);
--EXPECTF--
*** Testing vfprintf() : with unexpected values for args argument ***

Warning: vfprintf(): Too few arguments in %s on line %d

Warning: vfprintf(): Too few arguments in %s on line %d

Warning: vfprintf(): Too few arguments in %s on line %d

Warning: vfprintf(): Too few arguments in %s on line %d

Warning: vfprintf(): Too few arguments in %s on line %d

*** Testing vprintf() with unexpected values for args argument ***

-- Iteration 1 --
Expand All @@ -137,8 +131,10 @@ Warning: vfprintf(): Too few arguments in %s on line %d
-- Iteration 9 --
0.5
-- Iteration 10 --
Too few arguments

-- Iteration 11 --
Too few arguments

-- Iteration 12 --
1
Expand All @@ -157,10 +153,13 @@ string
-- Iteration 19 --
string
-- Iteration 20 --
Too few arguments

-- Iteration 21 --
Too few arguments

-- Iteration 22 --
Too few arguments

-- Iteration 23 --
Resource id #%d
Expand Down
35 changes: 12 additions & 23 deletions ext/standard/tests/strings/vprintf_variation2.phpt
Expand Up @@ -84,9 +84,13 @@ $values = array(
$counter = 1;
foreach($values as $value) {
echo "\n-- Iteration $counter --\n";
$result = vprintf($format,$value);
echo "\n";
var_dump($result);
try {
$result = vprintf($format,$value);
echo "\n";
var_dump($result);
} catch (\Error $e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please catch \ValueError when applying the other changes.

echo $e->getMessage(), "\n";
}
$counter++;
};

Expand Down Expand Up @@ -135,16 +139,10 @@ int(13)
int(3)

-- Iteration 10 --

Warning: vprintf(): Too few arguments in %s on line %d

bool(false)
Too few arguments

-- Iteration 11 --

Warning: vprintf(): Too few arguments in %s on line %d

bool(false)
Too few arguments

-- Iteration 12 --
1
Expand Down Expand Up @@ -179,22 +177,13 @@ string
int(6)

-- Iteration 20 --

Warning: vprintf(): Too few arguments in %s on line %d

bool(false)
Too few arguments

-- Iteration 21 --

Warning: vprintf(): Too few arguments in %s on line %d

bool(false)
Too few arguments

-- Iteration 22 --

Warning: vprintf(): Too few arguments in %s on line %d

bool(false)
Too few arguments

-- Iteration 23 --
Resource id #%d
Expand Down