Skip to content

Commit 227988d

Browse files
committed
Make null byte error a ValueError
1 parent b411980 commit 227988d

16 files changed

+64
-50
lines changed

Zend/zend_API.c

+27-13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t n
254254
return;
255255
}
256256

257+
if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL)
258+
&& Z_TYPE_P(arg) == IS_STRING) {
259+
zend_argument_value_error(num, "must not contain any null bytes");
260+
return;
261+
}
262+
257263
zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_type_name(arg));
258264
}
259265
/* }}} */
@@ -668,10 +674,12 @@ static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec
668674
char **p = va_arg(*va, char **);
669675
size_t *pl = va_arg(*va, size_t *);
670676
if (!zend_parse_arg_path(arg, p, pl, check_null)) {
671-
zend_spprintf(error, 0, "a valid path%s, %s given",
672-
check_null ? " or null" : "", zend_zval_type_name(arg)
673-
);
674-
return "";
677+
if (p) {
678+
zend_spprintf(error, 0, "must not contain any null bytes");
679+
return "";
680+
} else {
681+
return check_null ? "?string" : "string";
682+
}
675683
}
676684
}
677685
break;
@@ -680,10 +688,12 @@ static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec
680688
{
681689
zend_string **str = va_arg(*va, zend_string **);
682690
if (!zend_parse_arg_path_str(arg, str, check_null)) {
683-
zend_spprintf(error, 0, "a valid path%s, %s given",
684-
check_null ? " or null" : "", zend_zval_type_name(arg)
685-
);
686-
return "";
691+
if (str) {
692+
zend_spprintf(error, 0, "must not contain any null bytes");
693+
return "";
694+
} else {
695+
return check_null ? "?string" : "string";
696+
}
687697
}
688698
}
689699
break;
@@ -762,7 +772,7 @@ static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec
762772
if (!zend_parse_arg_object(arg, p, ce, check_null)) {
763773
if (ce) {
764774
if (check_null) {
765-
zend_spprintf(error, 0, "of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
775+
zend_spprintf(error, 0, "must be of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
766776
return "";
767777
} else {
768778
return ZSTR_VAL(ce->name);
@@ -795,14 +805,14 @@ static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec
795805
}
796806
if (ce_base) {
797807
if ((!*pce || !instanceof_function(*pce, ce_base))) {
798-
zend_spprintf(error, 0, "a class name derived from %s%s, %s given",
808+
zend_spprintf(error, 0, "must be a class name derived from %s%s, %s given",
799809
ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
800810
*pce = NULL;
801811
return "";
802812
}
803813
}
804814
if (!*pce) {
805-
zend_spprintf(error, 0, "a valid class name%s, %s given",
815+
zend_spprintf(error, 0, "must be a valid class name%s, %s given",
806816
check_null ? " or null" : "", Z_STRVAL_P(arg));
807817
return "";
808818
}
@@ -833,7 +843,7 @@ static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec
833843
}
834844

835845
if (is_callable_error) {
836-
zend_spprintf(error, 0, "a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
846+
zend_spprintf(error, 0, "must be a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
837847
efree(is_callable_error);
838848
return "";
839849
} else {
@@ -874,7 +884,11 @@ static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, cons
874884
}
875885
if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
876886
if (error) {
877-
zend_argument_type_error(arg_num, "must be %s", error);
887+
if (strcmp(error, "must not contain any null bytes") == 0) {
888+
zend_argument_value_error(arg_num, "%s", error);
889+
} else {
890+
zend_argument_type_error(arg_num, "%s", error);
891+
}
878892
efree(error);
879893
} else {
880894
zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_type_name(arg));

Zend/zend_API.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,8 @@ static zend_always_inline zval *zend_try_array_init(zval *zv)
12151215
_(Z_EXPECTED_FUNC_OR_NULL, "a valid callback or null") \
12161216
_(Z_EXPECTED_RESOURCE, "of type resource") \
12171217
_(Z_EXPECTED_RESOURCE_OR_NULL, "of type resource or null") \
1218-
_(Z_EXPECTED_PATH, "a valid path") \
1219-
_(Z_EXPECTED_PATH_OR_NULL, "a valid path or null") \
1218+
_(Z_EXPECTED_PATH, "of type string") \
1219+
_(Z_EXPECTED_PATH_OR_NULL, "of type ?string") \
12201220
_(Z_EXPECTED_OBJECT, "of type object") \
12211221
_(Z_EXPECTED_OBJECT_OR_NULL, "of type ?object") \
12221222
_(Z_EXPECTED_DOUBLE, "of type float") \

ext/gd/tests/imagegd2_nullbyte_injection.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Testing null byte injection in imagegd2
99
$image = imagecreate(1,1);// 1px image
1010
try {
1111
imagegd($image, "./foo\0bar");
12-
} catch (TypeError $e) {
12+
} catch (ValueError $e) {
1313
echo $e->getMessage(), "\n";
1414
}
1515
?>
1616
--EXPECT--
17-
imagegd(): Argument #2 ($to) must be a valid path, string given
17+
imagegd(): Argument #2 ($to) must not contain any null bytes

ext/gd/tests/imagegd_nullbyte_injection.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Testing null byte injection in imagegd
99
$image = imagecreate(1,1);// 1px image
1010
try {
1111
imagegd($image, "./foo\0bar");
12-
} catch (TypeError $e) {
12+
} catch (ValueError $e) {
1313
echo $e->getMessage(), "\n";
1414
}
1515
?>
1616
--EXPECT--
17-
imagegd(): Argument #2 ($to) must be a valid path, string given
17+
imagegd(): Argument #2 ($to) must not contain any null bytes

ext/gd/tests/imagexbm_nullbyte_injection.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ if(!extension_loaded('gd')) die('skip gd extension not available');
99
$image = imagecreate(1,1);// 1px image
1010
try {
1111
imagexbm($image, "./foo\0bar");
12-
} catch (TypeError $e) {
12+
} catch (ValueError $e) {
1313
echo $e->getMessage(), "\n";
1414
}
1515
?>
1616
--EXPECT--
17-
imagexbm(): Argument #2 ($filename) must be a valid path or null, string given
17+
imagexbm(): Argument #2 ($filename) must not contain any null bytes

ext/standard/tests/file/bug39863.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ $filename = __FILE__ . chr(0). ".ridiculous";
99

1010
try {
1111
var_dump(file_exists($filename));
12-
} catch (TypeError $e) {
12+
} catch (ValueError $e) {
1313
echo $e->getMessage(), "\n";
1414
}
1515
?>
1616
--EXPECT--
17-
file_exists(): Argument #1 ($filename) must be a valid path, string given
17+
file_exists(): Argument #1 ($filename) cannot contain any null bytes

ext/standard/tests/file/file_get_contents_variation8.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ ValueError: Path cannot be empty
6363
Warning: file_get_contents( ): Failed to open stream: No such file or directory in %s on line %d
6464
bool(false)
6565
-- Iteration 6 --
66-
TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, string given
66+
ValueError: file_get_contents(): Argument #1 ($filename) must not contain any null bytes
6767
-- Iteration 7 --
68-
TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, array given
68+
TypeError: file_get_contents(): Argument #1 ($filename) must be of type string, array given
6969
-- Iteration 8 --
7070

7171
Warning: file_get_contents(/no/such/file/dir): Failed to open stream: No such file or directory in %s on line %d

ext/standard/tests/file/filegroup_variation3.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ foreach($files_arr as $file) {
3838
echo "- Iteration $count -\n";
3939
try {
4040
var_dump( filegroup( $file_path."/".$file ) );
41-
} catch (TypeError $e) {
41+
} catch (Error $e) {
4242
echo $e->getMessage(), "\n";
4343
}
4444
clearstatcache();
@@ -75,8 +75,8 @@ bool(false)
7575
Warning: filegroup(): stat failed for %s/filegroup_variation3/filegroup*.tmp in %s on line %d
7676
bool(false)
7777
- Iteration 7 -
78-
filegroup(): Argument #1 ($filename) must be a valid path, string given
78+
filegroup(): Argument #1 ($filename) must not contain any null bytes
7979
- Iteration 8 -
80-
filegroup(): Argument #1 ($filename) must be a valid path, string given
80+
filegroup(): Argument #1 ($filename) must not contain any null bytes
8181

8282
*** Done ***

ext/standard/tests/file/fileowner_variation3.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ foreach($files_arr as $file) {
3838
echo "- Iteration $count -\n";
3939
try {
4040
var_dump( fileowner( $file_path."/".$file ) );
41-
} catch (TypeError $e) {
41+
} catch (Error $e) {
4242
echo $e->getMessage(), "\n";
4343
}
4444
clearstatcache();
@@ -75,8 +75,8 @@ bool(false)
7575
Warning: fileowner(): stat failed for %s/fileowner_variation3/fileowner*.tmp in %s on line %d
7676
bool(false)
7777
- Iteration 7 -
78-
fileowner(): Argument #1 ($filename) must be a valid path, string given
78+
fileowner(): Argument #1 ($filename) must not contain any null bytes
7979
- Iteration 8 -
80-
fileowner(): Argument #1 ($filename) must be a valid path, string given
80+
fileowner(): Argument #1 ($filename) must not contain any null bytes
8181

8282
*** Done ***

ext/standard/tests/file/fileperms_variation3.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ foreach($files_arr as $file) {
3737
echo "- Iteration $count -\n";
3838
try {
3939
var_dump( fileperms( $file_path."/".$file ) );
40-
} catch (TypeError $e) {
40+
} catch (Error $e) {
4141
echo $e->getMessage(), "\n";
4242
}
4343
clearstatcache();
@@ -74,8 +74,8 @@ bool(false)
7474
Warning: fileperms(): stat failed for %s/fileperms_variation3/fileperms*.tmp in %s on line %d
7575
bool(false)
7676
- Iteration 7 -
77-
fileperms(): Argument #1 ($filename) must be a valid path, string given
77+
fileperms(): Argument #1 ($filename) must not contain any null bytes
7878
- Iteration 8 -
79-
fileperms(): Argument #1 ($filename) must be a valid path, string given
79+
fileperms(): Argument #1 ($filename) must not contain any null bytes
8080

8181
*** Done ***

ext/standard/tests/file/is_dir_variation4.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ foreach($dirs_arr as $dir) {
3535
echo "\n-- Iteration $count --\n";
3636
try {
3737
var_dump( is_dir($file_path."/".$dir ) );
38-
} catch (TypeError $e) {
38+
} catch (Error $e) {
3939
echo $e->getMessage(), "\n";
4040
}
4141
$count++;
@@ -76,9 +76,9 @@ bool(true)
7676
bool(false)
7777

7878
-- Iteration 9 --
79-
is_dir(): Argument #1 ($filename) must be a valid path, string given
79+
is_dir(): Argument #1 ($filename) must not contain any null bytes
8080

8181
-- Iteration 10 --
82-
is_dir(): Argument #1 ($filename) must be a valid path, string given
82+
is_dir(): Argument #1 ($filename) must not contain any null bytes
8383

8484
*** Done ***

ext/standard/tests/file/is_file_variation4.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ foreach($files_arr as $file) {
3535
echo "- Iteration $count -\n";
3636
try {
3737
var_dump( is_file( $file_path."/".$file ) );
38-
} catch (TypeError $e) {
38+
} catch (Error $e) {
3939
echo $e->getMessage(), "\n";
4040
}
4141
clearstatcache();
@@ -66,8 +66,8 @@ bool(false)
6666
- Iteration 6 -
6767
bool(false)
6868
- Iteration 7 -
69-
is_file(): Argument #1 ($filename) must be a valid path, string given
69+
is_file(): Argument #1 ($filename) must not contain any null bytes
7070
- Iteration 8 -
71-
is_file(): Argument #1 ($filename) must be a valid path, string given
71+
is_file(): Argument #1 ($filename) must not contain any null bytes
7272

7373
*** Done ***

ext/standard/tests/file/is_readable_variation1.phpt

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ foreach($files_arr as $file) {
4848
echo "-- Iteration $counter --\n";
4949
try {
5050
var_dump( is_readable($file) );
51-
} catch (TypeError $e) {
51+
} catch (Error $e) {
5252
echo $e->getMessage(), "\n";
5353
}
5454
$counter++;
@@ -77,11 +77,11 @@ bool(false)
7777
-- Iteration 6 --
7878
bool(false)
7979
-- Iteration 7 --
80-
is_readable(): Argument #1 ($filename) must be a valid path, string given
80+
is_readable(): Argument #1 ($filename) must not contain any null bytes
8181
-- Iteration 8 --
82-
is_readable(): Argument #1 ($filename) must be a valid path, string given
82+
is_readable(): Argument #1 ($filename) must not contain any null bytes
8383
-- Iteration 9 --
84-
is_readable(): Argument #1 ($filename) must be a valid path, string given
84+
is_readable(): Argument #1 ($filename) must not contain any null bytes
8585
-- Iteration 10 --
8686
bool(true)
8787
-- Iteration 11 --
-2 Bytes
Binary file not shown.

ext/standard/tests/file/tempnam_variation3.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for( $i=0; $i<count($names_arr); $i++ ) {
3737
echo "-- Iteration $i --\n";
3838
try {
3939
$file_name = tempnam("$file_path", $names_arr[$i]);
40-
} catch (TypeError $e) {
40+
} catch (Error $e) {
4141
echo $e->getMessage(), "\n";
4242
continue;
4343
}
@@ -102,9 +102,9 @@ File name is => %s/%s
102102
File permissions are => 100600
103103
File created in => directory specified
104104
-- Iteration 6 --
105-
tempnam(): Argument #2 ($prefix) must be a valid path, string given
105+
tempnam(): Argument #2 ($prefix) must not contain any null bytes
106106
-- Iteration 7 --
107-
tempnam(): Argument #2 ($prefix) must be a valid path, array given
107+
tempnam(): Argument #2 ($prefix) must be of type string, array given
108108
-- Iteration 8 --
109109
File name is => %s/dir%s
110110
File permissions are => 100600

ext/standard/tests/file/tempnam_variation7.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ for( $i=0; $i<count($names_arr); $i++ ) {
3535
echo "-- Iteration $i --\n";
3636
try {
3737
$file_name = tempnam($names_arr[$i], "tempnam_variation3.tmp");
38-
} catch (TypeError $e) {
38+
} catch (Error $e) {
3939
echo $e->getMessage(), "\n";
4040
continue;
4141
}
@@ -101,9 +101,9 @@ File name is => %s%etempnam_variation3.tmp%s
101101
File permissions are => 100600
102102
File created in => temp dir
103103
-- Iteration 6 --
104-
tempnam(): Argument #1 ($dir) must be a valid path, string given
104+
tempnam(): Argument #1 ($dir) must not contain any null bytes
105105
-- Iteration 7 --
106-
tempnam(): Argument #1 ($dir) must be a valid path, array given
106+
tempnam(): Argument #1 ($dir) must be of type string, array given
107107
-- Iteration 8 --
108108

109109
Notice: tempnam(): file created in the system's temporary directory in %stempnam_variation7.php on line %d

0 commit comments

Comments
 (0)