Skip to content

[3.7] bpo-33312: Fix clang ubsan out of bounds warnings in dict. (GH-6537) #6543

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

Merged
merged 1 commit into from
Apr 20, 2018

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Apr 20, 2018

Fix clang ubsan (undefined behavior sanitizer) warnings in dictobject.c by
adjusting how the internal struct _dictkeysobject shared keys structure is
declared.

This remains ABI compatible. We get rid of the union at the end of the
struct being used for conveinence to avoid typecasting in favor of char[]
variable length array at the end of a struct. This is known to clang to be
used for variable sized objects and will not cause an undefined behavior
problem. Similarly, char arrays do not have strict aliasing undefined
behavior when cast.

PEP-007 does not currently list variable length arrays (VLAs) as allowed
in our subset of C99. If this turns out to be a problem, the fix to this is
to change the char dk_indices[] into dk_indices[1] and restore the
three size computation subtractions this change removes:
- Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)

If this works as is I'll make a separate PR to update PEP-007.
(cherry picked from commit 397f1b2)

Co-authored-by: Gregory P. Smith greg@krypto.org

https://bugs.python.org/issue33312

…6537)

Fix clang ubsan (undefined behavior sanitizer) warnings in dictobject.c by
adjusting how the internal struct _dictkeysobject shared keys structure is
declared.

This remains ABI compatible.  We get rid of the union at the end of the
struct being used for conveinence to avoid typecasting in favor of char[]
variable length array at the end of a struct. This is known to clang to be
used for variable sized objects and will not cause an undefined behavior
problem.  Similarly, char arrays do not have strict aliasing undefined
behavior when cast.

PEP-007 does not currently list variable length arrays (VLAs) as allowed
in our subset of C99.  If this turns out to be a problem, the fix to this is
to change the char `dk_indices[]` into `dk_indices[1]` and restore the
three size computation subtractions this change removes:
  `- Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)`

If this works as is I'll make a separate PR to update PEP-007.
(cherry picked from commit 397f1b2)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
@gpshead
Copy link
Member

gpshead commented Apr 20, 2018

I'm going to wait and watch the buildbots on master before merging this just in case anything has a problem with the variable length array (doubtful these days).

@gpshead gpshead self-assigned this Apr 20, 2018
@miss-islington
Copy link
Contributor Author

@gpshead: Backport status check is done, and it's a success ✅ .

1 similar comment
@miss-islington
Copy link
Contributor Author

@gpshead: Backport status check is done, and it's a success ✅ .

@gpshead gpshead merged commit 392520b into python:3.7 Apr 20, 2018
@miss-islington
Copy link
Contributor Author

Thanks, @gpshead!

@miss-islington miss-islington deleted the backport-397f1b2-3.7 branch April 20, 2018 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants