ldap3 icon indicating copy to clipboard operation
ldap3 copied to clipboard

IndexError: list index out of range

Open dehatanes opened this issue 2 years ago • 1 comments

Hey there :)

I noticed this (unusual) error while trying to connect to the Server.

  [... private stacktrace ...]
  File /usr/local/lib/python3.6/dist-packages/ldap3/core/connection.py, line 524, in bind
  File /usr/local/lib/python3.6/dist-packages/ldap3/strategy/sync.py, line 56, in open
  File /usr/local/lib/python3.6/dist-packages/ldap3/strategy/base.py, line 133, in open
  File /usr/local/lib/python3.6/dist-packages/ldap3/core/server.py, line 272, in update_availability
IndexError: list index out of range

It was an unusual exception that happened on an env that I don't own, so unfornutalely I could not reproduce it to bring further information here 😕

dehatanes avatar Jan 31 '23 18:01 dehatanes

Possible solution (?)

As I am not used with this lib's code and caveats I didn't felt comfortable on opening a PR, but here is a suggestion to the maintainers if you think is suitable :)

Looking at the lib's code I believe this error is related to the fact that inside the update_availability function we check for len(self._address_info) and then try to access self.address_info[cont] one line later.

(notice that in the first statement we use _address_info while using address_info without underscore in the second one).

As self.address_info recalculates the value of self._address_info we are susceptible to having a problem finding addresses or something alike that can lead to address_info returning a list with a smaller length than _address_info that we used to do the loop breaking check.

I believe a similar behavior of refreshing the addresses in the loop, but with a safer algorithm could be achieved by switching the variable of those lines like:

def update_availability(self, address, available):
        cont = 0
        while cont < len(self.address_info):  # <-- here changed
            if self._address_info[cont] == address:   # <-- here changed
                self._address_info[cont][5] = True if available else False
                self._address_info[cont][6] = datetime.now()
                break
            cont += 1

dehatanes avatar Jan 31 '23 18:01 dehatanes