Skip to content

Commit 2126031

Browse files
committed
ext/ldap: Fix phpGH-16136 (Memory leak in php_ldap_do_modify())
1 parent c910e78 commit 2126031

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

ext/ldap/ldap.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,17 +2150,11 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
21502150
ldap_mods[i]->mod_type = estrndup(ZSTR_VAL(attribute), ZSTR_LEN(attribute));
21512151
} else {
21522152
php_error_docref(NULL, E_WARNING, "Unknown attribute in the data");
2153-
/* Free allocated memory */
2154-
while (i >= 0) {
2155-
if (ldap_mods[i]->mod_type) {
2156-
efree(ldap_mods[i]->mod_type);
2157-
}
2158-
efree(ldap_mods[i]);
2159-
i--;
2160-
}
2161-
efree(num_berval);
2162-
efree(ldap_mods);
2163-
RETURN_FALSE;
2153+
RETVAL_FALSE;
2154+
num_berval[i] = 0;
2155+
num_attribs = i + 1;
2156+
ldap_mods[i]->mod_bvalues = NULL;
2157+
goto cleanup;
21642158
}
21652159

21662160
value = zend_hash_get_current_data(Z_ARRVAL_P(entry));

ext/ldap/tests/gh16136.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug GH-16136: Memory leak in php_ldap_do_modify() when entry is not a proper dictionary
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
8+
/* ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() share an underlying C function */
9+
/* We are assuming 3333 is not connectable */
10+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
11+
$valid_dn = "cn=userA,something";
12+
13+
$not_dict_of_attributes = [
14+
'attribute1' => 'value',
15+
'not_key_entry',
16+
'attribute3' => [
17+
'value1',
18+
'value2',
19+
],
20+
];
21+
try {
22+
var_dump(ldap_add($ldap, $valid_dn, $not_dict_of_attributes));
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
?>
28+
--EXPECTF--
29+
Warning: ldap_add(): Unknown attribute in the data in %s on line %d
30+
bool(false)

0 commit comments

Comments
 (0)