Skip to content

Commit 02a7c49

Browse files
committed
ldap: Fix memory leak in ldap_set_options()
Closes GH-20659.
1 parent 26c0cbd commit 02a7c49

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
- GD:
1414
. Fixed bug GH-20622 (imagestring/imagestringup overflow). (David Carlier)
1515

16+
- LDAP:
17+
. Fix memory leak in ldap_set_options(). (ndossche)
1618

1719
18 Dec 2025, PHP 8.3.29
1820

ext/ldap/ldap.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,15 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
664664
goto failure;
665665
}
666666

667+
zend_string *context_str = NULL;
667668
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1)) != NULL) {
668-
tmpstring = zval_get_string(tmp);
669+
context_str = zval_get_string(tmp);
669670
if (EG(exception)) {
670671
rc = -1;
671672
goto failure;
672673
}
673-
context.bv_val = ZSTR_VAL(tmpstring);
674-
context.bv_len = ZSTR_LEN(tmpstring);
674+
context.bv_val = ZSTR_VAL(context_str);
675+
context.bv_len = ZSTR_LEN(context_str);
675676
vlvInfo.ldvlv_context = &context;
676677
} else {
677678
vlvInfo.ldvlv_context = NULL;
@@ -683,6 +684,9 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
683684
if (rc != LDAP_SUCCESS) {
684685
php_error_docref(NULL, E_WARNING, "Failed to create VLV control value: %s (%d)", ldap_err2string(rc), rc);
685686
}
687+
if (context_str) {
688+
zend_string_release_ex(context_str, false);
689+
}
686690
} else {
687691
zend_type_error("%s(): Control OID %s cannot be of type array", get_active_function_name(), ZSTR_VAL(control_oid));
688692
rc = -1;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
ldap_set_option() - Leaks attrvalue and context
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
require "connect.inc";
8+
9+
$link = ldap_connect($uri);
10+
11+
$attrvalue = str_repeat("attrvalue", random_int(1, 1));
12+
$context = str_repeat("context", random_int(1, 1));
13+
14+
$controls = [
15+
["oid" => "2.16.840.1.113730.3.4.9", "value" => ["attrvalue" => $attrvalue, "context" => $context, "before" => 0, "after" => 0]],
16+
];
17+
18+
ldap_set_option($link, LDAP_OPT_CLIENT_CONTROLS, $controls);
19+
ldap_get_option($link, LDAP_OPT_CLIENT_CONTROLS, $controls_out);
20+
21+
var_dump($controls_out);
22+
?>
23+
--EXPECTF--
24+
array(1) {
25+
["2.16.840.1.113730.3.4.9"]=>
26+
array(3) {
27+
["oid"]=>
28+
string(23) "2.16.840.1.113730.3.4.9"
29+
["iscritical"]=>
30+
bool(false)
31+
["value"]=>
32+
string(28) "0%0%0 attrvaluecontext"
33+
}
34+
}

0 commit comments

Comments
 (0)