Unverified Commit 0f9bfa82 authored by Jisi Liu's avatar Jisi Liu Committed by GitHub

Merge pull request #4016 from jquesnelle/string-access-ub

fix undefined behavior in C++03
parents 27e877fd f5b08627
...@@ -155,12 +155,13 @@ string normalize(string path) { ...@@ -155,12 +155,13 @@ string normalize(string path) {
static const string dot("."); static const string dot(".");
static const string dotdot(".."); static const string dotdot("..");
const char *p = path.c_str();
std::vector<string> segments; std::vector<string> segments;
int segment_start = -1; int segment_start = -1;
// Find the path segments in `path` (separated by "/"). // Find the path segments in `path` (separated by "/").
for (int i = 0;; ++i) { for (int i = 0;; ++i) {
if (!is_separator(path[i]) && path[i] != '\0') { if (!is_separator(p[i]) && p[i] != '\0') {
// The current character does not end a segment, so start one unless it's // The current character does not end a segment, so start one unless it's
// already started. // already started.
if (segment_start < 0) { if (segment_start < 0) {
...@@ -169,7 +170,7 @@ string normalize(string path) { ...@@ -169,7 +170,7 @@ string normalize(string path) {
} else if (segment_start >= 0 && i > segment_start) { } else if (segment_start >= 0 && i > segment_start) {
// The current character is "/" or "\0", so this ends a segment. // The current character is "/" or "\0", so this ends a segment.
// Add that to `segments` if there's anything to add; handle "." and "..". // Add that to `segments` if there's anything to add; handle "." and "..".
string segment(path, segment_start, i - segment_start); string segment(p, segment_start, i - segment_start);
segment_start = -1; segment_start = -1;
if (segment == dotdot) { if (segment == dotdot) {
if (!segments.empty() && if (!segments.empty() &&
...@@ -180,7 +181,7 @@ string normalize(string path) { ...@@ -180,7 +181,7 @@ string normalize(string path) {
segments.push_back(segment); segments.push_back(segment);
} }
} }
if (path[i] == '\0') { if (p[i] == '\0') {
break; break;
} }
} }
...@@ -203,7 +204,7 @@ string normalize(string path) { ...@@ -203,7 +204,7 @@ string normalize(string path) {
result << segments[i]; result << segments[i];
} }
// Preserve trailing separator if the input contained it. // Preserve trailing separator if the input contained it.
if (!path.empty() && is_separator(path[path.size() - 1])) { if (!path.empty() && is_separator(p[path.size() - 1])) {
result << '\\'; result << '\\';
} }
return result.str(); return result.str();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment