diff options
author | Colin Decker <cgdecker@google.com> | 2013-10-03 22:09:01 -0400 |
---|---|---|
committer | Colin Decker <cgdecker@google.com> | 2013-10-03 22:09:01 -0400 |
commit | 52adb65169b24defa7e19b824dff3aa3ff6e7db0 (patch) | |
tree | 515e31789f7313770830269baea5defa4bf841c5 /jimfs | |
parent | db8b45d9924867522fd095c169b911b848972d2b (diff) | |
download | jimfs-52adb65169b24defa7e19b824dff3aa3ff6e7db0.tar.gz |
Make \0 character illegal in Unix paths.
Diffstat (limited to 'jimfs')
-rw-r--r-- | jimfs/src/main/java/com/google/jimfs/path/UnixPathType.java | 12 | ||||
-rw-r--r-- | jimfs/src/test/java/com/google/jimfs/path/UnixPathTypeTest.java | 20 |
2 files changed, 32 insertions, 0 deletions
diff --git a/jimfs/src/main/java/com/google/jimfs/path/UnixPathType.java b/jimfs/src/main/java/com/google/jimfs/path/UnixPathType.java index b7578f1..7da16be 100644 --- a/jimfs/src/main/java/com/google/jimfs/path/UnixPathType.java +++ b/jimfs/src/main/java/com/google/jimfs/path/UnixPathType.java @@ -19,6 +19,8 @@ package com.google.jimfs.path; import static com.google.common.base.Preconditions.checkArgument; import static com.google.jimfs.path.CaseSensitivity.CASE_SENSITIVE; +import java.nio.file.InvalidPathException; + import javax.annotation.Nullable; /** @@ -48,10 +50,20 @@ final class UnixPathType extends PathType { return emptyPath(); } + checkValid(path); + String root = path.startsWith("/") ? "/" : null; return new ParseResult(root, splitter().split(path)); } + private static void checkValid(String path) { + for (int i = 0; i < path.length(); i++) { + if (path.charAt(i) == '\0') { + throw new InvalidPathException(path, "nul character not allowed", i); + } + } + } + @Override public String toString(@Nullable String root, Iterable<String> names) { StringBuilder builder = new StringBuilder(); diff --git a/jimfs/src/test/java/com/google/jimfs/path/UnixPathTypeTest.java b/jimfs/src/test/java/com/google/jimfs/path/UnixPathTypeTest.java index ffab419..085a199 100644 --- a/jimfs/src/test/java/com/google/jimfs/path/UnixPathTypeTest.java +++ b/jimfs/src/test/java/com/google/jimfs/path/UnixPathTypeTest.java @@ -20,6 +20,8 @@ import static com.google.jimfs.path.CaseSensitivity.CASE_SENSITIVE; import static com.google.jimfs.path.PathTypeTest.assertParseResult; import static com.google.jimfs.path.PathTypeTest.assertUriRoundTripsCorrectly; import static com.google.jimfs.path.PathTypeTest.fileSystemUri; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import static org.truth0.Truth.ASSERT; import com.google.common.collect.ImmutableList; @@ -27,6 +29,7 @@ import com.google.common.collect.ImmutableList; import org.junit.Test; import java.net.URI; +import java.nio.file.InvalidPathException; /** * Tests for {@link UnixPathType}. @@ -81,4 +84,21 @@ public class UnixPathTypeTest { assertUriRoundTripsCorrectly(PathType.unix(), "/foo bar/"); assertUriRoundTripsCorrectly(PathType.unix(), "/foo bar/baz/one"); } + + @Test + public void testUnix_illegalCharacters() { + try { + PathType.unix().parsePath("/foo/bar\0"); + fail(); + } catch (InvalidPathException expected) { + assertEquals(8, expected.getIndex()); + } + + try { + PathType.unix().parsePath("/\u00001/foo"); + fail(); + } catch (InvalidPathException expected) { + assertEquals(1, expected.getIndex()); + } + } } |