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

Rename StaticArray#to_slice to #to_unsafe_slice #14613

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented May 23, 2024

StaticArray#to_slice is unsafe because it returns an unprotected pointer to data on the stack. This pointer can easily be overriden which invalidates the slice (see #14610).
Renaming to #to_unsafe_slice highlights the unsafe nature of using the method, and distinguishes it from other #to_slice implementations that are inherently more safe because they return heap pointers.

StaticArray#to_slice becomes deprecated.

The first commit replaces StaticArray where it is used in the spec suite as an example for the implementation of #to_slice. Using a String literal with the same payload seems like a good idea. It even simplifies those specs.

There are quite a lot of calls to StaticArray#to_slice in the standard library and spec suite. All of them get replaced. It's interesting to note that they're all explicit calls to StaticArray#to_slice and do not involve dynamic dispatch to some other #to_slice method. This makes rewriting easy and confirms that having StaticArray break from the unified #to_slice interface does not cause any practical problems. Calls to StaticArray#to_slice are typically unrelated to calls to other #to_slice methods.

Replaces part of #11031 and resolves part of #9606

Depends on #14615

(0...5).each { |i| array[i] = 1_u8 }
assert_prints base64_encode(array), "AQEBAQE=\n"
assert_prints base64_strict_encode(array), "AQEBAQE="
it "encodes calling `#to_slice`" do
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test was check that base64 can be called with StaticArray, why remove?

Copy link
Member Author

Choose a reason for hiding this comment

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

I interpret the intention of this spec to test that it works with a value that responds to #to_slice. StaticArray was just chosen as an example with a useful #to_slice method. This change respect that intention by replacing StaticArray with String.

I suppose we could add another spec to ensure StaticArray specifically keeps working as well. However this is only going to work as long as deprecated StaticArray#to_slice stays available. I don't think it makes sense to add an explicit overload StaticArray because passing the entire instance as argument is quite heavy and it would be easier to pass a reference, i.e. call to_unsafe_slice at the call site.

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

I'm gonna have to patch a good number of shards, but 👍

@straight-shoota
Copy link
Member Author

@ysbaddaden Yeah it would be super useful to a semantically aware tool to automatically rewrite calls to deprecated methods 🤔

@straight-shoota
Copy link
Member Author

Interpreter specs are broken due to #14615

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.

3 participants