]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-138661: fix data race in `PyCode_Addr2Line` (GH-138664) (#138834)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 7 Oct 2025 18:06:45 +0000 (20:06 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 18:06:45 +0000 (18:06 +0000)
gh-138661: fix data race in `PyCode_Addr2Line` (GH-138664)
(cherry picked from commit ea26f6da39294b7d3c28873d070a2218bd528b5f)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Include/internal/pycore_code.h
Objects/codeobject.c
Python/traceback.c

index 37a747aa4e3e46a64fd1b45880bd804f60318262..43286774eb2c714f21cd3cf5e90acb981664b4d2 100644 (file)
@@ -274,6 +274,8 @@ extern void _PyLineTable_InitAddressRange(
 /** API for traversing the line number table. */
 extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range);
 extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
+// This is used in dump_frame() in traceback.c without an attached tstate.
+extern int _PyCode_Addr2LineNoTstate(PyCodeObject *co, int addr);
 
 /** API for executors */
 extern void _PyCode_Clear_Executors(PyCodeObject *code);
index 42e021679b583f0b62510b1c8c47ddb04e22cfc1..1dbbb053e3fc4812e4dfaa8e8fdf745e0c8503f8 100644 (file)
@@ -1014,7 +1014,7 @@ failed:
  ******************/
 
 int
-PyCode_Addr2Line(PyCodeObject *co, int addrq)
+_PyCode_Addr2LineNoTstate(PyCodeObject *co, int addrq)
 {
     if (addrq < 0) {
         return co->co_firstlineno;
@@ -1028,6 +1028,16 @@ PyCode_Addr2Line(PyCodeObject *co, int addrq)
     return _PyCode_CheckLineNumber(addrq, &bounds);
 }
 
+int
+PyCode_Addr2Line(PyCodeObject *co, int addrq)
+{
+    int lineno;
+    Py_BEGIN_CRITICAL_SECTION(co);
+    lineno = _PyCode_Addr2LineNoTstate(co, addrq);
+    Py_END_CRITICAL_SECTION();
+    return lineno;
+}
+
 void
 _PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
 {
index c06cb1a59089e29f3eaf6caa09fa1b424f437db2..f3c5644aeb17990bf3dfc0f535cb5adc3543be58 100644 (file)
@@ -994,8 +994,8 @@ dump_frame(int fd, _PyInterpreterFrame *frame)
     } else {
         PUTS(fd, "???");
     }
-
-    int lineno = PyUnstable_InterpreterFrame_GetLine(frame);
+    int lasti = PyUnstable_InterpreterFrame_GetLasti(frame);
+    int lineno = _PyCode_Addr2LineNoTstate(code, lasti);
     PUTS(fd, ", line ");
     if (lineno >= 0) {
         _Py_DumpDecimal(fd, (size_t)lineno);