Skip to content

Protect Box.unbox from dereferencing null pointer#16514

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
straight-shoota:feat/box.unbox-null
Dec 20, 2025
Merged

Protect Box.unbox from dereferencing null pointer#16514
straight-shoota merged 2 commits intocrystal-lang:masterfrom
straight-shoota:feat/box.unbox-null

Conversation

@straight-shoota
Copy link
Member

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.
This is low hanging fruit for a bit more implicit memory safety.

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.
@straight-shoota straight-shoota self-assigned this Dec 17, 2025
@straight-shoota straight-shoota changed the title Protect Box.unbox from dereferncing null pointer Protect Box.unbox from dereferencing null pointer Dec 17, 2025
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
```

Copy link
Member Author

@straight-shoota straight-shoota Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ysbaddaden added this to the 1.19.0 milestone Dec 18, 2025
@straight-shoota straight-shoota merged commit ae4cbdb into crystal-lang:master Dec 20, 2025
40 of 41 checks passed
@straight-shoota straight-shoota deleted the feat/box.unbox-null branch December 20, 2025 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants