diff --git a/src/support/os_cygwin.cpp b/src/support/os_cygwin.cpp index 1adfa5f973..8ffaef55e4 100644 --- a/src/support/os_cygwin.cpp +++ b/src/support/os_cygwin.cpp @@ -187,8 +187,13 @@ bool path_prefix_is(string & path, string const & pre, path_case how) if (common_len != p2_len) return false; - if (how == CASE_ADJUSTED && !prefixIs(path, pre)) - path = to_utf8(p2 + p1.substr(common_len, p1_len - common_len)); + if (how == CASE_ADJUSTED && !prefixIs(path, pre)) { + if (p1_len < common_len) + path = to_utf8(p2.substr(0, p1_len)); + else + path = to_utf8(p2 + p1.substr(common_len, + p1_len - common_len)); + } return true; } diff --git a/src/support/os_unix.cpp b/src/support/os_unix.cpp index ef13ff7f9e..2e6f9ba309 100644 --- a/src/support/os_unix.cpp +++ b/src/support/os_unix.cpp @@ -100,8 +100,13 @@ bool path_prefix_is(string & path, string const & pre, path_case how) if (common_len != p2_len) return false; - if (how == CASE_ADJUSTED && !prefixIs(path, pre)) - path = to_utf8(p2 + p1.substr(common_len, p1_len - common_len)); + if (how == CASE_ADJUSTED && !prefixIs(path, pre)) { + if (p1_len < common_len) + path = to_utf8(p2.substr(0, p1_len)); + else + path = to_utf8(p2 + p1.substr(common_len, + p1_len - common_len)); + } return true; #else diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp index 743677833a..018b5edd6e 100644 --- a/src/support/os_win32.cpp +++ b/src/support/os_win32.cpp @@ -218,8 +218,13 @@ bool path_prefix_is(string & path, string const & pre, path_case how) if (common_len != p2_len) return false; - if (how == CASE_ADJUSTED && !prefixIs(path, pre)) - path = to_utf8(p2 + p1.substr(common_len, p1_len - common_len)); + if (how == CASE_ADJUSTED && !prefixIs(path, pre)) { + if (p1_len < common_len) + path = to_utf8(p2.substr(0, p1_len)); + else + path = to_utf8(p2 + p1.substr(common_len, + p1_len - common_len)); + } return true; }