Skip to content

Commit 05b0412

Browse files
committed
try and work around OS_LIB_PATH_RELATIVIZER_BASE crash:
1 parent ce3f2ea commit 05b0412

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

os/src/Path.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,28 @@ object Path extends PathMacros {
452452
val maybeBase = Option(System.getenv("OS_LIB_PATH_RELATIVIZER_BASE")).filter(_.nonEmpty)
453453
maybeBase match {
454454
case Some(base0) =>
455-
pathRelativizerSerializer(Path(base0))
455+
pathRelativizerSerializer(pathFromRawString(base0))
456456
case None =>
457457
rawPathSerializer
458458
}
459459
}
460460

461+
/**
462+
* Parse a String path without depending on `pathSerializer` to avoid
463+
* initialization cycles when constructing `defaultPathSerializer`.
464+
*/
465+
private def pathFromRawString(raw: String): Path = {
466+
val parsed =
467+
if (driveRelative(raw)) Paths.get(s"$driveRoot$raw")
468+
else Paths.get(raw)
469+
470+
if (parsed.iterator.asScala.count(_.startsWith("..")) > parsed.getNameCount / 2) {
471+
throw PathError.AbsolutePathOutsideRoot
472+
}
473+
474+
new Path(parsed.normalize())
475+
}
476+
461477
@experimental def pathRelativizerSerializer(base: os.Path): Serializer = new Serializer {
462478
private def serializeRelative(p: os.Path): Option[java.nio.file.Path] = {
463479
if (p.fileSystem == base.fileSystem && p.startsWith(base)) Some(p.relativeTo(base).toNIO)

os/test/src/PathTests.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -575,25 +575,22 @@ object PathTests extends TestSuite {
575575
assert(x / "hello" == y)
576576
}
577577
test("pathRelativizerSerializer") {
578-
def check(name: String)(cond: => Boolean): Unit = {
579-
if (!cond) throw new java.lang.AssertionError(s"check failed: $name")
580-
}
581578
val base = os.pwd / "base"
582579
val serializer = os.Path.pathRelativizerSerializer(base)
583580
os.Path.pathSerializer.withValue(serializer) {
584581
val inBase = base / "foo" / "bar"
585582
val outside = os.pwd / "outside"
586583

587-
check("serialize string in base")(inBase.toString == "foo/bar")
588-
check("serialize nio in base")(inBase.toNIO == java.nio.file.Paths.get("foo/bar"))
589-
check("serialize file in base")(inBase.toIO.getPath == new java.io.File("foo/bar").getPath)
584+
assert(inBase.toString == "foo/bar")
585+
assert(inBase.toNIO == java.nio.file.Paths.get("foo/bar"))
586+
assert(inBase.toIO.getPath == new java.io.File("foo/bar").getPath)
590587

591-
check("deserialize string relative")(os.Path("foo/bar") == inBase)
592-
check("deserialize nio relative")(os.Path(java.nio.file.Paths.get("foo/bar")) == inBase)
593-
check("deserialize file relative")(os.Path(new java.io.File("foo/bar")) == inBase)
588+
assert(os.Path("foo/bar") == inBase)
589+
assert(os.Path(java.nio.file.Paths.get("foo/bar")) == inBase)
590+
assert(os.Path(new java.io.File("foo/bar")) == inBase)
594591

595-
check("serialize string outside base")(outside.toString == outside.wrapped.toString)
596-
check("serialize nio outside base")(outside.toNIO == outside.wrapped)
592+
assert(outside.toString == outside.wrapped.toString)
593+
assert(outside.toNIO == outside.wrapped)
597594
}
598595
}
599596
}

0 commit comments

Comments
 (0)