Skip to content

Spurious -Warray-bounds warning on GCC 11 in free-threaded build #122974

Closed
@colesbury

Description

@colesbury

After #122418, GCC 11+ now emits a (spurious) warning in optimized builds, but not debug builds. Clang does not issue a warning

Objects/longobject.c: In function ‘_PyLong_FromMedium’:
./Include/internal/pycore_object.h:310:19: warning: array subscript ‘PyHeapTypeObject {aka struct _heaptypeobject}[0]’ is partly outside array bounds of ‘PyTypeObject[1]’ {aka ‘struct _typeobject[1]’} [-Warray-bounds]
  310 |     if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
      |                 ~~^~~~~~~~~~~
Ojects/longobject.c:6596:14: note: while referencing ‘PyLong_Type’
 6596 | PyTypeObject PyLong_Type = {
      |              ^~~~~~~~~~~

GCC is clever enough to inline the _Py_INCREF_TYPE call on PyLong_Type, but not clever enough to understand the _PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) guard.

static inline void
_Py_INCREF_TYPE(PyTypeObject *type)
{
if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
assert(_Py_IsImmortal(type));
return;
}
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
PyHeapTypeObject *ht = (PyHeapTypeObject *)type;
// Unsigned comparison so that `unique_id=-1`, which indicates that
// per-thread refcounting has been disabled on this type, is handled by
// the "else".
if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
# ifdef Py_REF_DEBUG
_Py_INCREF_IncRefTotal();
# endif
_Py_INCREF_STAT_INC();
tstate->types.refcounts[ht->unique_id]++;
}
else {
// The slow path resizes the thread-local refcount array if necessary.
// It handles the unique_id=-1 case to keep the inlinable function smaller.
_PyType_IncrefSlow(ht);
}
}

I don't see a great way to rewrite _Py_INCREF_TYPE to avoid the warning, so maybe we should just use a #pragma to suppress the Warray-bounds warnings in that function.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions