Commit c00eb73a authored by Gregory P. Smith's avatar Gregory P. Smith

Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,

PyBytes_FromStringAndSize or PyUnicode_FromStringAndSize.  [issue2587]
parent f1083200
...@@ -161,6 +161,11 @@ PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size) ...@@ -161,6 +161,11 @@ PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size)
Py_ssize_t alloc; Py_ssize_t alloc;
assert(size >= 0); assert(size >= 0);
if (size < 0) {
PyErr_SetString(PyExc_SystemError,
"Negative size passed to PyBytes_FromStringAndSize");
return NULL;
}
new = PyObject_New(PyBytesObject, &PyBytes_Type); new = PyObject_New(PyBytesObject, &PyBytes_Type);
if (new == NULL) if (new == NULL)
......
...@@ -56,6 +56,11 @@ PyString_FromStringAndSize(const char *str, Py_ssize_t size) ...@@ -56,6 +56,11 @@ PyString_FromStringAndSize(const char *str, Py_ssize_t size)
{ {
register PyStringObject *op; register PyStringObject *op;
assert(size >= 0); assert(size >= 0);
if (size < 0) {
PyErr_SetString(PyExc_SystemError,
"Negative size passed to PyString_FromStringAndSize");
return NULL;
}
if (size == 0 && (op = nullstring) != NULL) { if (size == 0 && (op = nullstring) != NULL) {
#ifdef COUNT_ALLOCS #ifdef COUNT_ALLOCS
null_strings++; null_strings++;
......
...@@ -465,6 +465,14 @@ PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, ...@@ -465,6 +465,14 @@ PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u,
PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
{ {
PyUnicodeObject *unicode; PyUnicodeObject *unicode;
assert(size <= 0);
if (size < 0) {
PyErr_SetString(PyExc_SystemError,
"Negative size passed to PyUnicode_FromStringAndSize");
return NULL;
}
/* If the Unicode data is known at construction time, we can apply /* If the Unicode data is known at construction time, we can apply
some optimizations which share commonly used objects. some optimizations which share commonly used objects.
Also, this means the input must be UTF-8, so fall back to the Also, this means the input must be UTF-8, so fall back to the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment