Skip to content

gh-124064: Fix some -Wconversion warnings #124174

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

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static inline void _PyGC_CLEAR_FINALIZED(PyObject *op) {
_PyObject_CLEAR_GC_BITS(op, _PyGC_BITS_FINALIZED);
#else
PyGC_Head *gc = _Py_AS_GC(op);
gc->_gc_prev &= ~_PyGC_PREV_MASK_FINALIZED;
gc->_gc_prev &= ~(uintptr_t)_PyGC_PREV_MASK_FINALIZED;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be better to make the _PyGC_PREV_MASK_FINALIZED literal unsigned.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried #define _PyGC_PREV_MASK_FINALIZED 1U but the code got miscompiled and failed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ugh... you're right. It won't get sign extended in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wrote a different fix in the following change: #124177

-#define _PyGC_PREV_MASK_FINALIZED 1
+#define _PyGC_PREV_MASK_FINALIZED ((uintptr_t)1)

It seems to work like that. I also prefer to fix the constant declaration rather than having to cast the constant every time.

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, I started a discussion about -Wsign-conversion in the #gsoc-security-project Discord channel. It seems to me that adding these explicit casts in many places to address -Wsign-conversion does not make the code less bug prone and probably does the opposite.

In this case, we now have a tighter coupling between the _PyGC_PREV_MASK_FINALIZED and the type of _gc_prev and _gc_next. That seems worse to me than before where we relied on the implicit sign conversion.

For example, if we hypothetically changed _gc_prev to uint64_t, the code would now be incorrect.

#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ _Py_memory_repeat(char* dest, Py_ssize_t len_dest, Py_ssize_t len_src)
Py_ssize_t copied = len_src;
while (copied < len_dest) {
Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
memcpy(dest + copied, dest, bytes_to_copy);
memcpy(dest + copied, dest, (size_t)bytes_to_copy);
copied += bytes_to_copy;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ typedef union _PyStackRef {
static inline PyObject *
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
{
PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
PyObject *cleared = ((PyObject *)((stackref).bits & (~(uintptr_t)Py_TAG_BITS)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

return cleared;
}
#else
Expand Down Expand Up @@ -133,7 +133,7 @@ _PyStackRef_FromPyObjectSteal(PyObject *obj)
{
// Make sure we don't take an already tagged value.
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
int tag = (obj == NULL || _Py_IsImmortal(obj)) ? (Py_TAG_DEFERRED) : Py_TAG_PTR;
unsigned int tag = (obj == NULL || _Py_IsImmortal(obj)) ? (Py_TAG_DEFERRED) : Py_TAG_PTR;
return ((_PyStackRef){.bits = ((uintptr_t)(obj)) | tag});
}
# define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
Expand Down
1 change: 0 additions & 1 deletion Tools/build/.warningignore_macos
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Include/internal/mimalloc/mimalloc/internal.h 4
Include/internal/pycore_backoff.h 1
Include/internal/pycore_dict.h 2
Include/internal/pycore_gc.h 1
Include/internal/pycore_long.h 2
Include/internal/pycore_object.h 4
Modules/_asynciomodule.c 3
Expand Down
4 changes: 0 additions & 4 deletions Tools/build/.warningignore_ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ Include/internal/pycore_asdl.h 1
Include/internal/pycore_backoff.h 3
Include/internal/pycore_blocks_output_buffer.h 1
Include/internal/pycore_dict.h 2
Include/internal/pycore_gc.h 1
Include/internal/pycore_gc.h 1
Include/internal/pycore_interp.h 1
Include/internal/pycore_list.h 1
Include/internal/pycore_long.h 3
Include/internal/pycore_object.h 4
Include/internal/pycore_obmalloc.h 1
Expand Down Expand Up @@ -233,7 +230,6 @@ Python/generated_cases.c.h 27
Python/generated_cases.c.h 27
Python/getargs.c 7
Python/hashtable.c 1
Python/import.c 6
Python/import.c 7
Python/initconfig.c 11
Python/instrumentation.c 43
Expand Down
Loading