Skip to content

Commit 994c68f

Browse files
bpo-40998: Address compiler warnings found by ubsan (pythonGH-20929)
Signed-off-by: Christian Heimes <christian@python.org> Automerge-Triggered-By: GH:tiran (cherry picked from commit 07f2ade) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 802ff7c commit 994c68f

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Addressed three compiler warnings found by undefined behavior sanitizer
2+
(ubsan).

Objects/unicodeobject.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,11 @@ xmlcharrefreplace(_PyBytesWriter *writer, char *str,
847847

848848
/* generate replacement */
849849
for (i = collstart; i < collend; ++i) {
850-
str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
850+
size = sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
851+
if (size < 0) {
852+
return NULL;
853+
}
854+
str += size;
851855
}
852856
return str;
853857
}

Parser/pegen/parse_string.c

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
7474
return NULL;
7575
}
7676
p = buf = PyBytes_AsString(u);
77+
if (p == NULL) {
78+
return NULL;
79+
}
7780
end = s + len;
7881
while (s < end) {
7982
if (*s == '\\') {

Python/pylifecycle.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,6 @@ Py_FinalizeEx(void)
13531353

13541354
/* Get current thread state and interpreter pointer */
13551355
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1356-
PyInterpreterState *interp = tstate->interp;
13571356

13581357
// Wrap up existing "threading"-module-created, non-daemon threads.
13591358
wait_for_thread_shutdown(tstate);
@@ -1376,13 +1375,13 @@ Py_FinalizeEx(void)
13761375
/* Copy the core config, PyInterpreterState_Delete() free
13771376
the core config memory */
13781377
#ifdef Py_REF_DEBUG
1379-
int show_ref_count = interp->config.show_ref_count;
1378+
int show_ref_count = tstate->interp->config.show_ref_count;
13801379
#endif
13811380
#ifdef Py_TRACE_REFS
1382-
int dump_refs = interp->config.dump_refs;
1381+
int dump_refs = tstate->interp->config.dump_refs;
13831382
#endif
13841383
#ifdef WITH_PYMALLOC
1385-
int malloc_stats = interp->config.malloc_stats;
1384+
int malloc_stats = tstate->interp->config.malloc_stats;
13861385
#endif
13871386

13881387
/* Remaining daemon threads will automatically exit

0 commit comments

Comments
 (0)