Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
The man(3) page fordlsym() states:
In unusual cases (see NOTES) the value of the symbol could actually beNULL. Therefore, a NULL return from dlsym() need not indicate an error.The correct way to distinguish an error from a symbol whose value is NULLis to call dlerror(3) to clear any old error conditions, then call dl‐sym(), and then call dlerror(3) again, saving its return value into avariable, and check whether this saved value is not NULL.As such, there can be cases where a call todlsym returnsNULL, while no error has been encountered and the string thatdlerror() returns has not been (re)set.
Currently, (https://github.com/python/cpython/blob/main/Modules/_ctypes/_ctypes.c#L970) we do:
<...>address= (void*)dlsym(handle,name);if (!address) {#ifdef__CYGWIN__<snipWindowsstuff>#elsePyErr_SetString(PyExc_ValueError,dlerror());<...>
Ifdlsym() returnsNULL, then by callingdlerror() we pass eitherNULL or a previous, unconsumed error string toPyErr_SetString.
In the context ofctypes, aNULL return bydlsym() might indeed always be considered an error.
To correctly express this, we should:
- Call
dlerror()beforedlsym(), to clear any previous error - Call
dlerror()afterdlsym(), to see ifdlsym()encountered any error- If
dlerror()returns a non-NULL value, then pass its result toPyErr_SetString - If
dlerror()returns NULL, then check ifaddressis NULL, and if so, pass a custom error string toPyErr_SetString.
- If
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-126554: ctypes: Correctly handle NULL dlsym values #126555
- [3.13] gh-126554: ctypes: Correctly handle NULL dlsym values (GH-126555) #126861
- [3.12] gh-126554: ctypes: Correctly handle NULL dlsym values (GH-126555) #127764
- gh-126554: correct detection of
gccforTestNullDlsym.test_null_dlsym#129872 - [3.13] gh-126554: correct detection of
gccforTestNullDlsym.test_null_dlsym(GH-129872) #129944 - [3.12] gh-126554: correct detection of
gccforTestNullDlsym.test_null_dlsym(GH-129872) #129945