Bug 1837964 - Part 2: Use UniquePtr with explicit free policy in ScriptLoader::{ConvertToUTF16,ConvertToUTF8} and their consumers. r=smaug

Remove raw pointer variant of ScriptLoader::{ConvertToUTF16,ConvertToUTF8} to
make it clearer how the buffer is allocated and how it should be freed.

The consumer in ServiceWorkerScriptCache expects the buffer allocated with
malloc, but given js_malloc and malloc are identical in browser, it also
uses JS::FreePolicy.

Differential Revision: https://phabricator.services.mozilla.com/D181204
This commit is contained in:
Tooru Fujisawa
2023-07-06 09:37:18 +00:00
parent cc0c38411e
commit 36f68ec1e5
6 changed files with 52 additions and 64 deletions

View File

@@ -2922,23 +2922,31 @@ static nsresult ConvertToUnicode(nsIChannel* aChannel, const uint8_t* aData,
}
/* static */
nsresult ScriptLoader::ConvertToUTF16(nsIChannel* aChannel,
const uint8_t* aData, uint32_t aLength,
const nsAString& aHintCharset,
Document* aDocument, char16_t*& aBufOut,
size_t& aLengthOut) {
return ConvertToUnicode(aChannel, aData, aLength, aHintCharset, aDocument,
aBufOut, aLengthOut);
nsresult ScriptLoader::ConvertToUTF16(
nsIChannel* aChannel, const uint8_t* aData, uint32_t aLength,
const nsAString& aHintCharset, Document* aDocument,
UniquePtr<char16_t[], JS::FreePolicy>& aBufOut, size_t& aLengthOut) {
char16_t* bufOut;
nsresult rv = ConvertToUnicode(aChannel, aData, aLength, aHintCharset,
aDocument, bufOut, aLengthOut);
if (NS_SUCCEEDED(rv)) {
aBufOut.reset(bufOut);
}
return rv;
}
/* static */
nsresult ScriptLoader::ConvertToUTF8(nsIChannel* aChannel, const uint8_t* aData,
uint32_t aLength,
const nsAString& aHintCharset,
Document* aDocument, Utf8Unit*& aBufOut,
size_t& aLengthOut) {
return ConvertToUnicode(aChannel, aData, aLength, aHintCharset, aDocument,
aBufOut, aLengthOut);
nsresult ScriptLoader::ConvertToUTF8(
nsIChannel* aChannel, const uint8_t* aData, uint32_t aLength,
const nsAString& aHintCharset, Document* aDocument,
UniquePtr<Utf8Unit[], JS::FreePolicy>& aBufOut, size_t& aLengthOut) {
Utf8Unit* bufOut;
nsresult rv = ConvertToUnicode(aChannel, aData, aLength, aHintCharset,
aDocument, bufOut, aLengthOut);
if (NS_SUCCEEDED(rv)) {
aBufOut.reset(bufOut);
}
return rv;
}
nsresult ScriptLoader::OnStreamComplete(