-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash in bytes constructor with mutating list #79154
Comments
Constructing bytes from mutating list can cause a crash. class X:
def __index__(self):
if len(a) < 1000:
a.append(self)
return 0
a = [X()]
bytes(a) This is not an issue about weird integer-like objects. It is about . The size of the list can be changed in other thread while iterate it in the bytes constructor. The optimization for the case of the list argument was added in bpo-6688. The code was refactored several times since, but this flaw was always. |
I have two options for solving this result.
Of course there is an option of removing this optimization at all. But bytes([x]) and bytes((x,)) are common cases. |
I carefully read both the two PRs. The first one, easy to understand. The second one, I spend some time to figure out why the test doesn't crash, why we need to have reference count checks in two places and make some experiments to test in different cases, how the reference counts will be. I am afraid I have to repeat this procedure after some time when reading the code again. :-( And while in some cases the second approach increases performance. But in others cases it might hurt. Codes storing the array in a variable will go into the iterator branch. |
PR 9841 looks good to me. I wouldn't worry about the performance hit. |
Thank you Xiang and Alexandre. I'll merge PR 9841. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: