Skip to content
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

Compiler: fix sizeof Nil and other empty types #8040

Merged
merged 1 commit into from
Aug 4, 2019

Conversation

asterite
Copy link
Member

@asterite asterite commented Aug 4, 2019

Fixes #7644

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Aug 4, 2019
@asterite
Copy link
Member Author

asterite commented Aug 4, 2019

I forgot to explain this about more: Pointer(T).malloc is implemented as a primitive, and there we delegate the allocated size to LLVM using its sizeof function, which for Nil (and other "empty" types returns 0). So mallocing a Nil pointer always asks for 0 memory. But when doing Pointer#clear we use our sizeof, which calls LLVM's once and then would change it to 1 if it's zero. That caused a segfault trying to access something past that pointer. I guess I made it like that for the Pointer(Void) and sizeof(Void) case, but I generalized too much.

I'm still not sure about us being able to do Pointer(Void) because it's just so it looks like what you can do with C, but at least this fixes the issue without introducing any breaking change.

@asterite asterite merged commit 337d1eb into crystal-lang:master Aug 4, 2019
@asterite asterite deleted the bug/sizeof-nil branch August 4, 2019 20:23
@asterite asterite added this to the 0.31.0 milestone Aug 4, 2019
@bcardiff bcardiff modified the milestones: 0.31.0, 0.30.1 Aug 8, 2019
dnamsons pushed a commit to dnamsons/crystal that referenced this pull request Jan 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shifting a Deque(Nil) more times than its initial capacity crashes the program
3 participants