Skip to content

Error when paths contain NUL characters #29913

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

Merged
merged 3 commits into from
Nov 21, 2015

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Nov 18, 2015

On Windows: Previously these paths were silently truncated at these NUL
characters, now they fail with ErrorKind::InvalidInput.

@rust-highfive
Copy link
Contributor

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@tbu-
Copy link
Contributor Author

tbu- commented Nov 18, 2015

I don't have a Windows machine right now, so this might not compile.

pub fn to_utf16(s: &Path) -> Vec<u16> {
s.as_os_str().encode_wide().chain(Some(0)).collect()
pub fn to_u16s(s: &Path) -> io::Result<Vec<u16>> {
if s.as_os_str().to_inner().to_inner().iter().any(|&b| b == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Could this check the u16 encoded version instead of the wtf-8 encoded version? The inner encoding may actually use a 0 when the u16 later becomes nonzero (I'm not sure).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A feature of the old and new UTF-8 (the old UTF-8 corresponds to our WTF-8) is that all ASCII codepoints and only the ASCII codepoints are represented by the bytes 0 to 127. I can still change that if you want, though.

Copy link
Member

Choose a reason for hiding this comment

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

While that may be true, yeah, it's more robust to check what we're actually returning rather than whatever encoding OsString happens to be in.

@alexcrichton
Copy link
Member

Can you add a test for this as well? Come to think of it I'm not sure we have a test for Unix either, so it may be good to have a test for that as well.

@tbu-
Copy link
Contributor Author

tbu- commented Nov 18, 2015

Could you give me a hint as to where to place the test? In the same file?

@alexcrichton
Copy link
Member

Ah nah I was thinking of a run-pass test which just makes a path with a null in it and make sure an error comes out and it doesn't succeed.

@tbu-
Copy link
Contributor Author

tbu- commented Nov 19, 2015

Updated according to the comment, and added a test that runs on all OSs.

@@ -377,8 +377,13 @@ impl fmt::Debug for File {
}
}

pub fn to_utf16(s: &Path) -> Vec<u16> {
s.as_os_str().encode_wide().chain(Some(0)).collect()
pub fn to_u16s(s: &Path) -> io::Result<Vec<u16>> {
Copy link
Member

Choose a reason for hiding this comment

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

Ah actually I was digging around the other day and ran across another to_utf16 function, could you merge this with that and update the signature there as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@tbu- tbu- force-pushed the pr_windows_path_error_on_nul branch from 81a91dd to 90ab4b5 Compare November 19, 2015 19:08
v.push(0);
v
pub fn to_u16s<S: AsRef<OsStr>>(s: &S) -> io::Result<Vec<u16>> {
fn inner(s: &OsStr) -> io::Result<Vec<u16>> {
Copy link
Member

Choose a reason for hiding this comment

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

In general this pattern isn't necessary for internal functions like this, I don't think there's much savings in terms of compile times for the standard library for example (and this definitely won't affect downstream code)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought this pattern was used to reduce code size, so the outer, essentially no-op function can be inlined, and there are not many (in this case 2) of the monomorphized functions lying around.

Copy link
Member

Choose a reason for hiding this comment

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

Ah yeah this is used for that but in this case it's so small and internal it likely doesn't matter anyway

@alexcrichton
Copy link
Member

Thanks! Can you also squash the commits?

tbu- added 2 commits November 19, 2015 20:05
On Windows: Previously these paths were silently truncated at these NUL
characters, now they fail with `ErrorKind::InvalidInput`.
@tbu- tbu- force-pushed the pr_windows_path_error_on_nul branch from 90ab4b5 to bec42cf Compare November 19, 2015 20:06
@tbu-
Copy link
Contributor Author

tbu- commented Nov 19, 2015

Squashed into meaningful commits. Does "can you squash the commits?" generally mean: Squash into one commit or into more meaningful commits?

@alexcrichton
Copy link
Member

@bors: r+ bec42cf7b40ac7abe0ff578ae76e4bea18072698

Ah yeah I'm personally always fine with just "squash away the temporary commits" moreso than "squash into one"

@bors
Copy link
Collaborator

bors commented Nov 19, 2015

⌛ Testing commit bec42cf with merge ca68a3b...

@bors
Copy link
Collaborator

bors commented Nov 19, 2015

💔 Test failed - auto-win-msvc-64-opt

@tbu- tbu- force-pushed the pr_windows_path_error_on_nul branch from bec42cf to ee30f41 Compare November 20, 2015 11:25
@tbu-
Copy link
Contributor Author

tbu- commented Nov 20, 2015

Fixed the compilation errors.

@alexcrichton
Copy link
Member

@bors: r+ ee30f4127df0bb58d55d6f3af16abedc150875cd

@bors
Copy link
Collaborator

bors commented Nov 20, 2015

⌛ Testing commit ee30f41 with merge d7701a7...

@bors
Copy link
Collaborator

bors commented Nov 20, 2015

💔 Test failed - auto-win-msvc-32-opt

@tbu- tbu- force-pushed the pr_windows_path_error_on_nul branch from ee30f41 to 7386eae Compare November 20, 2015 20:28
@tbu-
Copy link
Contributor Author

tbu- commented Nov 20, 2015

Added a type annotation to help the type checker.

@alexcrichton
Copy link
Member

@bors: r+ 7386eae

@bors
Copy link
Collaborator

bors commented Nov 20, 2015

⌛ Testing commit 7386eae with merge d901e1a...

@bors
Copy link
Collaborator

bors commented Nov 20, 2015

💔 Test failed - auto-win-msvc-64-opt

@tbu- tbu- force-pushed the pr_windows_path_error_on_nul branch from 7386eae to 2cdd0e9 Compare November 20, 2015 22:12
@tbu-
Copy link
Contributor Author

tbu- commented Nov 20, 2015

Removed unused import vec::Vec.

@alexcrichton
Copy link
Member

@bors: r+ 2cdd0e9

@bors
Copy link
Collaborator

bors commented Nov 20, 2015

⌛ Testing commit 2cdd0e9 with merge b0d1f6d...

@bors
Copy link
Collaborator

bors commented Nov 21, 2015

💔 Test failed - auto-win-gnu-64-opt

This check is necessary, because the underlying API only reads strings
until the first NUL.
@tbu- tbu- force-pushed the pr_windows_path_error_on_nul branch from 2cdd0e9 to 71dccf8 Compare November 21, 2015 01:11
@tbu-
Copy link
Contributor Author

tbu- commented Nov 21, 2015

Reintroduce vec::Vec import in test.

@alexcrichton
Copy link
Member

@bors: r+ 71dccf8

@bors
Copy link
Collaborator

bors commented Nov 21, 2015

⌛ Testing commit 71dccf8 with merge d1c7a93...

bors added a commit that referenced this pull request Nov 21, 2015
On Windows: Previously these paths were silently truncated at these NUL
characters, now they fail with `ErrorKind::InvalidInput`.
@bors bors merged commit 71dccf8 into rust-lang:master Nov 21, 2015
@tbu-
Copy link
Contributor Author

tbu- commented Nov 21, 2015

@alexcrichton Thanks for your patience!

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.

4 participants