php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79165 trying to access ldap_get_entries array triggers: Trying to access array offset
Submitted: 2020-01-24 11:55 UTC Modified: 2020-03-12 18:25 UTC
From: mbiebl at messageconcept dot com Assigned:
Status: Not a bug Package: LDAP related
PHP Version: 7.4.2 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mbiebl at messageconcept dot com
New email:
PHP Version: OS:

 

 [2020-01-24 11:55 UTC] mbiebl at messageconcept dot com
Description:
------------
I have the following code

$result = ldap_search($con, $dn, $filter, $justthese);
$entries = ldap_get_entries($con, $result);

and then the following code to iterate through the results

foreach ($entries as $e) { ... }

This code worked fine with 7.3 and now yields the following error with 7.4:
"Trying to access array offset on value of type resource"

ldap_get_entries() is supposed to return an array and var_dump($entries) prints
array(11) {
  ["count"]=>
  int(10)
  [0]=>
 ....
}

So something is at odds here. Why does PHP 7.4 think $entries is not an array?




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-24 12:05 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2020-01-24 12:05 UTC] nikic@php.net
foreach does not trigger this notice. Please check the reported line number carefully. It must involve something like "$array[$offset]".
 [2020-01-24 12:28 UTC] mbiebl at messageconcept dot com
-Status: Feedback +Status: Open
 [2020-01-24 12:28 UTC] mbiebl at messageconcept dot com
Hm, right. I guess I need to explicitly skip the "count" element now which is part of the $result array.

foreach (array_slice($entries, 1) as $e) { .. }

appears to work. Not sure if this is the cleanest solution though. WDYT?
 [2020-01-24 13:07 UTC] cmb@php.net
-Status: Open +Status: Feedback
 [2020-01-24 13:07 UTC] cmb@php.net
Could you please post the relevant code (and show the reported
line number)?
 [2020-01-24 13:12 UTC] mbiebl at messageconcept dot com
-Status: Feedback +Status: Open
 [2020-01-24 13:12 UTC] mbiebl at messageconcept dot com
$result = ldap_search($con, $dn, $filter, $justthese);
$entries = ldap_get_entries($con, $result);

foreach ($entries as $e) {
    if ($e[$this->uidNameAttribute][0] == '') {
        continue;
    }
    $principal = array('uri' => 'principals/' . strtolower($e[$this->uidNameAttribute][0]),);
    foreach ($this->fieldMap as $key => $value) {
        if (isset($e[$value['ldapProperty']])) {
            $principal[$key] = $e[$value['ldapProperty']][0];
        }
    }
    $principals[] = $principal;
}
 [2020-01-24 21:29 UTC] mbiebl at messageconcept dot com
-Status: Open +Status: Closed
 [2020-01-24 21:29 UTC] mbiebl at messageconcept dot com
Let's close this. This is an issue that needs to be solved in our code.
The issue was just hidden with previous PHP releases. We need to handle the 'count' array entry explicitly.
Not sure yet, if array_slice() is the proper solution. I guess there is no guarantee that the 'count' entry will always be the first entry in the result array.
 [2020-01-24 22:46 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 [2020-03-12 18:25 UTC] mbiebl at messageconcept dot com
@nikic: I wonder, should the example at https://www.php.net/manual/en/ldap.examples-controls.php be updated accordingly?

E.g. Example #5 uses

    $entries = ldap_get_entries($link, $result);
    foreach ($entries as $entry) {
        echo "cn: ".$entry['cn'][0]."\n";
    }

With PHP 7.4 this will now trigger the aforementioned notice afaics.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 14:01:31 2024 UTC