GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-126554: ctypes: Correctly handle NULL dlsym values by grgalex · Pull Request #126555 · python/cpython

Conversation

@grgalex

@grgalex grgalex commented Nov 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

For dlsym(), a return value of NULL does not necessarily indicate an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

  1. clear the previous error state by calling dlerror()
  2. call dlsym()
  3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In our case of ctypes, I understand that we subjectively treat a NULL return value from dlsym() as an error, so we must format a special string for that.

[1] https://man7.org/linux/man-pages/man3/dlsym.3.html

  • Issue: ctypes: We do not correctly handle NULL dlsym() return values #126554

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sapemeg reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@ghost

ghost commented Nov 7, 2024 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Nov 7, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Nov 7, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Nov 7, 2024
ctypes: We do not correctly handle NULL dlsym() return values #126554
Closed
@grgalex
grgalex force-pushed the fix-issue-126554 branch from b436c5f to bc3ce76 Compare November 7, 2024 20:31
@grgalex
ctypes: Correctly handle NULL dlsym values
3c3c29d
For dlsym(), a return value of NULL does not necessarily indicate
an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

 1. clear the previous error state by calling dlerror()
 2. call dlsym()
 3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In our case, we also subjectively treat a NULL return value
from dlsym() as an error, so we format a special string for that.

[1]: https://man7.org/linux/man-pages/man3/dlsym.3.html

Signed-off-by: Georgios Alexopoulos <[email protected]>
@grgalex
grgalex force-pushed the fix-issue-126554 branch from bc3ce76 to 3c3c29d Compare November 7, 2024 20:41
@efimov-mikhail

efimov-mikhail commented Nov 7, 2024

Copy link
Copy Markdown
Member

On success, these functions return the address associated with
symbol. On failure, they return NULL; the cause of the error can
be diagnosed using dlerror(3).

This is direct quotation from the man page you linked.
Why you think this is correct?

dlsym(), a return value of NULL does not necessarily indicate an error [1].

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@efimov-mikhail

Indeed, I failed to mention that at some point the man page contradicts itself, and I believe that the part you quoted is something the maintainer forgot to remove.

[edit: added this part]

I say this because further up, in the DESCRIPTION, the man page states:

In unusual cases (see NOTES) the value of the symbol could
actually be NULL.  Therefore, a NULL return from dlsym() need not
indicate an error.  The correct way to distinguish an error from
a symbol whose value is NULL is to call dlerror(3) to clear any
old error conditions, then call dlsym(), and then call dlerror(3)
again, saving its return value into a variable, and check whether
this saved value is not NULL.

[end of edit]

Additionally, further down, in the NOTES segment, the man page states:

There are several scenarios when the address of a global symbol
is NULL.  For example, a symbol can be placed at zero address by
the linker, via a linker script or with --defsym command-line
option.  Undefined weak symbols also have NULL value.  Finally,
the symbol value may be the result of a GNU indirect function
(IFUNC) resolver function that returns NULL as the resolved
value.  In the latter case, dlsym() also returns NULL without
error.  However, in the former two cases, the behavior of GNU
dynamic linker is inconsistent: relocation processing succeeds
and the symbol can be observed to have NULL value, but dlsym()
fails and dlerror() indicates a lookup error.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@efimov-mikhail

efimov-mikhail commented Nov 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Understood. IMO, it would be better if we can make an actual reproducible test case, and put this case into regular testing procedure.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 7, 2024

Copy link
Copy Markdown
Contributor Author

See reproducer here: #126554 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Nov 7, 2024
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to see new contributors :)

Unfortunately, this change does need a test. If it's really, really difficult to add one, then I guess we'll figure things out, but in general, it's difficult for us to accept even tiny bugfixes without proper tests.

For something like this, as long as the test works, it's probably OK. You could do something as hacky as hard-coding a byte string into a test file and unpacking that into a .so at runtime, and just disable all the systems that don't work. But it would be preferable if you could find a library that actually triggers this problem in the wild.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Misc/NEWS.d/next/C_API/2024-11-07-20-24-58.gh-issue-126554.ri12eb.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/_ctypes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/_ctypes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/_ctypes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/_ctypes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity ZeroIntensity added topic-ctypes needs backport to 3.12 only security fixes needs backport to 3.13 only security fixes labels Nov 7, 2024
grgalex and others added 3 commits November 8, 2024 15:15
@grgalex @ZeroIntensity
Update Misc/NEWS.d/next/C_API/2024-11-07-20-24-58.gh-issue-126554.ri1…
f71abe8
…2eb.rst

Co-authored-by: Peter Bierma <[email protected]>
@grgalex @ZeroIntensity
Update Modules/_ctypes/_ctypes.c
64a7394
Co-authored-by: Peter Bierma <[email protected]>
@grgalex @ZeroIntensity
Update Modules/_ctypes/_ctypes.c
512a084
Co-authored-by: Peter Bierma <[email protected]>
@grgalex

grgalex commented Nov 8, 2024

Copy link
Copy Markdown
Contributor Author

@ZeroIntensity

  1. I understand that I can just click "Commit" on your proposed changes, and then we can squash before merging
  2. With some grepping I found out that we probably also need to change

    cpython/Modules/_ctypes/callproc.c

    Line 1626 in b19d12f

    ptr = dlsym((void*)handle, name);

    using the same approach.
  3. Regarding the test case, I'm thinking something similar to https://github.com/python/cpython/blob/b19d12f44735583e339ee6ee560588fcf22633b4/Lib/test/test_ctypes/test_find.py, where we will:
    1. Write a fixed string (the content of foo.c from the reproducer),
    2. Compile (as does the test_find testcase) into a shared lib
    3. Load, and access the foo object.
efimov-mikhail and ZeroIntensity reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 8, 2024

Copy link
Copy Markdown
Contributor Author

(2) is done, pushing a commit. I also added a comment that explicitly states why we call dlerror() before dlsym().

From a project maintaner's POV, if we have 3 instances of this phenomenon, do we add a comment disclaimer at every instance? (This is the approach I took, but I'm unsure what the best / most common practice is.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 8, 2024

Copy link
Copy Markdown
Contributor Author

Proceeding with the test case (3), will let you know when ready.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex
Add dlerror() comment, handle callproc.c
cd7d5d7
Signed-off-by: Georgios Alexopoulos <[email protected]>
@ZeroIntensity

ZeroIntensity commented Nov 8, 2024

Copy link
Copy Markdown
Member
  • I understand that I can just click "Commit" on your proposed changes, and then we can squash before merging

Yup. In fact, that's the preferred workflow for us. Force pushing onto one commit is very much discouraged.

2. With some grepping I found out that we probably also need to change

That looks like a problem too, yeah. Thanks!

I'll create a separate issue if I can come up with a repro for the locale issue. (I'll CC you on it, if you want to author that PR as well.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex
test: Add ctypes null dlsym case
cf39b5b
Signed-off-by: Georgios Alexopoulos <[email protected]>
ZeroIntensity
ZeroIntensity reviewed Nov 8, 2024
View reviewed changes
Comment thread Lib/test/test_ctypes/test_dlerror.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 8, 2024

Copy link
Copy Markdown
Contributor Author

Added a commit with the test case under test_ctypes/test_dlerror. PTAL.

Running on the PR branch, it passes :)

On HEAD of main, it explodes (segfault).

Do we have any way of handling this gracefully (maybe running a subprocess with the interpreter?), or is it the correct way to go?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@efimov-mikhail

efimov-mikhail commented Nov 8, 2024

Copy link
Copy Markdown
Member

Do we have any way of handling this gracefully (maybe running a subprocess with the interpreter?)

Could you please explain, what exactly are you speaking about?
Gracefully handling what?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

If we want to be pedantic, we must check all 3 code paths that invoke dlsym().

In this case, we only check one (PyCFuncPtr_FromDll).

On one hand, since our amendment is the same across all 3 funcs, we should theoretically be OK.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Do we have any way of handling this gracefully (maybe running a subprocess with the interpreter?)

Could you please explain, what exactly are you speaking about? Gracefully handling what?

@efimov-mikhail

If I run the test on the main branch, the Python interpreter gets a segmentation fault.

What I'm saying is that in a scenario where someone runs all tests, when the ./python -m test process encounters this test, it will exit with a segmentation fault and terminate the testing procedure. It won't mark the test as failing. It will explode.

Maybe I have misunderstood something regarding the philosophy of testing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Nov 8, 2024

Copy link
Copy Markdown
Member

Run the test in a subprocess and check the return code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

grgalex and others added 2 commits November 14, 2024 15:43
@grgalex @encukou
Update Lib/test/test_ctypes/test_dlerror.py
cda7771
Co-authored-by: Petr Viktorin <[email protected]>
@grgalex
test: Handle different architectures, where IFUNC not supported
4b1bb47
Signed-off-by: Georgios Alexopoulos <[email protected]>
@ZeroIntensity

ZeroIntensity commented Nov 14, 2024

Copy link
Copy Markdown
Member

I'm not sure if the ASan failures are related.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz approved these changes Nov 14, 2024
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One PEP-7 nitpick and two de-chop suggestions. I didn't write suggestions for the PyErr_SetString usages since I wasn't sure it fits on 80 chars.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

grgalex reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Modules/_ctypes/callproc.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/callproc.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/_ctypes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

grgalex and others added 2 commits November 14, 2024 18:42
@grgalex @picnixz
Update Modules/_ctypes/callproc.c
2290aa4
Co-authored-by: Bénédikt Tran <[email protected]>
@grgalex
Don't break when under 80 chars
ab22349
Signed-off-by: Georgios Alexopoulos <[email protected]>
@ZeroIntensity ZeroIntensity added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 14, 2024
@bedevere-bot

bedevere-bot commented Nov 14, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @ZeroIntensity for commit ab22349 🤖

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 14, 2024
@ZeroIntensity

ZeroIntensity commented Nov 14, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Let's try the build bots again and see if it holds up now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Nov 14, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Buildbots are all good, the one failure is a problem with the DRC API that got merged the other day, which is unrelated. I'll submit a (separate) patch to fix that. I think we're good to merge!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou
encukou merged commit 8717f79 into python:main Nov 15, 2024
@miss-islington-app

miss-islington-app Bot commented Nov 15, 2024

Copy link
Copy Markdown

Thanks @grgalex for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 15, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 15, 2024
@grgalex @ZeroIntensity
@picnixz @encukou @miss-islington
pythongh-126554: ctypes: Correctly handle NULL dlsym values (pythonGH…
5b4116e
…-126555)

For dlsym(), a return value of NULL does not necessarily indicate
an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

 1. clear the previous error state by calling dlerror()
 2. call dlsym()
 3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In ctypes we choose to treat a NULL return value from dlsym()
as a "not found" error. This is the same as the fallback
message we use on Windows, Cygwin or when getting/formatting
the error reason fails.

[1]: https://man7.org/linux/man-pages/man3/dlsym.3.html

(cherry picked from commit 8717f79)

Co-authored-by: George Alexopoulos <[email protected]>
Signed-off-by: Georgios Alexopoulos <[email protected]>
Signed-off-by: Georgios Alexopoulos <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Nov 15, 2024

Copy link
Copy Markdown

Sorry, @grgalex and @encukou, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 8717f792f7cc343599dc60daf80630cceb66145b 3.12
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Nov 15, 2024

Copy link
Copy Markdown

GH-126861 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned encukou Nov 15, 2024
@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Nov 15, 2024
@efimov-mikhail

efimov-mikhail commented Nov 15, 2024

Copy link
Copy Markdown
Member

@grgalex, you've finished it! Congrats with your first contribution to CPython!

grgalex reacted with heart emoji ZeroIntensity and encukou reacted with rocket emoji
All reactions
  • ❤️ 1 reaction
  • 🚀 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@grgalex

grgalex commented Nov 15, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for the guidance @efimov-mikhail, @ZeroIntensity, @encukou.

Looking forward to collaborating again in the future!

ZeroIntensity and efimov-mikhail reacted with heart emoji
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv pushed a commit that referenced this pull request Nov 17, 2024
@miss-islington @grgalex
@ZeroIntensity @picnixz @encukou
[3.13] gh-126554: ctypes: Correctly handle NULL dlsym values (GH-126555…
52efb95
…) (#126861)

For dlsym(), a return value of NULL does not necessarily indicate
an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

 1. clear the previous error state by calling dlerror()
 2. call dlsym()
 3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In ctypes we choose to treat a NULL return value from dlsym()
as a "not found" error. This is the same as the fallback
message we use on Windows, Cygwin or when getting/formatting
the error reason fails.

[1]: https://man7.org/linux/man-pages/man3/dlsym.3.html

(cherry picked from commit 8717f79)

Signed-off-by: Georgios Alexopoulos <[email protected]>
Co-authored-by: George Alexopoulos <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
picnixz added a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
@grgalex @ZeroIntensity
@picnixz @encukou
pythongh-126554: ctypes: Correctly handle NULL dlsym values (pythonGH…
583e01a
…-126555)

For dlsym(), a return value of NULL does not necessarily indicate
an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

 1. clear the previous error state by calling dlerror()
 2. call dlsym()
 3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In ctypes we choose to treat a NULL return value from dlsym()
as a "not found" error. This is the same as the fallback
message we use on Windows, Cygwin or when getting/formatting
the error reason fails.

[1]: https://man7.org/linux/man-pages/man3/dlsym.3.html

Signed-off-by: Georgios Alexopoulos <[email protected]>
Signed-off-by: Georgios Alexopoulos <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
@picnixz

picnixz commented Dec 8, 2024

Copy link
Copy Markdown
Member

3.13 backports were done but not 3.12; @encukou Do you want the 3.12 backport in the end or can we close the issue with only 3.13 and 3.14 being affected?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Dec 9, 2024

Copy link
Copy Markdown
Member

As I said, I'm fine not backporting. If anyone disagrees, open the PR :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Dec 9, 2024

Copy link
Copy Markdown

GH-127764 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Dec 9, 2024
serhiy-storchaka pushed a commit that referenced this pull request Dec 17, 2024
@picnixz @grgalex
@ZeroIntensity @encukou
[3.12] gh-126554: ctypes: Correctly handle NULL dlsym values (GH-126555…
6ac578c
…) (GH-127764)

For dlsym(), a return value of NULL does not necessarily indicate
an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

 1. clear the previous error state by calling dlerror()
 2. call dlsym()
 3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In ctypes we choose to treat a NULL return value from dlsym()
as a "not found" error. This is the same as the fallback
message we use on Windows, Cygwin or when getting/formatting
the error reason fails.

[1]: https://man7.org/linux/man-pages/man3/dlsym.3.html

Signed-off-by: Georgios Alexopoulos <[email protected]>
Signed-off-by: Georgios Alexopoulos <[email protected]>
Co-authored-by: George Alexopoulos <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
@grgalex @ZeroIntensity
@picnixz @encukou @ebonnal
pythongh-126554: ctypes: Correctly handle NULL dlsym values (pythonGH…
1a7fa4b
…-126555)

For dlsym(), a return value of NULL does not necessarily indicate
an error [1].

Therefore, to avoid using stale (or NULL) dlerror() values, we must:

 1. clear the previous error state by calling dlerror()
 2. call dlsym()
 3. call dlerror()

If the return value of dlerror() is not NULL, an error occured.

In ctypes we choose to treat a NULL return value from dlsym()
as a "not found" error. This is the same as the fallback
message we use on Windows, Cygwin or when getting/formatting
the error reason fails.

[1]: https://man7.org/linux/man-pages/man3/dlsym.3.html

Signed-off-by: Georgios Alexopoulos <[email protected]>
Signed-off-by: Georgios Alexopoulos <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
@grgalex grgalex mentioned this pull request Nov 12, 2026
_interpchannels: ChannelID.__eq__ reads module state from sys.modules without a check #161841
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou approved these changes
@efimov-mikhail efimov-mikhail efimov-mikhail approved these changes
@picnixz picnixz picnixz approved these changes
@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@encukou encukou

Labels

topic-ctypes

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@grgalex @efimov-mikhail @ZeroIntensity @encukou @bedevere-bot @picnixz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.