Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ext/ldap/ldap.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ static void ldap_result_entry_free_obj(zend_object *obj)
} \
}

#define VERIFY_LDAP_RESULT_ENTRY_OPEN(re) \
{ \
ldap_resultdata *ldap_entry_result = Z_LDAP_RESULT_P(&re->res); \
VERIFY_LDAP_RESULT_OPEN(ldap_entry_result); \
}

static bool php_ldap_is_numerically_indexed_array(zend_array *arr)
{
if (zend_hash_num_elements(arr) == 0 || HT_IS_PACKED(arr)) {
Expand Down Expand Up @@ -1874,6 +1880,7 @@ PHP_FUNCTION(ldap_next_entry)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
RETVAL_FALSE;
Expand Down Expand Up @@ -2004,6 +2011,7 @@ PHP_FUNCTION(ldap_first_attribute)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
RETURN_FALSE;
Expand Down Expand Up @@ -2032,6 +2040,7 @@ PHP_FUNCTION(ldap_next_attribute)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

if (resultentry->ber == NULL) {
php_error_docref(NULL, E_WARNING, "Called before calling ldap_first_attribute() or no attributes found in result entry");
Expand Down Expand Up @@ -2075,6 +2084,7 @@ PHP_FUNCTION(ldap_get_attributes)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

array_init(return_value);
num_attrib = 0;
Expand Down Expand Up @@ -2129,6 +2139,7 @@ PHP_FUNCTION(ldap_get_values_len)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
Expand Down Expand Up @@ -2164,6 +2175,7 @@ PHP_FUNCTION(ldap_get_dn)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

text = ldap_get_dn(ld->link, resultentry->data);
if (text != NULL) {
Expand Down Expand Up @@ -3590,6 +3602,7 @@ PHP_FUNCTION(ldap_next_reference)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
RETVAL_FALSE;
Expand Down Expand Up @@ -3620,6 +3633,7 @@ PHP_FUNCTION(ldap_parse_reference)
VERIFY_LDAP_LINK_CONNECTED(ld);

resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
VERIFY_LDAP_RESULT_ENTRY_OPEN(resultentry);

if (ldap_parse_reference(ld->link, resultentry->data, &lreferrals, NULL /* &serverctrls */, 0) != LDAP_SUCCESS) {
RETURN_FALSE;
Expand Down
48 changes: 48 additions & 0 deletions ext/ldap/tests/ldap_free_result_entry_after_free.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
ldap_free_result() - entries of a freed result can no longer be used
--EXTENSIONS--
ldap
--SKIPIF--
<?php require_once('skipifbindfailure.inc'); ?>
--FILE--
<?php
require "connect.inc";

$link = ldap_connect_and_bind($uri, $user, $passwd, $protocol_version);
insert_dummy_data($link, $base);

$result = ldap_search($link, "cn=userA,$base", "(cn=*)");
$entry = ldap_first_entry($link, $result);
var_dump(ldap_get_dn($link, $entry));
ldap_free_result($result);

foreach ([
fn() => ldap_get_dn($link, $entry),
fn() => ldap_get_attributes($link, $entry),
fn() => ldap_first_attribute($link, $entry),
fn() => ldap_next_attribute($link, $entry),
fn() => ldap_next_entry($link, $entry),
fn() => ldap_get_values_len($link, $entry, "cn"),
] as $fn) {
try {
$fn();
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
}
?>
--CLEAN--
<?php
include "connect.inc";

$link = ldap_connect_and_bind($uri, $user, $passwd, $protocol_version);
remove_dummy_data($link, $base);
?>
--EXPECTF--
string(%d) "cn=userA,%s"
Error: LDAP result has already been closed
Error: LDAP result has already been closed
Error: LDAP result has already been closed
Error: LDAP result has already been closed
Error: LDAP result has already been closed
Error: LDAP result has already been closed
Loading