From 0c2982d13ea290a451efa8e8ddbaa9af1e7229a1 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Mon, 10 Nov 2014 19:38:08 -0800 Subject: [PATCH] fix not respecting length --- src/util.cc | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/util.cc b/src/util.cc index f89c7ad..e667992 100644 --- a/src/util.cc +++ b/src/util.cc @@ -98,7 +98,7 @@ bool CanonicalizePath(string* path, unsigned int* slash_bits, string* err) { } #ifdef _WIN32 -unsigned int ShiftOverBit(int offset, unsigned int bits) { +static unsigned int ShiftOverBit(int offset, unsigned int bits) { // e.g. for |offset| == 2: // | ... 9 8 7 6 5 4 3 2 1 0 | // \_________________/ \_/ @@ -124,25 +124,29 @@ bool CanonicalizePath(char* path, size_t* len, unsigned int* slash_bits, char* components[kMaxPathComponents]; int component_count = 0; + char* start = path; + char* dst = start; + const char* src = start; + const char* end = start + *len; + #ifdef _WIN32 // kMaxPathComponents protects this from overflowing. unsigned int bits = 0; - int bits_offset = 0; - for (char* c = path; (c = strpbrk(c, "/\\")) != NULL;) { - if (static_cast(c - path) >= *len) - break; - bits |= (*c == '\\') << bits_offset; - *c++ = '/'; - bits_offset++; + unsigned int bits_mask = 1; + // Convert \ to /, setting a bit in |bits| for each \ encountered. + for (char* c = path; c < end; ++c) { + switch (*c) { + case '\\': + bits |= bits_mask; + *c = '/'; + // Intentional fallthrough. + case '/': + bits_mask <<= 1; + } } - bits_offset = 0; + int bits_offset = 0; #endif - char* start = path; - char* dst = start; - const char* src = start; - const char* end = start + *len; - if (*src == '/') { #ifdef _WIN32 bits_offset++; -- 2.7.4