Skip to content

Commit 144f77a

Browse files
Issue python#28715: Added error checks for PyUnicode_AsUTF8().
1 parent 93ff872 commit 144f77a

5 files changed

Lines changed: 20 additions & 10 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,7 @@ PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
734734
return -1;
735735

736736
if (value && PyUnicode_Check(key) &&
737-
/* XXX struni _PyUnicode_AsString can fail (also in other places)! */
738-
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
737+
_PyUnicode_EqualToASCIIString(key, "_fields_"))
739738
return PyCStructUnionType_update_stgdict(self, value, 1);
740739
return 0;
741740
}
@@ -749,7 +748,7 @@ UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
749748
return -1;
750749

751750
if (PyUnicode_Check(key) &&
752-
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
751+
_PyUnicode_EqualToASCIIString(key, "_fields_"))
753752
return PyCStructUnionType_update_stgdict(self, value, 0);
754753
return 0;
755754
}

Modules/_ctypes/callproc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,9 @@ POINTER(PyObject *self, PyObject *cls)
16701670
return result;
16711671
}
16721672
if (PyUnicode_CheckExact(cls)) {
1673-
char *name = _PyUnicode_AsString(cls);
1673+
const char *name = PyUnicode_AsUTF8(cls);
1674+
if (name == NULL)
1675+
return NULL;
16741676
buf = PyMem_Malloc(strlen(name) + 3 + 1);
16751677
if (buf == NULL)
16761678
return PyErr_NoMemory();

Modules/ossaudiodev.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,11 +925,14 @@ static PyMethodDef oss_mixer_methods[] = {
925925
static PyObject *
926926
oss_getattro(oss_audio_t *self, PyObject *nameobj)
927927
{
928-
char *name = "";
928+
const char *name = "";
929929
PyObject * rval = NULL;
930930

931-
if (PyUnicode_Check(nameobj))
932-
name = _PyUnicode_AsString(nameobj);
931+
if (PyUnicode_Check(nameobj)) {
932+
name = PyUnicode_AsUTF8(nameobj);
933+
if (name == NULL)
934+
return NULL;
935+
}
933936

934937
if (strcmp(name, "closed") == 0) {
935938
rval = (self->fd == -1) ? Py_True : Py_False;

Python/ast.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,16 +2024,18 @@ ast_for_atom(struct compiling *c, const node *n)
20242024
errtype = "value error";
20252025
if (errtype) {
20262026
char buf[128];
2027+
const char *s = NULL;
20272028
PyObject *type, *value, *tback, *errstr;
20282029
PyErr_Fetch(&type, &value, &tback);
20292030
errstr = PyObject_Str(value);
2030-
if (errstr) {
2031-
char *s = _PyUnicode_AsString(errstr);
2031+
if (errstr)
2032+
s = PyUnicode_AsUTF8(errstr);
2033+
if (s) {
20322034
PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
2033-
Py_DECREF(errstr);
20342035
} else {
20352036
PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
20362037
}
2038+
Py_XDECREF(errstr);
20372039
ast_error(c, n, buf);
20382040
Py_DECREF(type);
20392041
Py_XDECREF(value);

Python/importdl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
147147
/* Package context is needed for single-phase init */
148148
oldcontext = _Py_PackageContext;
149149
_Py_PackageContext = PyUnicode_AsUTF8(name_unicode);
150+
if (_Py_PackageContext == NULL) {
151+
_Py_PackageContext = oldcontext;
152+
goto error;
153+
}
150154
m = p0();
151155
_Py_PackageContext = oldcontext;
152156

0 commit comments

Comments
 (0)