-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
bytes_concat seems to check overflow using undefined behaviour #71660
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
Comments
bytes_concat uses following code to check overflow: size = va.len + vb.len;
if (size < 0): {
PyErr_NoMemory();
goto done;
} This is wrong since signed ints overflow is undefined bahaviour. But one point is that Python's Makefile defines -fwrapv with gcc and |
This should be fixed. |
Attach a patch to fix this. |
The patch looks correct to me. bpo-1621 is open about the general problem of overflows. |
I changed the versions without checking first. Long story short: Objects/stringobject.c (Python 2 equivalent of Objects/bytesobject.c) is already fixed, but in all versions, Objects/bytearrayobject.c is apparently unfixed. Python 2 was fixed as part of r65335. Python 3 was supposed to be fixed in r66009 (bpo-3627), but it looks like some of the fixes were missed. |
The previous code was perfectly fine with ---- In my opinion the if (PYSSIZE_OVERFLOWS_ON_ADD(va.len, vb.len)) {
PyErr_NoMemory();
goto done;
} size = va.len + vb.len; even though |
Yes, the current code is valid with -fwrapv. But I am not sure if every compiler supports this feature. So maybe we can't totally rely on it. And in bpo-1621, some efforts seem to have worked to factor these out. |
Xiang Zhang, could you please write a patch for bytearray too? |
Of course. I forgot to check bytearray. :( The new patch now also includes bytearray. |
And bytearray_iconcat() please. |
Sorry. v3 now includes iconcat. |
LGTM. |
New changeset dac248056b20 by Serhiy Storchaka in branch '3.5': New changeset de8f0e9196d8 by Serhiy Storchaka in branch 'default': |
Here is a patch for 2.7. It fixes also concatenation and repetition of str and unicode. |
Left a comment. |
New changeset 130d97217e36 by Serhiy Storchaka in branch '2.7': |
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: