Skip to content

Commit 3435d5a

Browse files
committed
ext/ldap: Fix GH-16136 (Memory leak in php_ldap_do_modify())
1 parent e68df0e commit 3435d5a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ext/ldap/ldap.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,11 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
21542154
while (i >= 0) {
21552155
if (ldap_mods[i]->mod_type) {
21562156
efree(ldap_mods[i]->mod_type);
2157+
/* Free attribute values */
2158+
for (j = 0; j < num_berval[i]; j++) {
2159+
efree(ldap_mods[i]->mod_bvalues[j]);
2160+
}
2161+
efree(ldap_mods[i]->mod_bvalues);
21572162
}
21582163
efree(ldap_mods[i]);
21592164
i--;

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)