diff --git a/libs/utils/src/Path.cpp b/libs/utils/src/Path.cpp index 69fe2ea2b5..e31fa3469d 100644 --- a/libs/utils/src/Path.cpp +++ b/libs/utils/src/Path.cpp @@ -70,7 +70,8 @@ bool Path::isDirectory() const { Path Path::concat(const Path& path) const { if (path.isEmpty()) return *this; if (path.isAbsolute()) return path; - if (m_path.back() != SEPARATOR && !m_path.empty()) { + // std::string::back() is UB if the string is empty, so we rely on short-circuit evaluation + if (!m_path.empty() && m_path.back() != SEPARATOR) { return Path(m_path + SEPARATOR + path.getPath()); } return Path(m_path + path.getPath()); @@ -80,7 +81,8 @@ void Path::concatToSelf(const Path& path) { if (!path.isEmpty()) { if (path.isAbsolute()) { m_path = path.getPath(); - } else if (m_path.back() != SEPARATOR) { + // std::string::back() is UB if the string is empty, so we rely on short-circuit evaluation + } else if (!m_path.empty() && m_path.back() != SEPARATOR) { m_path = getCanonicalPath(m_path + SEPARATOR + path.getPath()); } else { m_path = getCanonicalPath(m_path + path.getPath()); diff --git a/libs/utils/test/test_WinPath.cpp b/libs/utils/test/test_WinPath.cpp index 8478ce548b..954e067e20 100644 --- a/libs/utils/test/test_WinPath.cpp +++ b/libs/utils/test/test_WinPath.cpp @@ -173,6 +173,14 @@ TEST(WinPathTest, Concatenate) { // Unix-style separators work too r = root.concat("out/bin/foo/bar"); EXPECT_EQ("C:\\Volumes\\Replicant\\blue\\out\\bin\\foo\\bar", r.getPath()); + + r = ""; + r = r.concat("foo\\bar"); + EXPECT_EQ("foo\\bar", r.getPath()); + + r = ""; + r.concatToSelf("foo\\bar"); + EXPECT_EQ("foo\\bar", r.getPath()); } TEST(PathTest, GetParent) { diff --git a/libs/utils/test/test_sstream.cpp b/libs/utils/test/test_sstream.cpp index a0870c0d9f..b9167ea31c 100644 --- a/libs/utils/test/test_sstream.cpp +++ b/libs/utils/test/test_sstream.cpp @@ -96,11 +96,6 @@ TEST(sstream, Formatting) { ss << (bool) true; EXPECT_STREQ("1", ss.c_str()); } - { - sstream ss; - ss << (const void *) 0x12345678; - EXPECT_STREQ("0x12345678", ss.c_str()); - } { sstream ss; ss << (const char *) "hello";