summaryrefslogtreecommitdiff
path: root/Objects/complexobject.c
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2017-02-04 15:05:40 -0800
committerSteve Dower <steve.dower@microsoft.com>2017-02-04 15:05:40 -0800
commitb2fa705fd3887c326e811c418469c784353027f4 (patch)
treeb3428f73de91453edbfd4df1a5d4a212d182eb44 /Objects/complexobject.c
parent134e58fd3aaa2e91390041e143f3f0a21a60142b (diff)
parentb53654b6dbfce8318a7d4d1cdaddca7a7fec194b (diff)
downloadcpython-b2fa705fd3887c326e811c418469c784353027f4.tar.gz
Issue #29392: Prevent crash when passing invalid arguments into msvcrt module.
Diffstat (limited to 'Objects/complexobject.c')
-rw-r--r--Objects/complexobject.c63
1 files changed, 37 insertions, 26 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index d82c5eb9f1..31e12784cc 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -759,29 +759,12 @@ static PyMemberDef complex_members[] = {
};
static PyObject *
-complex_subtype_from_string(PyTypeObject *type, PyObject *v)
+complex_from_string_inner(const char *s, Py_ssize_t len, void *type)
{
- const char *s, *start;
- char *end;
double x=0.0, y=0.0, z;
int got_bracket=0;
- PyObject *s_buffer = NULL;
- Py_ssize_t len;
-
- if (PyUnicode_Check(v)) {
- s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
- if (s_buffer == NULL)
- return NULL;
- s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
- if (s == NULL)
- goto error;
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "complex() argument must be a string or a number, not '%.200s'",
- Py_TYPE(v)->tp_name);
- return NULL;
- }
+ const char *start;
+ char *end;
/* position on first nonblank */
start = s;
@@ -822,7 +805,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
if (PyErr_ExceptionMatches(PyExc_ValueError))
PyErr_Clear();
else
- goto error;
+ return NULL;
}
if (end != s) {
/* all 4 forms starting with <float> land here */
@@ -835,7 +818,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
if (PyErr_ExceptionMatches(PyExc_ValueError))
PyErr_Clear();
else
- goto error;
+ return NULL;
}
if (end != s)
/* <float><signed-float>j */
@@ -890,18 +873,46 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
if (s-start != len)
goto parse_error;
- Py_XDECREF(s_buffer);
- return complex_subtype_from_doubles(type, x, y);
+ return complex_subtype_from_doubles((PyTypeObject *)type, x, y);
parse_error:
PyErr_SetString(PyExc_ValueError,
"complex() arg is a malformed string");
- error:
- Py_XDECREF(s_buffer);
return NULL;
}
static PyObject *
+complex_subtype_from_string(PyTypeObject *type, PyObject *v)
+{
+ const char *s;
+ PyObject *s_buffer = NULL, *result = NULL;
+ Py_ssize_t len;
+
+ if (PyUnicode_Check(v)) {
+ s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
+ if (s_buffer == NULL) {
+ return NULL;
+ }
+ s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
+ if (s == NULL) {
+ goto exit;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "complex() argument must be a string or a number, not '%.200s'",
+ Py_TYPE(v)->tp_name);
+ return NULL;
+ }
+
+ result = _Py_string_to_number_with_underscores(s, len, "complex", v, type,
+ complex_from_string_inner);
+ exit:
+ Py_DECREF(s_buffer);
+ return result;
+}
+
+static PyObject *
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *r, *i, *tmp;