Bug 1052427 - Make nsContentUtils::ASCIIToLower/ASCIIToUpper infallible; r=smaug

This commit is contained in:
Aryeh Gregor 2014-08-13 14:53:32 +03:00
parent c17dda821c
commit 0b4a8e008c
5 changed files with 45 additions and 65 deletions

View File

@ -1579,16 +1579,16 @@ public:
* @return NS_OK on success, or NS_ERROR_OUT_OF_MEMORY if making the string
* writable needs to allocate memory and that allocation fails.
*/
static nsresult ASCIIToLower(nsAString& aStr);
static nsresult ASCIIToLower(const nsAString& aSource, nsAString& aDest);
static void ASCIIToLower(nsAString& aStr);
static void ASCIIToLower(const nsAString& aSource, nsAString& aDest);
/**
* Convert ASCII a-z to A-Z.
* @return NS_OK on success, or NS_ERROR_OUT_OF_MEMORY if making the string
* writable needs to allocate memory and that allocation fails.
*/
static nsresult ASCIIToUpper(nsAString& aStr);
static nsresult ASCIIToUpper(const nsAString& aSource, nsAString& aDest);
static void ASCIIToUpper(nsAString& aStr);
static void ASCIIToUpper(const nsAString& aSource, nsAString& aDest);
/**
* Return whether aStr contains an ASCII uppercase character.

View File

@ -959,10 +959,8 @@ Element::SetAttribute(const nsAString& aName,
nsCOMPtr<nsIAtom> nameAtom;
if (IsHTML() && IsInHTMLDocument()) {
nsAutoString lower;
nsresult rv = nsContentUtils::ASCIIToLower(aName, lower);
if (NS_SUCCEEDED(rv)) {
nameAtom = do_GetAtom(lower);
}
nsContentUtils::ASCIIToLower(aName, lower);
nameAtom = do_GetAtom(lower);
}
else {
nameAtom = do_GetAtom(aName);

View File

@ -5555,14 +5555,13 @@ nsContentUtils::GetCurrentJSContextForThread()
}
/* static */
nsresult
void
nsContentUtils::ASCIIToLower(nsAString& aStr)
{
char16_t* iter = aStr.BeginWriting();
char16_t* end = aStr.EndWriting();
if (MOZ_UNLIKELY(!iter || !end)) {
return NS_ERROR_OUT_OF_MEMORY;
}
MOZ_ASSERT(iter && end);
while (iter != end) {
char16_t c = *iter;
if (c >= 'A' && c <= 'Z') {
@ -5570,43 +5569,38 @@ nsContentUtils::ASCIIToLower(nsAString& aStr)
}
++iter;
}
return NS_OK;
}
/* static */
nsresult
void
nsContentUtils::ASCIIToLower(const nsAString& aSource, nsAString& aDest)
{
uint32_t len = aSource.Length();
aDest.SetLength(len);
if (aDest.Length() == len) {
char16_t* dest = aDest.BeginWriting();
if (MOZ_UNLIKELY(!dest)) {
return NS_ERROR_OUT_OF_MEMORY;
}
const char16_t* iter = aSource.BeginReading();
const char16_t* end = aSource.EndReading();
while (iter != end) {
char16_t c = *iter;
*dest = (c >= 'A' && c <= 'Z') ?
c + ('a' - 'A') : c;
++iter;
++dest;
}
return NS_OK;
MOZ_ASSERT(aDest.Length() == len);
char16_t* dest = aDest.BeginWriting();
MOZ_ASSERT(dest);
const char16_t* iter = aSource.BeginReading();
const char16_t* end = aSource.EndReading();
while (iter != end) {
char16_t c = *iter;
*dest = (c >= 'A' && c <= 'Z') ?
c + ('a' - 'A') : c;
++iter;
++dest;
}
return NS_ERROR_OUT_OF_MEMORY;
}
/* static */
nsresult
void
nsContentUtils::ASCIIToUpper(nsAString& aStr)
{
char16_t* iter = aStr.BeginWriting();
char16_t* end = aStr.EndWriting();
if (MOZ_UNLIKELY(!iter || !end)) {
return NS_ERROR_OUT_OF_MEMORY;
}
MOZ_ASSERT(iter && end);
while (iter != end) {
char16_t c = *iter;
if (c >= 'a' && c <= 'z') {
@ -5614,32 +5608,28 @@ nsContentUtils::ASCIIToUpper(nsAString& aStr)
}
++iter;
}
return NS_OK;
}
/* static */
nsresult
void
nsContentUtils::ASCIIToUpper(const nsAString& aSource, nsAString& aDest)
{
uint32_t len = aSource.Length();
aDest.SetLength(len);
if (aDest.Length() == len) {
char16_t* dest = aDest.BeginWriting();
if (MOZ_UNLIKELY(!dest)) {
return NS_ERROR_OUT_OF_MEMORY;
}
const char16_t* iter = aSource.BeginReading();
const char16_t* end = aSource.EndReading();
while (iter != end) {
char16_t c = *iter;
*dest = (c >= 'a' && c <= 'z') ?
c + ('A' - 'a') : c;
++iter;
++dest;
}
return NS_OK;
MOZ_ASSERT(aDest.Length() == len);
char16_t* dest = aDest.BeginWriting();
MOZ_ASSERT(dest);
const char16_t* iter = aSource.BeginReading();
const char16_t* end = aSource.EndReading();
while (iter != end) {
char16_t c = *iter;
*dest = (c >= 'a' && c <= 'z') ?
c + ('A' - 'a') : c;
++iter;
++dest;
}
return NS_ERROR_OUT_OF_MEMORY;
}
/* static */

View File

@ -467,14 +467,12 @@ HTMLCanvasElement::ToDataURLImpl(JSContext* aCx,
}
nsAutoString type;
nsresult rv = nsContentUtils::ASCIIToLower(aMimeType, type);
if (NS_FAILED(rv)) {
return rv;
}
nsContentUtils::ASCIIToLower(aMimeType, type);
nsAutoString params;
bool usingCustomParseOptions;
rv = ParseParams(aCx, type, aEncoderOptions, params, &usingCustomParseOptions);
nsresult rv =
ParseParams(aCx, type, aEncoderOptions, params, &usingCustomParseOptions);
if (NS_FAILED(rv)) {
return rv;
}
@ -515,10 +513,7 @@ HTMLCanvasElement::ToBlob(JSContext* aCx,
}
nsAutoString type;
aRv = nsContentUtils::ASCIIToLower(aType, type);
if (aRv.Failed()) {
return;
}
nsContentUtils::ASCIIToLower(aType, type);
nsAutoString params;
bool usingCustomParseOptions;

View File

@ -381,10 +381,7 @@ DataTransfer::GetData(const nsAString& aFormat, nsAString& aData,
// for the URL type, parse out the first URI from the list. The URIs are
// separated by newlines
nsAutoString lowercaseFormat;
aRv = nsContentUtils::ASCIIToLower(aFormat, lowercaseFormat);
if (aRv.Failed()) {
return;
}
nsContentUtils::ASCIIToLower(aFormat, lowercaseFormat);
if (lowercaseFormat.EqualsLiteral("url")) {
int32_t lastidx = 0, idx;