summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-11-20 08:48:07 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2016-11-20 08:48:07 +0200
commitf16ff20fd73de317a70840fc339f8083c45ea466 (patch)
tree17e8e6412c0b8471ab9666fd61cda0212aa795f0
parentd97f310a9b3525c16d08f763bb3e5ed57bd62251 (diff)
parentb4855ddb5ebfbc737cf9ac20197271250a73625b (diff)
downloadcpython-f16ff20fd73de317a70840fc339f8083c45ea466.tar.gz
Issue #28715: Added error checks for PyUnicode_AsUTF8().
-rw-r--r--Modules/_ctypes/_ctypes.c5
-rw-r--r--Modules/_ctypes/callproc.c4
-rw-r--r--Modules/ossaudiodev.c9
-rw-r--r--Python/ast.c8
-rw-r--r--Python/importdl.c4
5 files changed, 20 insertions, 10 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index b3a95811eb..65c950e4c9 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -728,8 +728,7 @@ PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
return -1;
if (value && PyUnicode_Check(key) &&
- /* XXX struni _PyUnicode_AsString can fail (also in other places)! */
- 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
+ _PyUnicode_EqualToASCIIString(key, "_fields_"))
return PyCStructUnionType_update_stgdict(self, value, 1);
return 0;
}
@@ -743,7 +742,7 @@ UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
return -1;
if (PyUnicode_Check(key) &&
- 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
+ _PyUnicode_EqualToASCIIString(key, "_fields_"))
return PyCStructUnionType_update_stgdict(self, value, 0);
return 0;
}
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index d3044f3af8..7d542fb50d 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1666,7 +1666,9 @@ POINTER(PyObject *self, PyObject *cls)
return result;
}
if (PyUnicode_CheckExact(cls)) {
- char *name = _PyUnicode_AsString(cls);
+ const char *name = PyUnicode_AsUTF8(cls);
+ if (name == NULL)
+ return NULL;
buf = PyMem_Malloc(strlen(name) + 3 + 1);
if (buf == NULL)
return PyErr_NoMemory();
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c
index 4796203e57..5f0505c8a7 100644
--- a/Modules/ossaudiodev.c
+++ b/Modules/ossaudiodev.c
@@ -929,11 +929,14 @@ static PyMethodDef oss_mixer_methods[] = {
static PyObject *
oss_getattro(oss_audio_t *self, PyObject *nameobj)
{
- char *name = "";
+ const char *name = "";
PyObject * rval = NULL;
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ if (PyUnicode_Check(nameobj)) {
+ name = PyUnicode_AsUTF8(nameobj);
+ if (name == NULL)
+ return NULL;
+ }
if (strcmp(name, "closed") == 0) {
rval = (self->fd == -1) ? Py_True : Py_False;
diff --git a/Python/ast.c b/Python/ast.c
index 33b7df64ec..82f4529bf9 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -2118,17 +2118,19 @@ ast_for_atom(struct compiling *c, const node *n)
errtype = "value error";
if (errtype) {
char buf[128];
+ const char *s = NULL;
PyObject *type, *value, *tback, *errstr;
PyErr_Fetch(&type, &value, &tback);
errstr = PyObject_Str(value);
- if (errstr) {
- char *s = _PyUnicode_AsString(errstr);
+ if (errstr)
+ s = PyUnicode_AsUTF8(errstr);
+ if (s) {
PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
- Py_DECREF(errstr);
} else {
PyErr_Clear();
PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
}
+ Py_XDECREF(errstr);
ast_error(c, n, buf);
Py_DECREF(type);
Py_XDECREF(value);
diff --git a/Python/importdl.c b/Python/importdl.c
index ac03289c4d..f56fa94cc4 100644
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -147,6 +147,10 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
/* Package context is needed for single-phase init */
oldcontext = _Py_PackageContext;
_Py_PackageContext = PyUnicode_AsUTF8(name_unicode);
+ if (_Py_PackageContext == NULL) {
+ _Py_PackageContext = oldcontext;
+ goto error;
+ }
m = p0();
_Py_PackageContext = oldcontext;