Skip to content

Commit 3394813

Browse files
committed
fix: reject str in tp_init with hashlib-compatible error
Add PyUnicode_Check before PyArg_ParseTupleAndKeywords in all 4 tp_init functions, raising 'Strings must be encoded before hashing' when str is passed. Needed for PyPy where tp_vectorcall is not supported and tp_init is the fallback constructor path.
1 parent 53e721d commit 3394813

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/_xxhash.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ static int PYXXH32_init(PYXXH32Object *self, PyObject *args, PyObject *kwargs)
555555

556556
buf.buf = buf.obj = NULL;
557557

558+
if (PyTuple_GET_SIZE(args) >= 1 && PyUnicode_Check(PyTuple_GET_ITEM(args, 0))) {
559+
PyErr_SetString(PyExc_TypeError,
560+
"Strings must be encoded before hashing");
561+
return -1;
562+
}
558563
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|y*I:__init__", keywords, &buf, &seed)) {
559564
return -1;
560565
}
@@ -910,6 +915,11 @@ static int PYXXH64_init(PYXXH64Object *self, PyObject *args, PyObject *kwargs)
910915

911916
buf.buf = buf.obj = NULL;
912917

918+
if (PyTuple_GET_SIZE(args) >= 1 && PyUnicode_Check(PyTuple_GET_ITEM(args, 0))) {
919+
PyErr_SetString(PyExc_TypeError,
920+
"Strings must be encoded before hashing");
921+
return -1;
922+
}
913923
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|y*K:__init__", keywords, &buf, &seed)) {
914924
return -1;
915925
}
@@ -1264,6 +1274,11 @@ static int PYXXH3_64_init(PYXXH3_64Object *self, PyObject *args, PyObject *kwarg
12641274

12651275
buf.buf = buf.obj = NULL;
12661276

1277+
if (PyTuple_GET_SIZE(args) >= 1 && PyUnicode_Check(PyTuple_GET_ITEM(args, 0))) {
1278+
PyErr_SetString(PyExc_TypeError,
1279+
"Strings must be encoded before hashing");
1280+
return -1;
1281+
}
12671282
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|y*K:__init__", keywords, &buf, &seed)) {
12681283
return -1;
12691284
}
@@ -1627,6 +1642,11 @@ static int PYXXH3_128_init(PYXXH3_128Object *self, PyObject *args, PyObject *kwa
16271642

16281643
buf.buf = buf.obj = NULL;
16291644

1645+
if (PyTuple_GET_SIZE(args) >= 1 && PyUnicode_Check(PyTuple_GET_ITEM(args, 0))) {
1646+
PyErr_SetString(PyExc_TypeError,
1647+
"Strings must be encoded before hashing");
1648+
return -1;
1649+
}
16301650
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|y*K:__init__", keywords, &buf, &seed)) {
16311651
return -1;
16321652
}

0 commit comments

Comments
 (0)