Skip to content

Commit

Permalink
Bug 1272781 - nsEscape should work with ns(C)Strings and not with cha…
Browse files Browse the repository at this point in the history
…r pointers, r=smaug
  • Loading branch information
bakulf committed May 18, 2016
1 parent f5951cc commit 9889419
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 76 deletions.
25 changes: 10 additions & 15 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5230,24 +5230,21 @@ nsDocShell::LoadErrorPage(nsIURI* aURI, const char16_t* aURL,
// Create a URL to pass all the error information through to the page.

#undef SAFE_ESCAPE
#define SAFE_ESCAPE(cstring, escArg1, escArg2) \
{ \
char* s = nsEscape(escArg1, escArg2); \
if (!s) \
return NS_ERROR_OUT_OF_MEMORY; \
cstring.Adopt(s); \
#define SAFE_ESCAPE(output, input, params) \
if (NS_WARN_IF(!NS_Escape(input, output, params))) { \
return NS_ERROR_OUT_OF_MEMORY; \
}

nsCString escapedUrl, escapedCharset, escapedError, escapedDescription,
escapedCSSClass;
SAFE_ESCAPE(escapedUrl, url.get(), url_Path);
SAFE_ESCAPE(escapedCharset, charset.get(), url_Path);
SAFE_ESCAPE(escapedError,
NS_ConvertUTF16toUTF8(aErrorType).get(), url_Path);
SAFE_ESCAPE(escapedUrl, url, url_Path);
SAFE_ESCAPE(escapedCharset, charset, url_Path);
SAFE_ESCAPE(escapedError, NS_ConvertUTF16toUTF8(aErrorType), url_Path);
SAFE_ESCAPE(escapedDescription,
NS_ConvertUTF16toUTF8(aDescription).get(), url_Path);
NS_ConvertUTF16toUTF8(aDescription), url_Path);
if (aCSSClass) {
SAFE_ESCAPE(escapedCSSClass, aCSSClass, url_Path);
nsCString cssClass(aCSSClass);
SAFE_ESCAPE(escapedCSSClass, cssClass, url_Path);
}
nsCString errorPageUrl("about:");
errorPageUrl.AppendASCII(aErrorPage);
Expand Down Expand Up @@ -5276,9 +5273,7 @@ nsDocShell::LoadErrorPage(nsIURI* aURI, const char16_t* aURL,
nsresult rv = GetAppManifestURL(manifestURL);
if (manifestURL.Length() > 0) {
nsCString manifestParam;
SAFE_ESCAPE(manifestParam,
NS_ConvertUTF16toUTF8(manifestURL).get(),
url_Path);
SAFE_ESCAPE(manifestParam, NS_ConvertUTF16toUTF8(manifestURL), url_Path);
errorPageUrl.AppendLiteral("&m=");
errorPageUrl.AppendASCII(manifestParam.get());
}
Expand Down
12 changes: 6 additions & 6 deletions dom/base/nsXHTMLContentSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ nsXHTMLContentSerializer::EscapeURI(nsIContent* aContent, const nsAString& aURI,
if (textToSubURI && !IsASCII(part)) {
rv = textToSubURI->ConvertAndEscape(mCharset.get(), part.get(), getter_Copies(escapedURI));
NS_ENSURE_SUCCESS(rv, rv);
}
else {
escapedURI.Adopt(nsEscape(NS_ConvertUTF16toUTF8(part).get(), url_Path));
} else if (NS_WARN_IF(!NS_Escape(NS_ConvertUTF16toUTF8(part), escapedURI,
url_Path))) {
return NS_ERROR_OUT_OF_MEMORY;
}
AppendASCIItoUTF16(escapedURI, aEscapedURI);

Expand All @@ -212,9 +212,9 @@ nsXHTMLContentSerializer::EscapeURI(nsIContent* aContent, const nsAString& aURI,
if (textToSubURI) {
rv = textToSubURI->ConvertAndEscape(mCharset.get(), part.get(), getter_Copies(escapedURI));
NS_ENSURE_SUCCESS(rv, rv);
}
else {
escapedURI.Adopt(nsEscape(NS_ConvertUTF16toUTF8(part).get(), url_Path));
} else if (NS_WARN_IF(!NS_Escape(NS_ConvertUTF16toUTF8(part), escapedURI,
url_Path))) {
return NS_ERROR_OUT_OF_MEMORY;
}
AppendASCIItoUTF16(escapedURI, aEscapedURI);
}
Expand Down
16 changes: 9 additions & 7 deletions dom/html/nsFormSubmission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ nsFSURLEncoded::GetEncodedSubmission(nsIURI* aURI,
HandleMailtoSubject(path);

// Append the body to and force-plain-text args to the mailto line
nsCString escapedBody;
escapedBody.Adopt(nsEscape(mQueryString.get(), url_XAlphas));
nsAutoCString escapedBody;
if (NS_WARN_IF(!NS_Escape(mQueryString, escapedBody, url_XAlphas))) {
return NS_ERROR_OUT_OF_MEMORY;
}

path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;

Expand Down Expand Up @@ -666,11 +668,11 @@ nsFSTextPlain::GetEncodedSubmission(nsIURI* aURI,
HandleMailtoSubject(path);

// Append the body to and force-plain-text args to the mailto line
char* escapedBuf = nsEscape(NS_ConvertUTF16toUTF8(mBody).get(),
url_XAlphas);
NS_ENSURE_TRUE(escapedBuf, NS_ERROR_OUT_OF_MEMORY);
nsCString escapedBody;
escapedBody.Adopt(escapedBuf);
nsAutoCString escapedBody;
if (NS_WARN_IF(!NS_Escape(NS_ConvertUTF16toUTF8(mBody), escapedBody,
url_XAlphas))) {
return NS_ERROR_OUT_OF_MEMORY;
}

path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody;

Expand Down
3 changes: 1 addition & 2 deletions intl/uconv/nsTextToSubURI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ NS_IMETHODIMP nsTextToSubURI::ConvertAndEscape(
outlen += finLen;
}
}
pBuf[outlen] = '\0';
*_retval = nsEscape(pBuf, url_XPAlphas);
*_retval = nsEscape(pBuf, outlen, nullptr, url_XPAlphas);
if (nullptr == *_retval) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
Expand Down
24 changes: 14 additions & 10 deletions netwerk/base/nsDirectoryIndexStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,28 @@ nsDirectoryIndexStream::Read(char* aBuf, uint32_t aCount, uint32_t* aReadCount)
mBuf.AppendLiteral("201: ");

// The "filename" field
char* escaped = nullptr;
if (!NS_IsNativeUTF8()) {
nsAutoString leafname;
rv = current->GetLeafName(leafname);
if (NS_FAILED(rv)) return rv;
if (!leafname.IsEmpty())
escaped = nsEscape(NS_ConvertUTF16toUTF8(leafname).get(), url_Path);

nsAutoCString escaped;
if (!leafname.IsEmpty() &&
NS_Escape(NS_ConvertUTF16toUTF8(leafname), escaped, url_Path)) {
mBuf.Append(escaped);
mBuf.Append(' ');
}
} else {
nsAutoCString leafname;
rv = current->GetNativeLeafName(leafname);
if (NS_FAILED(rv)) return rv;
if (!leafname.IsEmpty())
escaped = nsEscape(leafname.get(), url_Path);
}
if (escaped) {
mBuf += escaped;
mBuf.Append(' ');
free(escaped);

nsAutoCString escaped;
if (!leafname.IsEmpty() &&
NS_Escape(leafname, escaped, url_Path)) {
mBuf.Append(escaped);
mBuf.Append(' ');
}
}

// The "content-length" field
Expand Down
6 changes: 3 additions & 3 deletions netwerk/streamconv/converters/nsFTPDirListingConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ nsFTPDirListingConv::DigestBufferLines(char *aBuffer, nsCString &aString) {
PR_FormatTimeUSEnglish(buffer, sizeof(buffer),
"%a, %d %b %Y %H:%M:%S", &result.fe_time );

char *escapedDate = nsEscape(buffer, url_Path);
aString.Append(escapedDate);
free(escapedDate);
nsAutoCString escaped;
NS_WARN_IF(!NS_Escape(nsDependentCString(buffer), escaped, url_Path));
aString.Append(escaped);
aString.Append(' ');

// ENTRY TYPE
Expand Down
10 changes: 4 additions & 6 deletions rdf/datasource/nsFileSystemDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,16 +1007,14 @@ FileSystemDataSource::GetFolderList(nsIRDFResource *source, bool allowHidden,
fullURI.Append('/');
}

char *escLeafStr = nsEscape(NS_ConvertUTF16toUTF8(leafStr).get(), url_Path);
nsAutoCString leaf;
bool escaped = NS_Escape(NS_ConvertUTF16toUTF8(leafStr), leaf, url_Path);
leafStr.Truncate();

if (!escLeafStr)
if (!escaped) {
continue;
}

nsAutoCString leaf(escLeafStr);
free(escLeafStr);
escLeafStr = nullptr;

// using nsEscape() [above] doesn't escape slashes, so do that by hand
int32_t aOffset;
while ((aOffset = leaf.FindChar('/')) >= 0)
Expand Down
16 changes: 2 additions & 14 deletions xpcom/io/nsEscape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,8 @@ AppendPercentHex(char16_t* aBuffer, char16_t aChar)

//----------------------------------------------------------------------------------------
char*
nsEscape(const char* aStr, nsEscapeMask aFlags)
//----------------------------------------------------------------------------------------
{
if (!aStr) {
return nullptr;
}

return nsEscapeWithLength(aStr, strlen(aStr), nullptr, aFlags);
}

//----------------------------------------------------------------------------------------
char*
nsEscapeWithLength(const char* aStr, size_t aLength, size_t* aOutputLength,
nsEscapeMask aFlags)
nsEscape(const char* aStr, size_t aLength, size_t* aOutputLength,
nsEscapeMask aFlags)
//----------------------------------------------------------------------------------------
{
if (!aStr) {
Expand Down
18 changes: 5 additions & 13 deletions xpcom/io/nsEscape.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ extern "C" {
* @param aMask How to escape the string
* @return A newly allocated escaped string that must be free'd with
* nsCRT::free, or null on failure
* @note: Please, don't use this function. Use NS_Escape instead!
*/
char* nsEscapeWithLength(const char* aStr, size_t aLength, size_t* aOutputLen,
nsEscapeMask aMask);

/**
* Escape the given string according to mask
* @param aStr The string to escape
* @param aMask How to escape the string
* @return A newly allocated escaped string that must be free'd with
* nsCRT::free, or null on failure
*/
char* nsEscape(const char* aStr, nsEscapeMask aMask);
char* nsEscape(const char* aStr, size_t aLength, size_t* aOutputLen,
nsEscapeMask aMask);

char* nsUnescape(char* aStr);
/* decode % escaped hex codes into character values,
Expand Down Expand Up @@ -201,8 +193,8 @@ NS_Escape(const nsACString& aOriginal, nsACString& aEscaped,
nsEscapeMask aMask)
{
size_t escLen = 0;
char* esc = nsEscapeWithLength(aOriginal.BeginReading(), aOriginal.Length(),
&escLen, aMask);
char* esc = nsEscape(aOriginal.BeginReading(), aOriginal.Length(), &escLen,
aMask);
if (! esc) {
return false;
}
Expand Down

0 comments on commit 9889419

Please sign in to comment.