From 2ccce395cadf79bc0914b1fd04dd22fed6c7c8fe Mon Sep 17 00:00:00 2001 From: jhorvitz Date: Tue, 16 Feb 2021 16:09:44 -0800 Subject: [PATCH] Switch from `getSegments()` to `containsUpLevelReferences()` in `ArtifactRoot#asDerivedRoot`. `getSegments()` iterates over the path string twice and also creates garbage by taking substrings and putting them in a list. `containsUpLevelReferences()` takes constant time, as it does not need to look past the 3rd character of the already-normalized path string. Additionally, clean up `PathFragment#containsUpLevelReferences` so that it does not perform string concatenation. PiperOrigin-RevId: 357826526 --- .../com/google/devtools/build/lib/actions/ArtifactRoot.java | 2 +- .../java/com/google/devtools/build/lib/vfs/PathFragment.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/actions/ArtifactRoot.java b/src/main/java/com/google/devtools/build/lib/actions/ArtifactRoot.java index 4f376d2a3f60a5..cd2206b725b2bc 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/ArtifactRoot.java +++ b/src/main/java/com/google/devtools/build/lib/actions/ArtifactRoot.java @@ -107,7 +107,7 @@ public static ArtifactRoot asDerivedRoot( // Make sure that we are not creating a derived artifact under the execRoot. Preconditions.checkArgument(!execPath.isEmpty(), "empty execPath"); Preconditions.checkArgument( - !execPath.getSegments().contains(".."), + !execPath.containsUplevelReferences(), "execPath: %s contains parent directory reference (..)", execPath); Preconditions.checkArgument( diff --git a/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java b/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java index 6704b2f518de70..28e79c54699cf8 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java @@ -614,7 +614,8 @@ public PathFragment toRelative() { */ public boolean containsUplevelReferences() { // Path is normalized, so any ".." would have to be the first segment. - return normalizedPath.equals("..") || normalizedPath.startsWith(".." + SEPARATOR_CHAR); + return normalizedPath.startsWith("..") + && (normalizedPath.length() == 2 || normalizedPath.charAt(2) == SEPARATOR_CHAR); } /**