Bug 442870 – CompressWhitespace can freeze the browser. r=bsmedberg

This commit is contained in:
Manish Singh 2008-08-14 12:16:24 +02:00
Родитель 3f314aee54
Коммит d2f474a8aa
2 изменённых файлов: 48 добавлений и 25 удалений

Просмотреть файл

@ -1037,42 +1037,39 @@ ToNewUTF8String(const nsAString& aSource)
void
CompressWhitespace(nsAString& aString)
{
aString.Trim(" \n\t\r");
PRUnichar *start;
PRUint32 len = NS_StringGetMutableData(aString, PR_UINT32_MAX, &start);
PRUnichar *end = start + len;
PRUnichar *from = start, *to = start;
for (PRUnichar *cur = start; cur < end; ++cur) {
if (!NS_IsAsciiWhitespace(*cur))
continue;
// Skip any leading whitespace
while (from < end && NS_IsAsciiWhitespace(*from))
from++;
*cur = ' ';
while (from < end) {
PRUnichar theChar = *from++;
PRUnichar *wend;
for (wend = cur + 1; wend < end && NS_IsAsciiWhitespace(*wend); ++wend) {
// nothing to do but loop
if (NS_IsAsciiWhitespace(theChar)) {
// We found a whitespace char, so skip over any more
while (from < end && NS_IsAsciiWhitespace(*from))
from++;
// Turn all whitespace into spaces
theChar = ' ';
}
if (wend == cur + 1)
continue;
PRUint32 wlen = wend - cur - 1;
// fix "end"
end -= wlen;
// move everything forwards a bit
for (PRUnichar *m = cur + 1; m < end; ++m) {
*m = *(m + wlen);
}
*to++ = theChar;
}
// re-terminate
*end = '\0';
// Drop any trailing space
if (to > start && to[-1] == ' ')
to--;
// Set the new length.
aString.SetLength(end - start);
// Re-terminate the string
*to = '\0';
// Set the new length
aString.SetLength(to - start);
}
PRUint32

Просмотреть файл

@ -201,6 +201,31 @@ int testRFind() {
return ret;
}
int testCompressWhitespace() {
PRInt32 ret = 0;
// CompressWhitespace utility function
nsString s;
s.AssignLiteral(" ");
CompressWhitespace(s);
ret += CHECK(s.EqualsLiteral(""));
s.AssignLiteral(" no more leading spaces");
CompressWhitespace(s);
ret += CHECK(s.EqualsLiteral("no more leading spaces"));
s.AssignLiteral("no more trailing spaces ");
CompressWhitespace(s);
ret += CHECK(s.EqualsLiteral("no more trailing spaces"));
s.AssignLiteral(" hello one 2 three 45 ");
CompressWhitespace(s);
ret += CHECK(s.EqualsLiteral("hello one 2 three 45"));
return ret;
}
int main() {
int rv = 0;
rv += testEmpty();
@ -209,6 +234,7 @@ int main() {
rv += testFind();
rv += testVoid();
rv += testRFind();
rv += testCompressWhitespace();
if (0 == rv) {
fprintf(stderr, "PASS: StringAPI tests\n");
}