From 269bb496c56dcf268831e02df035040162f30590 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Thu, 4 Oct 2018 14:45:55 -0700 Subject: [PATCH] Fix normalize_path's handling of "/.." Currently it normalizes the path to a string with a single uninitialized byte. It should instead normalize it to "/". Bug: none Test: /data/nativetest/linker-unit-tests/linker-unit-tests32 Test: /data/nativetest64/linker-unit-tests/linker-unit-tests64 Change-Id: I06e0f7598d16acfa21875dad53efbc293cfeb44d --- linker/linker_utils.cpp | 4 ++-- linker/tests/linker_utils_test.cpp | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/linker/linker_utils.cpp b/linker/linker_utils.cpp index 789d5c1b8..6b9aec9fa 100644 --- a/linker/linker_utils.cpp +++ b/linker/linker_utils.cpp @@ -98,8 +98,8 @@ bool normalize_path(const char* path, std::string* normalized_path) { while (out_ptr > buf && *--out_ptr != '/') { } if (in_ptr[0] == 0) { - // retain '/' - out_ptr++; + // retain '/' (or write the initial '/' for "/..") + *out_ptr++ = '/'; } continue; } diff --git a/linker/tests/linker_utils_test.cpp b/linker/tests/linker_utils_test.cpp index dce223a10..e406af5f6 100644 --- a/linker/tests/linker_utils_test.cpp +++ b/linker/tests/linker_utils_test.cpp @@ -58,6 +58,9 @@ TEST(linker_utils, normalize_path_smoke) { ASSERT_TRUE(normalize_path("/a/../../b", &output)); ASSERT_EQ("/b", output); + ASSERT_TRUE(normalize_path("/..", &output)); + ASSERT_EQ("/", output); + output = "unchanged"; ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output)); ASSERT_EQ("unchanged", output);