Skip to content

Commit 3e800e9

Browse files
committed
Move custom type checks to ZPP
Closes GH-6034
1 parent ddc2a2d commit 3e800e9

19 files changed

+117
-122
lines changed

Zend/zend_operators.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(zval *op) /* {{{ */
816816
} else {
817817
/* Previously we used strtol here, not is_numeric_string,
818818
* and strtol gives you LONG_MAX/_MIN on overflow.
819-
* We use use saturating conversion to emulate strtol()'s
819+
* We use saturating conversion to emulate strtol()'s
820820
* behaviour.
821821
*/
822822
return zend_dval_to_lval_cap(dval);

ext/mbstring/mbstring.c

+12-18
Original file line numberDiff line numberDiff line change
@@ -3513,8 +3513,8 @@ PHP_FUNCTION(mb_send_mail)
35133513
size_t message_len;
35143514
char *subject;
35153515
size_t subject_len;
3516-
zval *headers = NULL;
35173516
zend_string *extra_cmd = NULL;
3517+
HashTable *headers_ht = NULL;
35183518
zend_string *str_headers = NULL, *tmp_headers;
35193519
size_t n, i;
35203520
char *to_r = NULL;
@@ -3560,30 +3560,24 @@ PHP_FUNCTION(mb_send_mail)
35603560
Z_PARAM_STRING(subject, subject_len)
35613561
Z_PARAM_STRING(message, message_len)
35623562
Z_PARAM_OPTIONAL
3563-
Z_PARAM_ZVAL(headers)
3564-
Z_PARAM_STR(extra_cmd)
3563+
Z_PARAM_STR_OR_ARRAY_HT_OR_NULL(str_headers, headers_ht)
3564+
Z_PARAM_STR_OR_NULL(extra_cmd)
35653565
ZEND_PARSE_PARAMETERS_END();
35663566

35673567
/* ASCIIZ check */
35683568
MAIL_ASCIIZ_CHECK_MBSTRING(to, to_len);
35693569
MAIL_ASCIIZ_CHECK_MBSTRING(subject, subject_len);
35703570
MAIL_ASCIIZ_CHECK_MBSTRING(message, message_len);
3571-
if (headers) {
3572-
switch(Z_TYPE_P(headers)) {
3573-
case IS_STRING:
3574-
tmp_headers = zend_string_init(Z_STRVAL_P(headers), Z_STRLEN_P(headers), 0);
3575-
MAIL_ASCIIZ_CHECK_MBSTRING(ZSTR_VAL(tmp_headers), ZSTR_LEN(tmp_headers));
3576-
str_headers = php_trim(tmp_headers, NULL, 0, 2);
3577-
zend_string_release_ex(tmp_headers, 0);
3578-
break;
3579-
case IS_ARRAY:
3580-
str_headers = php_mail_build_headers(Z_ARRVAL_P(headers));
3581-
break;
3582-
default:
3583-
zend_argument_value_error(4, "must be of type string|array|null, %s given", zend_zval_type_name(headers));
3584-
RETURN_THROWS();
3585-
}
3571+
3572+
if (str_headers) {
3573+
tmp_headers = zend_string_init(ZSTR_VAL(str_headers), ZSTR_LEN(str_headers), 0);
3574+
MAIL_ASCIIZ_CHECK_MBSTRING(ZSTR_VAL(tmp_headers), ZSTR_LEN(tmp_headers));
3575+
str_headers = php_trim(tmp_headers, NULL, 0, 2);
3576+
zend_string_release_ex(tmp_headers, 0);
3577+
} else if (headers_ht) {
3578+
str_headers = php_mail_build_headers(headers_ht);
35863579
}
3580+
35873581
if (extra_cmd) {
35883582
MAIL_ASCIIZ_CHECK_MBSTRING(ZSTR_VAL(extra_cmd), ZSTR_LEN(extra_cmd));
35893583
}

ext/mbstring/mbstring.stub.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ function mb_encode_numericentity(string $string, array $convmap, ?string $encodi
7777

7878
function mb_decode_numericentity(string $string, array $convmap, ?string $encoding = null): string {}
7979

80-
/** @param string|array|null $additional_headers */
81-
function mb_send_mail(string $to, string $subject, string $message, $additional_headers = null, ?string $additional_parameters = null): bool {}
80+
function mb_send_mail(string $to, string $subject, string $message, array|string|null $additional_headers = null, ?string $additional_parameters = null): bool {}
8281

8382
function mb_get_info(string $type = "all"): array|string|int|false {}
8483

ext/mbstring/mbstring_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 5ad8a8cf20eeae59713d19135ecccbee243754eb */
2+
* Stub hash: 84096daa0fd395f57401f11e9e79f7c8420e8a93 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_language, 0, 0, MAY_BE_STRING|MAY_BE_BOOL)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, language, IS_STRING, 1, "null")
@@ -178,7 +178,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_send_mail, 0, 3, _IS_BOOL, 0)
178178
ZEND_ARG_TYPE_INFO(0, to, IS_STRING, 0)
179179
ZEND_ARG_TYPE_INFO(0, subject, IS_STRING, 0)
180180
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
181-
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, additional_headers, "null")
181+
ZEND_ARG_TYPE_MASK(0, additional_headers, MAY_BE_ARRAY|MAY_BE_STRING|MAY_BE_NULL, "null")
182182
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, additional_parameters, IS_STRING, 1, "null")
183183
ZEND_END_ARG_INFO()
184184

ext/oci8/oci8.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ function oci_fetch_all($statement_resource, &$output, int $skip = 0, int $maximu
266266

267267
/**
268268
* @param resource $statement_resource
269-
* @param mixed $output
269+
* @param array $output
270270
* @alias oci_fetch_all
271271
* @deprecated
272272
*/

ext/oci8/oci8_arginfo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a5a245b354b48a56c274c8f74c974d92ec430853 */
2+
* Stub hash: 447880a4bc4add36beab835cc07c09a254dc0c2b */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_oci_define_by_name, 0, 3, _IS_BOOL, 0)
55
ZEND_ARG_INFO(0, statement_resource)

ext/odbc/odbc.stub.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ function odbc_fetch_into($result_id, &$result_array, int $rownumber = 0): int|fa
5757
/** @param resource $result_id */
5858
function odbc_fetch_row($result_id, int $row_number = UNKNOWN): bool {}
5959

60-
/**
61-
* @param resource $result_id
62-
* @param string|int $field
63-
*/
64-
function odbc_result($result_id, $field): string|bool|null {}
60+
/** @param resource $result_id */
61+
function odbc_result($result_id, string|int $field): string|bool|null {}
6562

6663
/** @param resource $result_id */
6764
function odbc_result_all($result_id, string $format = ''): int|false {}

ext/odbc/odbc_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 14702a5bd87902871d456de2289f4ae236e5bfa5 */
2+
* Stub hash: cf17952d8c3b88f218bbb8d1c21ba40079574c04 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_odbc_close_all, 0, 0, IS_VOID, 0)
55
ZEND_END_ARG_INFO()
@@ -70,7 +70,7 @@ ZEND_END_ARG_INFO()
7070

7171
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_odbc_result, 0, 2, MAY_BE_STRING|MAY_BE_BOOL|MAY_BE_NULL)
7272
ZEND_ARG_INFO(0, result_id)
73-
ZEND_ARG_INFO(0, field)
73+
ZEND_ARG_TYPE_MASK(0, field, MAY_BE_STRING|MAY_BE_LONG, NULL)
7474
ZEND_END_ARG_INFO()
7575

7676
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_odbc_result_all, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)

ext/odbc/php_odbc.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -1736,31 +1736,31 @@ PHP_FUNCTION(odbc_fetch_row)
17361736
PHP_FUNCTION(odbc_result)
17371737
{
17381738
char *field;
1739-
zend_string *field_str;
1739+
zend_string *field_str, *pv_field_str;
1740+
zend_long pv_field_long;
17401741
int field_ind;
17411742
SQLSMALLINT sql_c_type = SQL_C_CHAR;
17421743
odbc_result *result;
17431744
int i = 0;
17441745
RETCODE rc;
17451746
SQLLEN fieldsize;
1746-
zval *pv_res, *pv_field;
1747+
zval *pv_res;
17471748
#ifdef HAVE_SQL_EXTENDED_FETCH
17481749
SQLULEN crow;
17491750
SQLUSMALLINT RowStatus[1];
17501751
#endif
17511752

1752-
field_ind = -1;
1753-
field = NULL;
1754-
1755-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_field) == FAILURE) {
1756-
RETURN_THROWS();
1757-
}
1753+
ZEND_PARSE_PARAMETERS_START(2, 2)
1754+
Z_PARAM_RESOURCE(pv_res)
1755+
Z_PARAM_STR_OR_LONG(pv_field_str, pv_field_long)
1756+
ZEND_PARSE_PARAMETERS_END();
17581757

1759-
if (Z_TYPE_P(pv_field) == IS_STRING) {
1760-
field = Z_STRVAL_P(pv_field);
1758+
if (pv_field_str) {
1759+
field = ZSTR_VAL(pv_field_str);
1760+
field_ind = -1;
17611761
} else {
1762-
convert_to_long_ex(pv_field);
1763-
field_ind = Z_LVAL_P(pv_field) - 1;
1762+
field = NULL;
1763+
field_ind = (int) pv_field_long - 1;
17641764
}
17651765

17661766
if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {

ext/xml/tests/bug72714.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ parse(3015809298423721);
2828
parse(20);
2929
?>
3030
--EXPECTF--
31-
Notice: xml_parser_set_option(): tagstart ignored, because it is out of range in %s%ebug72714.php on line %d
31+
Warning: xml_parser_set_option(): tagstart ignored, because it is out of range in %s on line %d
3232
string(9) "NS1:TOTAL"
3333
string(0) ""

ext/xml/tests/xml_parser_set_option_variation3.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ $values = array(
7777
// loop through each element of the array for value
7878

7979
foreach($values as $value) {
80-
echo @"\nArg value $value \n";
81-
var_dump( xml_parser_set_option($parser, $option, $value) );
82-
};
80+
echo @"\nArg value $value \n";
81+
var_dump(xml_parser_set_option($parser, $option, $value));
82+
}
8383

8484
fclose($fp);
8585
xml_parser_free($parser);

ext/xml/xml.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ PHP_FUNCTION(xml_parser_set_option)
14271427
case PHP_XML_OPTION_SKIP_TAGSTART:
14281428
parser->toffset = zval_get_long(val);
14291429
if (parser->toffset < 0) {
1430-
php_error_docref(NULL, E_NOTICE, "tagstart ignored, because it is out of range");
1430+
php_error_docref(NULL, E_WARNING, "tagstart ignored, because it is out of range");
14311431
parser->toffset = 0;
14321432
}
14331433
break;
@@ -1445,6 +1445,7 @@ PHP_FUNCTION(xml_parser_set_option)
14451445
zend_argument_value_error(3, "is not a supported target encoding");
14461446
RETURN_THROWS();
14471447
}
1448+
14481449
parser->target_encoding = enc->name;
14491450
break;
14501451
}

ext/xsl/php_xsl.stub.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ public function transformToUri(object $document, string $uri) {}
2525
*/
2626
public function transformToXml(object $document) {}
2727

28-
/**
29-
* @param string|array $name
30-
* @return bool
31-
*/
32-
public function setParameter(string $namespace, $name, string $value = UNKNOWN) {}
28+
/** @return bool */
29+
public function setParameter(string $namespace, array|string $name, ?string $value = null) {}
3330

3431
/** @return string|false */
3532
public function getParameter(string $namespace, string $name) {}
@@ -40,11 +37,8 @@ public function removeParameter(string $namespace, string $name) {}
4037
/** @return bool */
4138
public function hasExsltSupport() {}
4239

43-
/**
44-
* @param string|array|null $restrict
45-
* @return void
46-
*/
47-
public function registerPHPFunctions($restrict = null) {}
40+
/** @return void */
41+
public function registerPHPFunctions(array|string|null $restrict = null) {}
4842

4943
/** @return bool */
5044
public function setProfiling(?string $filename) {}

ext/xsl/php_xsl_arginfo.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 13fd80938fec3bea7ac4bbcfb6e0b69b230fba72 */
2+
* Stub hash: 4a3997bafb6c17714ee94443837be2d2842386e2 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_XSLTProcessor_importStylesheet, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, stylesheet, IS_OBJECT, 0)
@@ -21,8 +21,8 @@ ZEND_END_ARG_INFO()
2121

2222
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_XSLTProcessor_setParameter, 0, 0, 2)
2323
ZEND_ARG_TYPE_INFO(0, namespace, IS_STRING, 0)
24-
ZEND_ARG_INFO(0, name)
25-
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
24+
ZEND_ARG_TYPE_MASK(0, name, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
25+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 1, "null")
2626
ZEND_END_ARG_INFO()
2727

2828
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_XSLTProcessor_getParameter, 0, 0, 2)
@@ -36,7 +36,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_XSLTProcessor_hasExsltSupport, 0, 0, 0)
3636
ZEND_END_ARG_INFO()
3737

3838
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_XSLTProcessor_registerPHPFunctions, 0, 0, 0)
39-
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, restrict, "null")
39+
ZEND_ARG_TYPE_MASK(0, restrict, MAY_BE_ARRAY|MAY_BE_STRING|MAY_BE_NULL, "null")
4040
ZEND_END_ARG_INFO()
4141

4242
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_XSLTProcessor_setProfiling, 0, 0, 1)

ext/xsl/tests/xsltprocessor_setparameter-nostring.phpt

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ Check xsltprocessor::setparameter error handling with no-string
44
Memleak: http://bugs.php.net/bug.php?id=48221
55
--SKIPIF--
66
<?php
7-
if (!extension_loaded('xsl')) {
8-
die("skip\n");
9-
}
7+
if (!extension_loaded('xsl')) {
8+
die("skip\n");
9+
}
1010
?>
1111
--FILE--
1212
<?php
1313
include __DIR__ .'/prepare.inc';
1414
$proc->importStylesheet($xsl);
15-
var_dump($proc->setParameter('', array(4, 'abc')));
15+
try {
16+
$proc->setParameter('', array(4, 'abc'));
17+
} catch (TypeError $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
1620
$proc->transformToXml($dom);
1721
?>
18-
--EXPECTF--
19-
Warning: XSLTProcessor::setParameter(): Invalid parameter array in %s on line %d
20-
bool(false)
22+
--EXPECT--
23+
XSLTProcessor::setParameter(): Argument #2 ($name) must contain only string keys
2124
--CREDITS--
2225
Christian Weiske, cweiske@php.net
2326
PHP Testfest Berlin 2009-05-09

ext/xsl/xsltprocessor.c

+26-13
Original file line numberDiff line numberDiff line change
@@ -666,21 +666,35 @@ PHP_METHOD(XSLTProcessor, setParameter)
666666
{
667667

668668
zval *id = ZEND_THIS;
669-
zval *array_value, *entry, new_string;
669+
zval *entry, new_string;
670+
HashTable *array_value;
670671
xsl_object *intern;
671672
char *namespace;
672673
size_t namespace_len;
673-
zend_string *string_key, *name, *value;
674+
zend_string *string_key, *name, *value = NULL;
675+
676+
ZEND_PARSE_PARAMETERS_START(2, 3)
677+
Z_PARAM_STRING(namespace, namespace_len)
678+
Z_PARAM_STR_OR_ARRAY_HT(name, array_value)
679+
Z_PARAM_OPTIONAL
680+
Z_PARAM_STR_OR_NULL(value)
681+
ZEND_PARSE_PARAMETERS_END();
682+
683+
intern = Z_XSL_P(id);
684+
685+
if (array_value) {
686+
if (value) {
687+
zend_argument_value_error(3, "must be null when argument #2 ($name) is an array");
688+
RETURN_THROWS();
689+
}
674690

675-
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "sa", &namespace, &namespace_len, &array_value) == SUCCESS) {
676-
intern = Z_XSL_P(id);
677-
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(array_value), string_key, entry) {
691+
ZEND_HASH_FOREACH_STR_KEY_VAL(array_value, string_key, entry) {
678692
zval tmp;
679693
zend_string *str;
680694

681695
if (string_key == NULL) {
682-
php_error_docref(NULL, E_WARNING, "Invalid parameter array");
683-
RETURN_FALSE;
696+
zend_argument_type_error(2, "must contain only string keys");
697+
RETURN_THROWS();
684698
}
685699
str = zval_try_get_string(entry);
686700
if (UNEXPECTED(!str)) {
@@ -690,18 +704,17 @@ PHP_METHOD(XSLTProcessor, setParameter)
690704
zend_hash_update(intern->parameter, string_key, &tmp);
691705
} ZEND_HASH_FOREACH_END();
692706
RETURN_TRUE;
693-
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "sSS", &namespace, &namespace_len, &name, &value) == SUCCESS) {
694-
695-
intern = Z_XSL_P(id);
707+
} else {
708+
if (!value) {
709+
zend_argument_value_error(3, "cannot be null when argument #2 ($name) is a string");
710+
RETURN_THROWS();
711+
}
696712

697713
ZVAL_STR_COPY(&new_string, value);
698714

699715
zend_hash_update(intern->parameter, name, &new_string);
700716
RETURN_TRUE;
701-
} else {
702-
WRONG_PARAM_COUNT;
703717
}
704-
705718
}
706719
/* }}} end XSLTProcessor::setParameter */
707720

0 commit comments

Comments
 (0)