Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit aece8b0

Browse files
committedNov 10, 2014
fix CanonicalizePath going past StringPiece length + test
1 parent 7d60548 commit aece8b0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
 

‎src/util.cc

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ bool CanonicalizePath(char* path, size_t* len, unsigned int* slash_bits,
129129
unsigned int bits = 0;
130130
int bits_offset = 0;
131131
for (char* c = path; (c = strpbrk(c, "/\\")) != NULL;) {
132+
if (static_cast<size_t>(c - path) >= *len)
133+
break;
132134
bits |= (*c == '\\') << bits_offset;
133135
*c++ = '/';
134136
bits_offset++;

‎src/util_test.cc

+10
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ TEST(CanonicalizePath, SlashTracking) {
244244
EXPECT_EQ(1, slash_bits);
245245
}
246246

247+
TEST(CanonicalizePath, CanonicalizeNotExceedingLen) {
248+
// Make sure searching \/ doesn't go past supplied len.
249+
char buf[] = "foo/bar\\baz.h\\"; // Last \ past end.
250+
unsigned int slash_bits;
251+
string err;
252+
size_t size = 13;
253+
EXPECT_TRUE(::CanonicalizePath(buf, &size, &slash_bits, &err));
254+
EXPECT_EQ(0, strncmp("foo/bar/baz.h", buf, size));
255+
EXPECT_EQ(2, slash_bits); // Not including the trailing one.
256+
}
247257
#endif
248258

249259
TEST(CanonicalizePath, EmptyResult) {

0 commit comments

Comments
 (0)