Skip to content

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

Merged
pablogsal merged 1 commit intopython:mainfrom
chrstphrchvz:patch-111178-pablogsal
Dec 6, 2023
Merged

gh-111178: Define visitproc callback functions properly and remove unnecessary casts in gcmodule.c#112687
pablogsal merged 1 commit intopython:mainfrom
chrstphrchvz:patch-111178-pablogsal

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
@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
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants