Skip to content

Commit 78266bf

Browse files
kumaraditya303miss-islington
authored andcommitted
gh-138661: fix data race in PyCode_Addr2Line (GH-138664)
(cherry picked from commit ea26f6d) Co-authored-by: Kumar Aditya <[email protected]>
1 parent 664d17f commit 78266bf

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Include/internal/pycore_code.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ extern void _PyLineTable_InitAddressRange(
274274
/** API for traversing the line number table. */
275275
extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range);
276276
extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
277+
// This is used in dump_frame() in traceback.c without an attached tstate.
278+
extern int _PyCode_Addr2LineNoTstate(PyCodeObject *co, int addr);
277279

278280
/** API for executors */
279281
extern void _PyCode_Clear_Executors(PyCodeObject *code);

Objects/codeobject.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
10141014
******************/
10151015

10161016
int
1017-
PyCode_Addr2Line(PyCodeObject *co, int addrq)
1017+
_PyCode_Addr2LineNoTstate(PyCodeObject *co, int addrq)
10181018
{
10191019
if (addrq < 0) {
10201020
return co->co_firstlineno;
@@ -1028,6 +1028,16 @@ PyCode_Addr2Line(PyCodeObject *co, int addrq)
10281028
return _PyCode_CheckLineNumber(addrq, &bounds);
10291029
}
10301030

1031+
int
1032+
PyCode_Addr2Line(PyCodeObject *co, int addrq)
1033+
{
1034+
int lineno;
1035+
Py_BEGIN_CRITICAL_SECTION(co);
1036+
lineno = _PyCode_Addr2LineNoTstate(co, addrq);
1037+
Py_END_CRITICAL_SECTION();
1038+
return lineno;
1039+
}
1040+
10311041
void
10321042
_PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
10331043
{

Python/traceback.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,8 @@ dump_frame(int fd, _PyInterpreterFrame *frame)
994994
} else {
995995
PUTS(fd, "???");
996996
}
997-
998-
int lineno = PyUnstable_InterpreterFrame_GetLine(frame);
997+
int lasti = PyUnstable_InterpreterFrame_GetLasti(frame);
998+
int lineno = _PyCode_Addr2LineNoTstate(code, lasti);
999999
PUTS(fd, ", line ");
10001000
if (lineno >= 0) {
10011001
_Py_DumpDecimal(fd, (size_t)lineno);

0 commit comments

Comments
 (0)