Skip to content

Don't assume the size when allocating a CFNumber #977

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
May 11, 2017

Conversation

NobodyNada
Copy link

CoreFoundation assumed the size of a struct __CFNumber (which contains a CFRuntimeBase and an int64_t) would be the size of a CFRuntimeBase plus 8 bytes.

This assumption may be true on some platforms, but on my Raspberry Pi, the size of a CFRuntimeBase was 20 bytes. The compiler inserted four bytes of padding before the int64_t to align it to an 8-byte boundary, causing the structure to take up 32 bytes instead of the 28 bytes the code allocated. Writing to the int64_t overwrote some of malloc's internal bookkeeping information and caused a crash.

I changed the code to use a couple of sizeofs to calculate the extra space to allocate, like the rest of CoreFoundation does (see this search).

CoreFoundation assumed the size of a `struct __CFNumber` (which contains a `CFRuntimeBase` and an `int64_t`) would be the size of a `CFRuntimeBase` plus 8 bytes.

This assumption may be true on some platforms, but on my Raspberry Pi, the size of a `CFRuntimeBase` was 20 bytes.  The compiler inserted four bytes of padding before the `int64_t` to align it to an 8-byte boundary, causing the structure to take up 32 bytes instead of the 28 bytes the code allocated.  Writing to the `int64_t` overwrote some of `malloc`'s internal bookkeeping information and caused a crash.

I changed the code to use a couple of `sizeof`s to calculate the extra space to allocate, like the rest of CoreFoundation does (see [this search](https://github.com/apple/swift-corelibs-foundation/search?utf8=✓&q=_CFRuntimeCreateInstance&type=)).
@parkera
Copy link
Contributor

parkera commented May 11, 2017

Looks good, thanks.

@parkera
Copy link
Contributor

parkera commented May 11, 2017

@swift-ci test and merge

@swift-ci swift-ci merged commit b707ecf into swiftlang:master May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants