NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/57095: libedit's replace_history_entry function has a small memory leak



>Number:         57095
>Category:       lib
>Synopsis:       libedit's replace_history_entry function has a small memory leak
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Dec 02 17:30:00 +0000 2022
>Originator:     Yilei Yang
>Release:        git hash c88d35a3adc2cdd8cb57d37d241a9e979b877581 at https://github.com/NetBSD/src
>Organization:
Google
>Environment:
>Description:
We have an internal build of CPython, and the new changes since Python 3.10 now detects a memory leak in the use of add_history + replace_history_entry here in an ASan build: https://github.com/python/cpython/blob/0563be23a557917228a8b48cbb31bda285a3a815/Modules/readline.c#L1225-L1233

Upon investigation, it appears that the following call in replace_history_entry:

https://github.com/NetBSD/src/blob/3de79feea1982eb1e10c0a3165625b0d0f4c5c32/lib/libedit/readline.c#L1609

copies the `ev.str`, but later `ev.str` is simply overridden by the H_REPLACE operation here:

https://github.com/NetBSD/src/blob/3de79feea1982eb1e10c0a3165625b0d0f4c5c32/lib/libedit/history.c#L1167

Thus the memory previously held by `ev.str` is leaked.
>How-To-Repeat:
I'm not sure how to easily build a CPython runtime in an ASan build that uses libedit, but the issue is exposed by these lines: https://github.com/python/cpython/blob/0563be23a557917228a8b48cbb31bda285a3a815/Modules/readline.c#L1225-L1233
>Fix:
Changing https://github.com/NetBSD/src/blob/3de79feea1982eb1e10c0a3165625b0d0f4c5c32/lib/libedit/readline.c#L1609 from

> he->line = strdup(ev.str);

to

> he->line = ev.str;

should fix the memory leak.



Home | Main Index | Thread Index | Old Index