From 62c99cd96bcb1015636048b0e422827735af0959 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 17 Jun 2020 09:03:27 +0200 Subject: [PATCH 1/2] bpo-40998: Address compiler warnings found by ubsan Signed-off-by: Christian Heimes --- .../next/Build/2020-06-17-09-05-02.bpo-40998.sgqmg9.rst | 2 ++ Objects/unicodeobject.c | 6 +++++- Parser/string_parser.c | 3 +++ Python/pylifecycle.c | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Build/2020-06-17-09-05-02.bpo-40998.sgqmg9.rst diff --git a/Misc/NEWS.d/next/Build/2020-06-17-09-05-02.bpo-40998.sgqmg9.rst b/Misc/NEWS.d/next/Build/2020-06-17-09-05-02.bpo-40998.sgqmg9.rst new file mode 100644 index 00000000000000..c268e4fd0d9cbf --- /dev/null +++ b/Misc/NEWS.d/next/Build/2020-06-17-09-05-02.bpo-40998.sgqmg9.rst @@ -0,0 +1,2 @@ +Addressed three compiler warnings found by undefined behavior sanitizer +(ubsan). diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index e7a63e7b973d89..70688c8c013816 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -839,7 +839,11 @@ xmlcharrefreplace(_PyBytesWriter *writer, char *str, /* generate replacement */ for (i = collstart; i < collend; ++i) { - str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); + size = sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); + if (size < 0) { + return NULL; + } + str += size; } return str; } diff --git a/Parser/string_parser.c b/Parser/string_parser.c index 1285968b319177..8f6433dbcec131 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -69,6 +69,9 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t) return NULL; } p = buf = PyBytes_AsString(u); + if (p == NULL) { + return NULL; + } end = s + len; while (s < end) { if (*s == '\\') { diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 82ce4f15ad283a..174cea6cddfb07 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1644,7 +1644,9 @@ Py_FinalizeEx(void) /* Get current thread state and interpreter pointer */ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); +#if (defined(Py_REF_DEBUG) || defined(Py_TRACE_REFS) || defined(WITH_PYMALLOC)) PyInterpreterState *interp = tstate->interp; +#endif // Wrap up existing "threading"-module-created, non-daemon threads. wait_for_thread_shutdown(tstate); From 851deb9546c08ebd29a4ef1f2c1ba8d9fc0156c5 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 18 Nov 2020 13:16:40 +0100 Subject: [PATCH 2/2] Drop interp var and use tstate->interp in debug code --- Python/pylifecycle.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 174cea6cddfb07..9771951d2d84ca 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1644,9 +1644,6 @@ Py_FinalizeEx(void) /* Get current thread state and interpreter pointer */ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); -#if (defined(Py_REF_DEBUG) || defined(Py_TRACE_REFS) || defined(WITH_PYMALLOC)) - PyInterpreterState *interp = tstate->interp; -#endif // Wrap up existing "threading"-module-created, non-daemon threads. wait_for_thread_shutdown(tstate); @@ -1669,13 +1666,13 @@ Py_FinalizeEx(void) /* Copy the core config, PyInterpreterState_Delete() free the core config memory */ #ifdef Py_REF_DEBUG - int show_ref_count = interp->config.show_ref_count; + int show_ref_count = tstate->interp->config.show_ref_count; #endif #ifdef Py_TRACE_REFS - int dump_refs = interp->config.dump_refs; + int dump_refs = tstate->interp->config.dump_refs; #endif #ifdef WITH_PYMALLOC - int malloc_stats = interp->config.malloc_stats; + int malloc_stats = tstate->interp->config.malloc_stats; #endif /* Remaining daemon threads will automatically exit