Skip to content

get_hash_info might potentially swallow errors in sysmodule.c #115320

@sobolevn

Description

@sobolevn

Bug report

cpython/Python/sysmodule.c

Lines 1494 to 1526 in 54bde5d

static PyObject *
get_hash_info(PyThreadState *tstate)
{
PyObject *hash_info;
int field = 0;
PyHash_FuncDef *hashfunc;
hash_info = PyStructSequence_New(&Hash_InfoType);
if (hash_info == NULL)
return NULL;
hashfunc = PyHash_GetFuncDef();
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(8*sizeof(Py_hash_t)));
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromSsize_t(_PyHASH_MODULUS));
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(_PyHASH_INF));
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(0)); // This is no longer used
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(_PyHASH_IMAG));
PyStructSequence_SET_ITEM(hash_info, field++,
PyUnicode_FromString(hashfunc->name));
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(hashfunc->hash_bits));
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(hashfunc->seed_bits));
PyStructSequence_SET_ITEM(hash_info, field++,
PyLong_FromLong(Py_HASH_CUTOFF));
if (_PyErr_Occurred(tstate)) {
Py_CLEAR(hash_info);
return NULL;
}
return hash_info;

This code only checks for errors in the very last line:

cpython/Python/sysmodule.c

Lines 1522 to 1526 in 54bde5d

if (_PyErr_Occurred(tstate)) {
Py_CLEAR(hash_info);
return NULL;
}
return hash_info;

I think that this pattern should not be used. Because it can potentially swallow previous errors on only show the very last one.

I propose to refactor this code to show the first error instead.

Linked PRs

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions