mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-08 10:44:56 +00:00
Bug 1318403 - Part 4.2: Use realloc instead of malloc when resizing a newly created string buffer. r=Waldo
This commit is contained in:
parent
f68188a9e5
commit
9b2c99ea6f
@ -603,6 +603,24 @@ js::SubstringKernel(JSContext* cx, HandleString str, int32_t beginInt, int32_t l
|
|||||||
return NewDependentString(cx, str, begin, len);
|
return NewDependentString(cx, str, begin, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
static auto
|
||||||
|
ReallocChars(JSContext* cx, UniquePtr<CharT[], JS::FreePolicy> chars, size_t oldLength,
|
||||||
|
size_t newLength)
|
||||||
|
-> decltype(chars)
|
||||||
|
{
|
||||||
|
using AnyCharPtr = decltype(chars);
|
||||||
|
|
||||||
|
CharT* oldChars = chars.release();
|
||||||
|
CharT* newChars = cx->pod_realloc(oldChars, oldLength, newLength);
|
||||||
|
if (!newChars) {
|
||||||
|
js_free(oldChars);
|
||||||
|
return AnyCharPtr();
|
||||||
|
}
|
||||||
|
|
||||||
|
return AnyCharPtr(newChars);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* U+03A3 GREEK CAPITAL LETTER SIGMA has two different lower case mappings
|
* U+03A3 GREEK CAPITAL LETTER SIGMA has two different lower case mappings
|
||||||
* depending on its context:
|
* depending on its context:
|
||||||
@ -823,11 +841,10 @@ ToLowerCase(JSContext* cx, JSLinearString* str)
|
|||||||
"Latin-1 strings don't have special lower case mappings");
|
"Latin-1 strings don't have special lower case mappings");
|
||||||
resultLength = ToLowerCaseLength(chars, readChars, length);
|
resultLength = ToLowerCaseLength(chars, readChars, length);
|
||||||
|
|
||||||
AnyCharPtr buf = cx->make_pod_array<CharT>(resultLength + 1);
|
AnyCharPtr buf = ReallocChars(cx, Move(newChars), length + 1, resultLength + 1);
|
||||||
if (!buf)
|
if (!buf)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
PodCopy(buf.get(), newChars.get(), readChars);
|
|
||||||
newChars = Move(buf);
|
newChars = Move(buf);
|
||||||
|
|
||||||
MOZ_ALWAYS_TRUE(length ==
|
MOZ_ALWAYS_TRUE(length ==
|
||||||
@ -1125,11 +1142,10 @@ ToUpperCase(JSContext* cx, JSLinearString* str)
|
|||||||
if (readChars < length) {
|
if (readChars < length) {
|
||||||
resultLength = ToUpperCaseLength(chars, readChars, length);
|
resultLength = ToUpperCaseLength(chars, readChars, length);
|
||||||
|
|
||||||
Latin1CharPtr buf2 = cx->make_pod_array<Latin1Char>(resultLength + 1);
|
Latin1CharPtr buf2 = ReallocChars(cx, Move(buf), length + 1, resultLength + 1);
|
||||||
if (!buf2)
|
if (!buf2)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
CopyChars(buf2.get(), buf.get(), readChars);
|
|
||||||
buf = Move(buf2);
|
buf = Move(buf2);
|
||||||
|
|
||||||
MOZ_ALWAYS_TRUE(length ==
|
MOZ_ALWAYS_TRUE(length ==
|
||||||
@ -1148,11 +1164,10 @@ ToUpperCase(JSContext* cx, JSLinearString* str)
|
|||||||
if (readChars < length) {
|
if (readChars < length) {
|
||||||
resultLength = ToUpperCaseLength(chars, readChars, length);
|
resultLength = ToUpperCaseLength(chars, readChars, length);
|
||||||
|
|
||||||
TwoByteCharPtr buf2 = cx->make_pod_array<char16_t>(resultLength + 1);
|
TwoByteCharPtr buf2 = ReallocChars(cx, Move(buf), length + 1, resultLength + 1);
|
||||||
if (!buf2)
|
if (!buf2)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
CopyChars(buf2.get(), buf.get(), readChars);
|
|
||||||
buf = Move(buf2);
|
buf = Move(buf2);
|
||||||
|
|
||||||
MOZ_ALWAYS_TRUE(length ==
|
MOZ_ALWAYS_TRUE(length ==
|
||||||
|
Loading…
Reference in New Issue
Block a user