diff options
author | Scott Graham <scottmg@chromium.org> | 2014-11-10 22:48:55 (GMT) |
---|---|---|
committer | Scott Graham <scottmg@chromium.org> | 2014-11-10 22:48:55 (GMT) |
commit | aece8b098cb2a5d722edf0221dfd0744a818a8d1 (patch) | |
tree | 13111e176ac8334654ab4ce5826ebe6b828ffa6d /src | |
parent | 7d60548d7bafc7374d6486a5a6b350b4b4a2fa29 (diff) | |
download | Ninja-aece8b098cb2a5d722edf0221dfd0744a818a8d1.zip Ninja-aece8b098cb2a5d722edf0221dfd0744a818a8d1.tar.gz Ninja-aece8b098cb2a5d722edf0221dfd0744a818a8d1.tar.bz2 |
fix CanonicalizePath going past StringPiece length + test
Diffstat (limited to 'src')
-rw-r--r-- | src/util.cc | 2 | ||||
-rw-r--r-- | src/util_test.cc | 10 |
2 files changed, 12 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc index eaf720f..f89c7ad 100644 --- a/src/util.cc +++ b/src/util.cc @@ -129,6 +129,8 @@ bool CanonicalizePath(char* path, size_t* len, unsigned int* slash_bits, unsigned int bits = 0; int bits_offset = 0; for (char* c = path; (c = strpbrk(c, "/\\")) != NULL;) { + if (static_cast<size_t>(c - path) >= *len) + break; bits |= (*c == '\\') << bits_offset; *c++ = '/'; bits_offset++; diff --git a/src/util_test.cc b/src/util_test.cc index 8ec7a28..0073994 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -244,6 +244,16 @@ TEST(CanonicalizePath, SlashTracking) { EXPECT_EQ(1, slash_bits); } +TEST(CanonicalizePath, CanonicalizeNotExceedingLen) { + // Make sure searching \/ doesn't go past supplied len. + char buf[] = "foo/bar\\baz.h\\"; // Last \ past end. + unsigned int slash_bits; + string err; + size_t size = 13; + EXPECT_TRUE(::CanonicalizePath(buf, &size, &slash_bits, &err)); + EXPECT_EQ(0, strncmp("foo/bar/baz.h", buf, size)); + EXPECT_EQ(2, slash_bits); // Not including the trailing one. +} #endif TEST(CanonicalizePath, EmptyResult) { |