diff --git a/extensions/python/xpcom/src/PyGInputStream.cpp b/extensions/python/xpcom/src/PyGInputStream.cpp index 822dc580ee4..078e2f6512c 100644 --- a/extensions/python/xpcom/src/PyGInputStream.cpp +++ b/extensions/python/xpcom/src/PyGInputStream.cpp @@ -103,18 +103,23 @@ PyG_nsIInputStream::Read(char * buf, PRUint32 count, PRUint32 *_retval) const char *methodName = "read"; nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "i", count); if (NS_SUCCEEDED(nr)) { - PRUint32 py_size; + Py_ssize_t py_size; const void *py_buf; - if (PyObject_AsReadBuffer(ret, &py_buf, (int *)&py_size)!=0) { + if (PyObject_AsReadBuffer(ret, &py_buf, &py_size)!=0) { PyErr_Format(PyExc_TypeError, "nsIInputStream::read() method must return a buffer object - not a '%s' object", ret->ob_type->tp_name); nr = HandleNativeGatewayError(methodName); } else { - if (py_size > count) { - PyXPCOM_LogWarning("nsIInputStream::read() was asked for %d bytes, but the string returned is %d bytes - truncating!\n", count, py_size); - py_size = count; + if ( (py_size & 0xFFFFFFFF) != py_size) { + PyErr_SetString(PyExc_RuntimeError, "Python Buffer length overflows 32-bit in PyObject_AsWriteBuffer"); + nr = HandleNativeGatewayError(methodName); + } else { + if (py_size > count) { + PyXPCOM_LogWarning("nsIInputStream::read() was asked for %d bytes, but the string returned is %d bytes - truncating!\n", count, py_size); + py_size = count; + } + memcpy(buf, py_buf, py_size); + *_retval = py_size; } - memcpy(buf, py_buf, py_size); - *_retval = py_size; } } return nr; diff --git a/extensions/python/xpcom/src/PyIInputStream.cpp b/extensions/python/xpcom/src/PyIInputStream.cpp index 67bdc549807..8e90b6b71e7 100644 --- a/extensions/python/xpcom/src/PyIInputStream.cpp +++ b/extensions/python/xpcom/src/PyIInputStream.cpp @@ -65,12 +65,19 @@ static PyObject *DoPyRead_Buffer(nsIInputStream *pI, PyObject *obBuffer, PRUint3 { PRUint32 nread; void *buf; - PRUint32 buf_len; - if (PyObject_AsWriteBuffer(obBuffer, &buf, (int *)&buf_len) != 0) { + Py_ssize_t buf_len; + if (PyObject_AsWriteBuffer(obBuffer, &buf, &buf_len) != 0) { PyErr_Clear(); PyErr_SetString(PyExc_TypeError, "The buffer object does not have a write buffer!"); return NULL; } + + if ( (buf_len & 0xFFFFFFFF) != buf_len) { + PyErr_Clear(); + PyErr_SetString(PyExc_RuntimeError, "Python Buffer length overflows 32-bit in PyObject_AsWriteBuffer"); + return NULL; + } + if (n==(PRUint32)-1) { n = buf_len; } else { @@ -116,11 +123,17 @@ static PyObject *DoPyRead_Size(nsIInputStream *pI, PRUint32 n) rc = PyBuffer_New(nread); if (rc != NULL) { void *ob_buf; - PRUint32 buf_len; - if (PyObject_AsWriteBuffer(rc, &ob_buf, (int *)&buf_len) != 0) { + Py_ssize_t buf_len; + if (PyObject_AsWriteBuffer(rc, &ob_buf, &buf_len) != 0) { // should never fail - we just created it! return NULL; } + + if ( (buf_len & 0xFFFFFFFF) != buf_len) { + PyErr_SetString(PyExc_RuntimeError, "Python Buffer length overflows 32-bit in PyObject_AsWriteBuffer"); + return NULL; + } + if (buf_len != nread) { PyErr_SetString(PyExc_RuntimeError, "New buffer isn't the size we created it!"); return NULL; diff --git a/extensions/python/xpcom/src/PyXPCOM.h b/extensions/python/xpcom/src/PyXPCOM.h index e5cc3be18de..1a879dd383c 100644 --- a/extensions/python/xpcom/src/PyXPCOM.h +++ b/extensions/python/xpcom/src/PyXPCOM.h @@ -78,6 +78,12 @@ #include +// python 2.4 doesn't have Py_ssize_t +// => fallback to int +#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) +typedef int Py_ssize_t; +#endif + // PYXPCOM_EXPORT means 'exported from the pyxpcom core lib' - which changes // spelling depending on whether pyxpcom is being built or just referenced. #ifdef BUILD_PYXPCOM