Protect Box.unbox from dereferencing null pointer#16514
Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom Dec 20, 2025
Merged
Protect Box.unbox from dereferencing null pointer#16514straight-shoota merged 2 commits intocrystal-lang:masterfrom
Box.unbox from dereferencing null pointer#16514straight-shoota merged 2 commits intocrystal-lang:masterfrom
Conversation
Unboxing a null pointer leads to invalid memory access when the target type is not nilable. We can avoid this by raising an explicit exception. This makes `Box.unbox` a little bit safer. Of course we can still not protect against any other invalid pointer. But null pointers are easily identifiable as invalid, so this is low hanging fruit.
Box.unbox from dereferncing null pointerBox.unbox from dereferencing null pointer
ysbaddaden
reviewed
Dec 18, 2025
spec/std/box_spec.cr
Outdated
Comment on lines
+64
to
+65
| Box(Int32?).unbox(Pointer(Void).null).should be_nil | ||
| Box(Int32 | String?).unbox(Pointer(Void).null).should be_nil |
Collaborator
There was a problem hiding this comment.
Thought: I'm wondering if these two cases shouldn't raise; they're mixed unions so Box will allocate them in the HEAP, so a NULL pointer when unboxing should be unexpected.
Box(Nil).box(nil) # => Pointer(Void).null
Box(String?).box(nil) # => Pointer(Void).null
Box(Int32?).box(nil) # => Pointer(Void)@0x7ef22cce1900
Box(Int32 | String | Nil).box(nil) # => Pointer(Void)@0x7ef229049140
```
Member
Author
There was a problem hiding this comment.
Yes, that's another option. I actually wanted to mention that, but apparently I forgot 🙈
The intention of a null pointer is clear, so we could interpret as nil even though it's not a correctly typed value. But I suppose raising might be better because it is really an exceptional condition.
ysbaddaden
approved these changes
Dec 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unboxing a null pointer leads to invalid memory access when the target type is not nilable.
We can avoid this by raising an explicit exception. This makes
Box.unboxa little bit safer.Of course we can still not protect against any other invalid pointer. But null pointers are easily identifiable as invalid.
This is low hanging fruit for a bit more implicit memory safety.