From fbf28d1ce77818ec61db490a80190d6722731274 Mon Sep 17 00:00:00 2001 From: Andrey Makarov Date: Sun, 23 Feb 2020 22:22:46 +0300 Subject: [PATCH] fix 3 minor bugs in joinPath (see #13455) (#13462) [backport] (cherry picked from commit 3dad130034e3212159dda5d61988ca2b18959b24) --- lib/pure/os.nim | 3 ++- lib/pure/pathnorm.nim | 3 ++- tests/stdlib/tos.nim | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index d91a135c31..a204f26c43 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -130,6 +130,7 @@ proc joinPath*(head, tail: string): string {. when defined(posix): assert joinPath("usr", "lib") == "usr/lib" assert joinPath("usr", "") == "usr/" + assert joinPath("", "") == "" assert joinPath("", "lib") == "lib" assert joinPath("", "/lib") == "/lib" assert joinPath("usr/", "/lib") == "usr/lib" @@ -138,7 +139,7 @@ proc joinPath*(head, tail: string): string {. result = newStringOfCap(head.len + tail.len) var state = 0 addNormalizePath(head, result, state, DirSep) - if tail.len == 0: + if result.len != 0 and result[^1] notin {DirSep, AltSep} and tail.len == 0: result.add DirSep else: addNormalizePath(tail, result, state, DirSep) diff --git a/lib/pure/pathnorm.nim b/lib/pure/pathnorm.nim index 407ec156ce..3659e85e62 100644 --- a/lib/pure/pathnorm.nim +++ b/lib/pure/pathnorm.nim @@ -69,7 +69,8 @@ proc addNormalizePath*(x: string; result: var string; state: var int; while hasNext(it, x): let b = next(it, x) if (state shr 1 == 0) and isSlash(x, b): - result.add dirSep + if result.len == 0 or result[^1] notin {DirSep, AltSep}: + result.add dirSep state = state or 1 elif isDotDot(x, b): if (state shr 1) >= 1: diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index ab0cce8de1..6902f3851f 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -338,6 +338,10 @@ block ospaths: doAssert joinPath("", "lib") == "lib" doAssert joinPath("", "/lib") == unixToNativePath"/lib" doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib" + doAssert joinPath("", "") == unixToNativePath"" + doAssert joinPath("/" / "") == unixToNativePath"/" + doAssert joinPath("/", "/a/b/c") == unixToNativePath"/a/b/c" + doAssert joinPath("foo/","") == unixToNativePath"foo/" block getTempDir: block TMPDIR: