Bug 1294940 - Part 2: Add validation for ConstUTF8CharsZ. r=jwalden

--HG--
extra : rebase_source : eb101f4fa5eacf839908d3f5f6729763dca78992
This commit is contained in:
Tooru Fujisawa 2016-08-13 23:03:30 +09:00
parent fc1c230284
commit c12ce2fc48
2 changed files with 26 additions and 3 deletions

View File

@ -127,6 +127,9 @@ class ConstUTF8CharsZ
: data_(aBytes)
{
MOZ_ASSERT(aBytes[aLength] == '\0');
#ifdef DEBUG
validate(aLength);
#endif
}
const void* get() const { return data_; }
@ -134,6 +137,11 @@ class ConstUTF8CharsZ
const char* c_str() const { return data_; }
explicit operator bool() const { return data_ != nullptr; }
private:
#ifdef DEBUG
void validate(size_t aLength);
#endif
};
/*

View File

@ -249,6 +249,7 @@ ReportTooBigCharacter(JSContext* cx, uint32_t v)
enum InflateUTF8Action {
CountAndReportInvalids,
CountAndIgnoreInvalids,
AssertNoInvalids,
Copy
};
@ -261,6 +262,7 @@ static bool
InflateUTF8StringToBuffer(JSContext* cx, const UTF8Chars src, char16_t* dst, size_t* dstlenp,
bool* isAsciip)
{
if (Action != AssertNoInvalids)
*isAsciip = true;
// Count how many char16_t characters need to be in the inflated string.
@ -276,6 +278,7 @@ InflateUTF8StringToBuffer(JSContext* cx, const UTF8Chars src, char16_t* dst, siz
} else {
// Non-ASCII code unit. Determine its length in bytes (n).
if (Action != AssertNoInvalids)
*isAsciip = false;
uint32_t n = 1;
while (v & (0x80 >> n))
@ -286,6 +289,8 @@ InflateUTF8StringToBuffer(JSContext* cx, const UTF8Chars src, char16_t* dst, siz
if (Action == CountAndReportInvalids) { \
report(cx, arg); \
return false; \
} else if (Action == AssertNoInvalids) { \
MOZ_CRASH("invalid UTF-8 string: " # report); \
} else { \
if (Action == Copy) \
dst[j] = char16_t(REPLACE_UTF8); \
@ -350,6 +355,7 @@ InflateUTF8StringToBuffer(JSContext* cx, const UTF8Chars src, char16_t* dst, siz
}
}
if (Action != AssertNoInvalids)
*dstlenp = j;
return true;
@ -398,3 +404,12 @@ JS::LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8, size_t
return InflateUTF8StringHelper<CountAndIgnoreInvalids>(cx, utf8, outlen);
}
#ifdef DEBUG
void
JS::ConstUTF8CharsZ::validate(size_t aLength)
{
MOZ_ASSERT(data_);
UTF8Chars chars(data_, aLength);
InflateUTF8StringToBuffer<AssertNoInvalids>(nullptr, chars, nullptr, nullptr, nullptr);
}
#endif