Skip to content

Commit 6d5a8c2

Browse files
authored
gh-133476: Assert with the PyStackRef_IsTaggedInt function (GH-133477)
1 parent c492ac7 commit 6d5a8c2

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Include/internal/pycore_stackref.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,16 @@ PyStackRef_IsNone(_PyStackRef ref)
9595
return _Py_stackref_get_object(ref) == Py_None;
9696
}
9797

98+
static inline bool
99+
PyStackRef_IsTaggedInt(_PyStackRef ref)
100+
{
101+
return (ref.index & 1) == 1;
102+
}
103+
98104
static inline PyObject *
99105
_PyStackRef_AsPyObjectBorrow(_PyStackRef ref, const char *filename, int linenumber)
100106
{
101-
assert((ref.index & 1) == 0);
107+
assert(!PyStackRef_IsTaggedInt(ref));
102108
_Py_stackref_record_borrow(ref, filename, linenumber);
103109
return _Py_stackref_get_object(ref);
104110
}
@@ -135,12 +141,6 @@ _PyStackRef_FromPyObjectImmortal(PyObject *obj, const char *filename, int linenu
135141
}
136142
#define PyStackRef_FromPyObjectImmortal(obj) _PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj), __FILE__, __LINE__)
137143

138-
static inline bool
139-
PyStackRef_IsTaggedInt(_PyStackRef ref)
140-
{
141-
return (ref.index & 1) == 1;
142-
}
143-
144144
static inline void
145145
_PyStackRef_CLOSE(_PyStackRef ref, const char *filename, int linenumber)
146146
{
@@ -256,7 +256,7 @@ PyStackRef_TagInt(intptr_t i)
256256
static inline intptr_t
257257
PyStackRef_UntagInt(_PyStackRef i)
258258
{
259-
assert((i.bits & Py_INT_TAG) == Py_INT_TAG);
259+
assert(PyStackRef_IsTaggedInt(i));
260260
intptr_t val = (intptr_t)i.bits;
261261
return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, val, 2);
262262
}
@@ -286,6 +286,7 @@ static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
286286
static inline PyObject *
287287
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
288288
{
289+
assert(!PyStackRef_IsTaggedInt(stackref));
289290
PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
290291
return cleared;
291292
}

0 commit comments

Comments
 (0)