Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-111178: Define visitproc callback functions properly and remove unnecessary casts in gcmodule.c #112687

Merged
merged 1 commit into from
Dec 6, 2023

Conversation

chrstphrchvz
Copy link
Contributor

@chrstphrchvz chrstphrchvz commented Dec 4, 2023

Because many other changes are likely needed for gh-111178, I have opened this PR separately for review by the respective codeowner. The error for visit_reachable() might be the most common one I encounter.

Potential compiler warnings addressed:

Modules/gcmodule.c:606:21: warning: cast from 'int (*)(PyObject *, PyGC_Head *)' (aka 'int (*)(struct _object *, PyGC_Head *)') to 'visitproc' (aka 'int (*)(struct _object *, void *)') converts to incompatible function type [-Wcast-function-type-strict]
  606 |                     (visitproc)visit_reachable,
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
Modules/gcmodule.c:754:25: warning: cast from 'int (*)(PyObject *, PyGC_Head *)' (aka 'int (*)(struct _object *, PyGC_Head *)') to 'visitproc' (aka 'int (*)(struct _object *, void *)') converts to incompatible function type [-Wcast-function-type-strict]
  754 |                         (visitproc)visit_move,
      |                         ^~~~~~~~~~~~~~~~~~~~~
Modules/gcmodule.c:1707:27: warning: cast from 'int (*)(PyObject *, PyObject *)' (aka 'int (*)(struct _object *, struct _object *)') to 'visitproc' (aka 'int (*)(struct _object *, void *)') converts to incompatible function type [-Wcast-function-type-strict]
 1707 |         if (traverse(obj, (visitproc)referrersvisit, objs)) {
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~
Modules/gcmodule.c:1773:27: warning: cast from 'int (*)(PyObject *, PyObject *)' (aka 'int (*)(struct _object *, struct _object *)') to 'visitproc' (aka 'int (*)(struct _object *, void *)') converts to incompatible function type [-Wcast-function-type-strict]
 1773 |         if (traverse(obj, (visitproc)referentsvisit, result)) {
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~

A few example UBSan -fsanitize=function errors addresses:

Objects/dictobject.c:3650:17: runtime error: call to function visit_reachable through pointer to incorrect function type 'int (*)(struct _object *, void *)'
gcmodule.c:502: note: visit_reachable defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/dictobject.c:3650:17 in 
Objects/descrobject.c:694:5: runtime error: call to function visit_reachable through pointer to incorrect function type 'int (*)(struct _object *, void *)'
gcmodule.c:502: note: visit_reachable defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/descrobject.c:694:5 in 
Objects/dictobject.c:3658:17: runtime error: call to function visit_reachable through pointer to incorrect function type 'int (*)(struct _object *, void *)'
gcmodule.c:502: note: visit_reachable defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/dictobject.c:3658:17 in 
Objects/dictobject.c:3659:17: runtime error: call to function visit_reachable through pointer to incorrect function type 'int (*)(struct _object *, void *)'
gcmodule.c:502: note: visit_reachable defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/dictobject.c:3659:17 in 
Objects/funcobject.c:1278:5: runtime error: call to function visit_reachable through pointer to incorrect function type 'int (*)(struct _object *, void *)'
gcmodule.c:502: note: visit_reachable defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/funcobject.c:1278:5 in 

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Thank you for helping eliminate this kind of undefined behaviour.

@pablogsal pablogsal merged commit b920d6c into python:main Dec 6, 2023
34 checks passed
@chrstphrchvz chrstphrchvz deleted the patch-111178-pablogsal branch December 6, 2023 21:17
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants