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

Change Strings in SIP crate to PathBufs/OsStrings #2490

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gememma
Copy link
Member

@gememma gememma commented Jun 3, 2024

Closes #2198

This results in some lossy conversions where the SIP code is called from other crates which could be eliminated if all the Strings in the whole codebase were evaluated - there are plenty that would make more sense as OsStrings or Paths - but that would be a much larger issue. Eventually I think we could probably eliminate all the to_string_lossy() calls and avoid conversion to String altogether.

@gememma gememma force-pushed the sip-crate-strings branch 3 times, most recently from 1433be1 to 5d94b04 Compare June 3, 2024 13:54
@gememma gememma marked this pull request as draft June 3, 2024 15:33
@gememma gememma force-pushed the sip-crate-strings branch 2 times, most recently from d057886 to 677f1d2 Compare June 4, 2024 09:09
@gememma gememma marked this pull request as ready for review June 6, 2024 11:10
@@ -57,7 +57,9 @@ fn update_ptr_from_bypass(ptr: *const c_char, bypass: Bypass) -> *const c_char {
// inside mirrord's temp bin dir. The detour has returned us the original path of the file
// (stripped mirrord's dir path), so now we carry out the operation locally, on the stripped
// path.
Bypass::FileOperationInMirrordBinTempDir(stripped_ptr) => stripped_ptr,
Bypass::FileOperationInMirrordBinTempDir(path_buf) => {
path_buf.as_os_str().as_encoded_bytes().as_ptr() as _
Copy link
Member

Choose a reason for hiding this comment

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

Returning ptr into a PathBuf that goes out of scope here, not good.

Comment on lines 150 to 147
FileOperationInMirrordBinTempDir(*const c_char),
FileOperationInMirrordBinTempDir(PathBuf),
Copy link
Member

Choose a reason for hiding this comment

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

Why do you want to return a new PathBuf here?
Why not the new pointer like before, or just the length of the removed prefix?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't return a pointer into the original String because the switch over to using PathBuf means that operations like stripping a prefix result in a new PathBuf, where before they kept the same String buffer - so the FileOperationInMirrordBinTempDir needs to own the value, otherwise the pointer will be dangling

Copy link
Member

Choose a reason for hiding this comment

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

I disagree. You don't need to return a PathBuf, as all the information the callers (that handle that variant) ever need, is how many bytes of the path to skip (this is equivalent to a new pointer into the original path buffer, either one works). If we still want to create new PathBufs (which I don't think is necessary) while finding out that number, we can still at the end just check the length difference of the old and new PathBufs, and only return that number.

Comment on lines 272 to 326
if let Bypass(FileOperationInMirrordBinTempDir(later_ptr)) = path_buf_detour {
if let Bypass(FileOperationInMirrordBinTempDir(path_buf)) = path_buf_detour {
let later_ptr: *const i8 = path_buf.as_os_str().as_encoded_bytes().as_ptr() as _;
Copy link
Member

Choose a reason for hiding this comment

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

If you change what later_ptr is, you also have to change the code that uses that variable in line 292 and after that, because it does not hold the information it used to hold anymore.

The calculation of prefix_len is now wrong. Making the prefix_len correct would be enough to make the rest of the function correct.

prefix_len is supposed to hold the length of the prefix that should be removed from the path.

It is currently calculated as

            let prefix_len = later_ptr.offset_from(path);

But after the change later_ptr and path are pointers into two different buffers, so taking their offset makes no sense.

If you really want FileOperationInMirrordBinTempDir to hold a PathBuf (which doesn't look convenient to me), then you could calculate prefix_len by reducing the length of path_buf from *buflen.
However I think it would be nicer to either leave the value of that variant a pointer as it was, or hold the prefix length directly (but then you would have to calculate the new pointer in the places in the code where the returned pointer was used as-is).

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 think calculating the length in a new way should be sufficient - I don't believe you can have FileOperationInMirrordBinTempDir contain a meaningful pointer with these changes

@gememma gememma force-pushed the sip-crate-strings branch 2 times, most recently from d39cb61 to 5feabae Compare June 19, 2024 13:33
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.

Improve String handling in SIP crate
2 participants