Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e209739

Browse files
Issue #28715: Added error checks for PyUnicode_AsUTF8().
2 parents 3c38e06 + 144f77a commit e209739

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
@@ -728,8 +728,7 @@ PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
728728
return -1;
729729

730730
if (value && PyUnicode_Check(key) &&
731-
/* XXX struni _PyUnicode_AsString can fail (also in other places)! */
732-
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
731+
_PyUnicode_EqualToASCIIString(key, "_fields_"))
733732
return PyCStructUnionType_update_stgdict(self, value, 1);
734733
return 0;
735734
}
@@ -743,7 +742,7 @@ UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
743742
return -1;
744743

745744
if (PyUnicode_Check(key) &&
746-
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
745+
_PyUnicode_EqualToASCIIString(key, "_fields_"))
747746
return PyCStructUnionType_update_stgdict(self, value, 0);
748747
return 0;
749748
}

Modules/_ctypes/callproc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,9 @@ POINTER(PyObject *self, PyObject *cls)
16661666
return result;
16671667
}
16681668
if (PyUnicode_CheckExact(cls)) {
1669-
char *name = _PyUnicode_AsString(cls);
1669+
const char *name = PyUnicode_AsUTF8(cls);
1670+
if (name == NULL)
1671+
return NULL;
16701672
buf = PyMem_Malloc(strlen(name) + 3 + 1);
16711673
if (buf == NULL)
16721674
return PyErr_NoMemory();

Modules/ossaudiodev.c

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

935-
if (PyUnicode_Check(nameobj))
936-
name = _PyUnicode_AsString(nameobj);
935+
if (PyUnicode_Check(nameobj)) {
936+
name = PyUnicode_AsUTF8(nameobj);
937+
if (name == NULL)
938+
return NULL;
939+
}
937940

938941
if (strcmp(name, "closed") == 0) {
939942
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
@@ -2118,17 +2118,19 @@ ast_for_atom(struct compiling *c, const node *n)
21182118
errtype = "value error";
21192119
if (errtype) {
21202120
char buf[128];
2121+
const char *s = NULL;
21212122
PyObject *type, *value, *tback, *errstr;
21222123
PyErr_Fetch(&type, &value, &tback);
21232124
errstr = PyObject_Str(value);
2124-
if (errstr) {
2125-
char *s = _PyUnicode_AsString(errstr);
2125+
if (errstr)
2126+
s = PyUnicode_AsUTF8(errstr);
2127+
if (s) {
21262128
PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
2127-
Py_DECREF(errstr);
21282129
} else {
21292130
PyErr_Clear();
21302131
PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
21312132
}
2133+
Py_XDECREF(errstr);
21322134
ast_error(c, n, buf);
21332135
Py_DECREF(type);
21342136
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)