Fix regression in MakeRelativePath()
The logic in the function only works correctly when the
`input` and `dest` arguments are zero-terminated strings.
Changing them to std::string_view in a previous CL broke
this assumption, resulting in buffer overflows, runtime
errors with stdlib++ assertions turned on.
Fixed: 358
Change-Id: Idfb52ffcbc38943ca5c8d231fae2fd0c50f20fd3
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/16700
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: David Turner <digit@google.com>
Reviewed-by: David Turner <digit@google.com>
diff --git a/src/gn/filesystem_utils.cc b/src/gn/filesystem_utils.cc
index d747289..672c7f5 100644
--- a/src/gn/filesystem_utils.cc
+++ b/src/gn/filesystem_utils.cc
@@ -722,9 +722,11 @@
size_t common_prefix_len = 0;
size_t max_common_length = std::min(input.size(), dest.size());
for (size_t i = common_prefix_len; i <= max_common_length; i++) {
- if ((IsSlash(input[i]) || input[i] == '\0') && IsSlash(dest[i]))
+ if (dest.size() == i)
+ break;
+ if ((input.size() == i || IsSlash(input[i])) && IsSlash(dest[i]))
common_prefix_len = i + 1;
- else if (input[i] != dest[i])
+ else if (input.size() == i || input[i] != dest[i])
break;
}
@@ -736,7 +738,7 @@
// Append any remaining unique input.
if (common_prefix_len <= input.size())
- ret.append(&input[common_prefix_len], input.size() - common_prefix_len);
+ ret.append(input.begin() + common_prefix_len, input.end());
else if (input.back() != '/' && !ret.empty())
ret.pop_back();