diff options
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index 5fc9991..a790e52 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -847,7 +847,7 @@ bu_longlong(const char *p, const formatdef *f) } while (--i > 0); /* Extend the sign bit. */ if (SIZEOF_LONG_LONG > f->size) - x |= -(x & (1L << ((8 * f->size) - 1))); + x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); if (x >= LONG_MIN && x <= LONG_MAX) return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); return PyLong_FromLongLong(x); @@ -1083,7 +1083,7 @@ lu_longlong(const char *p, const formatdef *f) } while (i > 0); /* Extend the sign bit. */ if (SIZEOF_LONG_LONG > f->size) - x |= -(x & (1L << ((8 * f->size) - 1))); + x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); if (x >= LONG_MIN && x <= LONG_MAX) return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); return PyLong_FromLongLong(x); @@ -1532,21 +1532,35 @@ strings."); static PyObject * s_unpack(PyObject *self, PyObject *inputstr) { + char *start; + Py_ssize_t len; + PyObject *args=NULL, *result; PyStructObject *soself = (PyStructObject *)self; assert(PyStruct_Check(self)); assert(soself->s_codes != NULL); - if (inputstr != NULL && PyBytes_Check(inputstr) && - PyBytes_GET_SIZE(inputstr) == soself->s_size) { - return s_unpack_internal(soself, PyBytes_AS_STRING(inputstr)); + if (inputstr == NULL) + goto fail; + if (PyString_Check(inputstr) && + PyString_GET_SIZE(inputstr) == soself->s_size) { + return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); } - if (inputstr == NULL || !PyString_Check(inputstr) || - PyString_GET_SIZE(inputstr) != soself->s_size) { - PyErr_Format(StructError, - "unpack requires a string argument of length %zd", - soself->s_size); + args = PyTuple_Pack(1, inputstr); + if (args == NULL) return NULL; - } - return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); + if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) + goto fail; + if (soself->s_size != len) + goto fail; + result = s_unpack_internal(soself, start); + Py_DECREF(args); + return result; + +fail: + Py_XDECREF(args); + PyErr_Format(StructError, + "unpack requires a string argument of length %zd", + soself->s_size); + return NULL; } PyDoc_STRVAR(s_unpack_from__doc__, |