Skip to content

Commit 6f50850

Browse files
committed
ext/ldap: Use zend_*_has_nul_byte() APIs
And also make them throw ValueErrors instead of TypeErrors
1 parent 30b44f5 commit 6f50850

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

ext/ldap/ldap.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,21 +2509,6 @@ static size_t _ldap_str_equal_to_const(const char *str, size_t str_len, const ch
25092509
}
25102510
/* }}} */
25112511

2512-
/* {{{ _ldap_strlen_max */
2513-
static size_t _ldap_strlen_max(const char *str, size_t max_len)
2514-
{
2515-
size_t i;
2516-
2517-
for (i = 0; i < max_len; ++i) {
2518-
if (str[i] == '\0') {
2519-
return i;
2520-
}
2521-
}
2522-
2523-
return max_len;
2524-
}
2525-
/* }}} */
2526-
25272512
/* {{{ _ldap_hash_fetch */
25282513
static void _ldap_hash_fetch(zval *hashTbl, const char *key, zval **out)
25292514
{
@@ -2588,8 +2573,8 @@ PHP_FUNCTION(ldap_modify_batch)
25882573
zend_ulong tmpUlong;
25892574

25902575
/* make sure the DN contains no NUL bytes */
2591-
if (_ldap_strlen_max(dn, dn_len) != dn_len) {
2592-
zend_argument_type_error(2, "must not contain null bytes");
2576+
if (zend_char_has_nul_byte(dn, dn_len)) {
2577+
zend_argument_value_error(2, "must not contain null bytes");
25932578
RETURN_THROWS();
25942579
}
25952580

@@ -2652,8 +2637,8 @@ PHP_FUNCTION(ldap_modify_batch)
26522637
RETURN_THROWS();
26532638
}
26542639

2655-
if (Z_STRLEN_P(modinfo) != _ldap_strlen_max(Z_STRVAL_P(modinfo), Z_STRLEN_P(modinfo))) {
2656-
zend_type_error("%s(): Option \"" LDAP_MODIFY_BATCH_ATTRIB "\" cannot contain null-bytes", get_active_function_name());
2640+
if (zend_str_has_nul_byte(Z_STR_P(modinfo))) {
2641+
zend_argument_value_error(3, "the value for option \"" LDAP_MODIFY_BATCH_ATTRIB "\" must not contain null bytes");
26572642
RETURN_THROWS();
26582643
}
26592644
}

ext/ldap/tests/ldap_modify_batch_programming_error.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,15 @@ try {
255255

256256
?>
257257
--EXPECT--
258-
TypeError: ldap_modify_batch(): Argument #2 ($dn) must not contain null bytes
258+
ValueError: ldap_modify_batch(): Argument #2 ($dn) must not contain null bytes
259259
TypeError: ldap_modify_batch(): Argument #3 ($modifications_info) must be integer-indexed
260260
TypeError: ldap_modify_batch(): Argument #3 ($modifications_info) must be integer-indexed
261261
ValueError: ldap_modify_batch(): Argument #3 ($modifications_info) must have consecutive integer indices starting from 0
262262
ValueError: ldap_modify_batch(): Argument #3 ($modifications_info) must only contain arrays
263263
TypeError: ldap_modify_batch(): Argument #3 ($modifications_info) must only contain string-indexed arrays
264264
ValueError: ldap_modify_batch(): Argument #3 ($modifications_info) must contain arrays only containing the "attrib", "modtype" and "values" keys
265265
TypeError: ldap_modify_batch(): Option "attrib" must be of type string, int given
266-
TypeError: ldap_modify_batch(): Option "attrib" cannot contain null-bytes
266+
ValueError: ldap_modify_batch(): Argument #3 ($modifications_info) the value for option "attrib" must not contain null bytes
267267
TypeError: ldap_modify_batch(): Option "modtype" must be of type int, stdClass given
268268
ValueError: ldap_modify_batch(): Option "modtype" must be one of the LDAP_MODIFY_BATCH_* constants
269269
ValueError: ldap_modify_batch(): If option "modtype" is LDAP_MODIFY_BATCH_REMOVE_ALL, option "values" cannot be provided

0 commit comments

Comments
 (0)