Skip to content

Commit a930e52

Browse files
Fix unchecked NULL returns in several C modules (#160213)
1 parent e60a9da commit a930e52

10 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎Modules/_elementtree.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4285,6 +4285,9 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
42854285
if (events_to_report == Py_None) {
42864286
/* default is "end" only */
42874287
self->end_event_obj = PyUnicode_FromString("end");
4288+
if (self->end_event_obj == NULL) {
4289+
return NULL;
4290+
}
42884291
Py_RETURN_NONE;
42894292
}
42904293

‎Modules/_io/winconsoleio.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,10 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
962962

963963
bytes_size += _buflen(self);
964964
bytes = PyBytes_FromStringAndSize(NULL, bytes_size);
965+
if (bytes == NULL) {
966+
PyMem_Free(buf);
967+
return NULL;
968+
}
965969
rn = _copyfrombuf(self, PyBytes_AS_STRING(bytes), bytes_size);
966970

967971
if (len) {

‎Modules/_testinternalcapi.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,9 @@ get_crossinterp_data(PyObject *self, PyObject *args, PyObject *kwargs)
26252625
}
26262626
else {
26272627
mode = PyUnicode_AsUTF8(modeobj);
2628+
if (mode == NULL) {
2629+
return NULL;
2630+
}
26282631
if (strlen(mode) == 0) {
26292632
mode = "xidata";
26302633
}

‎Modules/_xxtestfuzz/fuzzer.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ static int fuzz_sre_match(const char* data, size_t size) {
325325

326326
PyObject* pattern = compiled_patterns[idx];
327327
PyObject* match_callable = PyObject_GetAttrString(pattern, "match");
328+
if (match_callable == NULL) {
329+
Py_DECREF(to_match);
330+
return 0;
331+
}
328332

329333
PyObject* matches = PyObject_CallOneArg(match_callable, to_match);
330334

‎Modules/getpath.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,11 @@ getpath_warn(PyObject *Py_UNUSED(self), PyObject *args)
632632
if (!PyArg_ParseTuple(args, "U", &msgobj)) {
633633
return NULL;
634634
}
635-
fprintf(stderr, "%s\n", PyUnicode_AsUTF8(msgobj));
635+
const char *msg = PyUnicode_AsUTF8(msgobj);
636+
if (msg == NULL) {
637+
return NULL;
638+
}
639+
fprintf(stderr, "%s\n", msg);
636640
Py_RETURN_NONE;
637641
}
638642

‎Modules/posixmodule.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5675,8 +5675,9 @@ os__getvolumepathname_impl(PyObject *module, path_t *path)
56755675
goto exit;
56765676
}
56775677
result = PyUnicode_FromWideChar(mountpath, -1);
5678-
if (PyBytes_Check(path->object))
5678+
if (result != NULL && PyBytes_Check(path->object)) {
56795679
Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
5680+
}
56805681

56815682
exit:
56825683
PyMem_Free(mountpath);

‎Modules/readline.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,10 +1508,19 @@ flex_complete(const char *text, int start, int end)
15081508

15091509
done:
15101510
if (state) {
1511-
Py_XDECREF(state->begidx);
1512-
Py_XDECREF(state->endidx);
1513-
state->begidx = PyLong_FromLong((long) start);
1514-
state->endidx = PyLong_FromLong((long) end);
1511+
PyObject *begidx = PyLong_FromLong((long) start);
1512+
PyObject *endidx = PyLong_FromLong((long) end);
1513+
if (begidx == NULL || endidx == NULL) {
1514+
Py_XDECREF(begidx);
1515+
Py_XDECREF(endidx);
1516+
PyErr_FormatUnraisable("Exception ignored while preparing "
1517+
"readline completion");
1518+
rl_attempted_completion_over = 1;
1519+
PyGILState_Release(gilstate);
1520+
return NULL;
1521+
}
1522+
Py_XSETREF(state->begidx, begidx);
1523+
Py_XSETREF(state->endidx, endidx);
15151524
}
15161525
result = completion_matches((char *)text, *on_completion);
15171526
PyGILState_Release(gilstate);

‎Modules/socketmodule.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,9 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
17861786
PyObject *vmId = PyUnicode_FromWideChar(guidStr, -1);
17871787
res = RpcStringFreeW(&guidStr);
17881788
assert(res == RPC_S_OK);
1789+
if (vmId == NULL) {
1790+
return 0;
1791+
}
17891792

17901793
res = UuidToStringW(&a->ServiceId, &guidStr);
17911794
if (res != RPC_S_OK) {

‎Python/ceval.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,16 @@ dtrace_function_entry(_PyInterpreterFrame *frame)
12251225
int lineno;
12261226

12271227
PyCodeObject *code = _PyFrame_GetCode(frame);
1228+
PyObject *exc = PyErr_GetRaisedException();
12281229
filename = PyUnicode_AsUTF8(code->co_filename);
12291230
funcname = PyUnicode_AsUTF8(code->co_name);
1231+
PyErr_SetRaisedException(exc);
1232+
if (filename == NULL) {
1233+
filename = "<unknown>";
1234+
}
1235+
if (funcname == NULL) {
1236+
funcname = "<unknown>";
1237+
}
12301238
lineno = PyUnstable_InterpreterFrame_GetLine(frame);
12311239

12321240
PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
@@ -1240,8 +1248,16 @@ dtrace_function_return(_PyInterpreterFrame *frame)
12401248
int lineno;
12411249

12421250
PyCodeObject *code = _PyFrame_GetCode(frame);
1251+
PyObject *exc = PyErr_GetRaisedException();
12431252
filename = PyUnicode_AsUTF8(code->co_filename);
12441253
funcname = PyUnicode_AsUTF8(code->co_name);
1254+
PyErr_SetRaisedException(exc);
1255+
if (filename == NULL) {
1256+
filename = "<unknown>";
1257+
}
1258+
if (funcname == NULL) {
1259+
funcname = "<unknown>";
1260+
}
12451261
lineno = PyUnstable_InterpreterFrame_GetLine(frame);
12461262

12471263
PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);

‎Python/crossinterp.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,11 @@ check_missing___main___attr(PyObject *exc)
664664
}
665665
}
666666
const char *err = PyUnicode_AsUTF8(msgobj);
667+
if (err == NULL) {
668+
PyErr_Clear();
669+
Py_DECREF(msgobj);
670+
return 0;
671+
}
667672

668673
// Check if it's a missing __main__ attr.
669674
int cmp = strncmp(err, "module '__main__' has no attribute '", 36);

0 commit comments

Comments
 (0)