Skip to content

SyntaxError.__init__ memory leak on re-initialization #146250

@bkap123

Description

@bkap123

Bug report

Bug description:

This is a sub-issue for #146102 and more information can be found here`.


When re-initializing a SyntaxError (by calling its __init__ method), it currently does not decref its prior values. This reproducer crashes with ASan:

e = SyntaxError("msg", ("file.py", 1, 2, "txt", 2, 3))
e.__init__("new_msg", ("new_file.py", 2, 3, "new_txt", 3, 4))
ASan report

=================================================================
==576215==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 72 byte(s) in 1 object(s) allocated from:
    #0 0x7fb60c520cb5 in malloc (/usr/lib/libasan.so.8+0x120cb5) (BuildId: 0b96d08695bbce2da9d4770c29ad2e72fb536f47)
    #1 0x5600a9805972 in _PyMem_RawMalloc Objects/obmalloc.c:65
    #2 0x5600a9804d63 in _PyMem_DebugRawAlloc Objects/obmalloc.c:3043
    #3 0x5600a9804dc7 in _PyMem_DebugRawMalloc Objects/obmalloc.c:3076
    #4 0x5600a9806748 in _PyMem_DebugMalloc Objects/obmalloc.c:3241
    #5 0x5600a982ecf3 in PyObject_Malloc Objects/obmalloc.c:1649
    #6 0x5600a98cdf45 in PyUnicode_New Objects/unicodeobject.c:1318
    #7 0x5600a98d3471 in unicode_decode_utf8 Objects/unicodeobject.c:5273
    #8 0x5600a98df566 in PyUnicode_DecodeUTF8Stateful Objects/unicodeobject.c:5381
    #9 0x5600a96eeb3b in _PyPegen_decode_string Parser/string_parser.c:245
    #10 0x5600a96ef0b0 in _PyPegen_parse_string Parser/string_parser.c:338
    #11 0x5600a95ce09e in _PyPegen_constant_from_string Parser/action_helpers.c:1480
    #12 0x5600a95e9b7a in string_rule Parser/parser.c:17349
    #13 0x5600a963ed92 in _tmp_156_rule Parser/parser.c:37961
    #14 0x5600a963f435 in _loop1_82_rule Parser/parser.c:33521
    #15 0x5600a96486ab in strings_rule Parser/parser.c:17420
    #16 0x5600a9605530 in atom_rule Parser/parser.c:15361
    #17 0x5600a9607dce in primary_raw Parser/parser.c:14999
    #18 0x5600a96083ed in primary_rule Parser/parser.c:14797
    #19 0x5600a9608da2 in await_primary_rule Parser/parser.c:14751
    #20 0x5600a960934c in power_rule Parser/parser.c:14627
    #21 0x5600a960af00 in factor_rule Parser/parser.c:14577
    #22 0x5600a960d6b4 in term_raw Parser/parser.c:14418
    #23 0x5600a960dcd3 in term_rule Parser/parser.c:14161
    #24 0x5600a960e9e5 in sum_raw Parser/parser.c:14114
    #25 0x5600a960f037 in sum_rule Parser/parser.c:13993
    #26 0x5600a9610687 in shift_expr_raw Parser/parser.c:13953
    #27 0x5600a9610cd9 in shift_expr_rule Parser/parser.c:13813
    #28 0x5600a9611502 in bitwise_and_raw Parser/parser.c:13773
    #29 0x5600a9611b54 in bitwise_and_rule Parser/parser.c:13691

SUMMARY: AddressSanitizer: 72 byte(s) leaked in 1 allocation(s).

The fix is to call Py_XDECREF before replacing the entries in the SyntaxError object.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions