Prevent integer overflow for unknown fields

The PyInt_FromLong() conversion function will truncate 64 bit integer
values on 32 bit architectures. We will now use the PyLong_* functions
with the appropriate minimum size for each field.

Note that this commit also switches to the unsigned versions, since the
unknown integer fields have been declared unsigned anyway.

Fixes #6205
This commit is contained in:
Timo Röhling 2019-12-16 16:21:41 +01:00
parent 8b69867fef
commit 5100be2b77

View File

@ -274,13 +274,13 @@ static PyObject* GetData(PyUnknownFieldRef* self, void *closure) {
PyObject* data = NULL; PyObject* data = NULL;
switch (field->type()) { switch (field->type()) {
case UnknownField::TYPE_VARINT: case UnknownField::TYPE_VARINT:
data = PyLong_FromLong(field->varint()); data = PyLong_FromUnsignedLongLong(field->varint());
break; break;
case UnknownField::TYPE_FIXED32: case UnknownField::TYPE_FIXED32:
data = PyLong_FromLong(field->fixed32()); data = PyLong_FromUnsignedLong(field->fixed32());
break; break;
case UnknownField::TYPE_FIXED64: case UnknownField::TYPE_FIXED64:
data = PyLong_FromLong(field->fixed64()); data = PyLong_FromUnsignedLongLong(field->fixed64());
break; break;
case UnknownField::TYPE_LENGTH_DELIMITED: case UnknownField::TYPE_LENGTH_DELIMITED:
data = PyBytes_FromStringAndSize(field->length_delimited().data(), data = PyBytes_FromStringAndSize(field->length_delimited().data(),