Bug 1811934 - Have nsID::ToString() return an managed string instead of a raw pointer r=xpcom-reviewers,nika

And use nsID::ToProvidedString(...) method when suitable.

This naturally fixes a memory leak in dom/fetch/FetchParent.cpp.

Differential Revision: https://phabricator.services.mozilla.com/D167606
This commit is contained in:
serge-sans-paille 2023-01-31 16:44:04 +00:00
Родитель c03916bb96
Коммит da45a5a7c1
9 изменённых файлов: 24 добавлений и 48 удалений

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

@ -27,7 +27,7 @@ FetchParent::FetchParentCSPEventListener::FetchParentCSPEventListener(
: mActorID(aActorID), mEventTarget(aEventTarget) {
MOZ_ASSERT(mEventTarget);
FETCH_LOG(("FetchParentCSPEventListener [%p] actor ID: %s", this,
mActorID.ToString()));
mActorID.ToString().get()));
}
NS_IMETHODIMP FetchParent::FetchParentCSPEventListener::OnCSPViolationEvent(

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

@ -594,8 +594,7 @@ nsXPCWrappedJS* nsXPCWrappedJS::FindInherited(REFNSIID aIID) {
return nullptr;
}
nsresult
nsIXPConnectWrappedJS::GetInterfaceIID(nsIID** iid) {
nsresult nsIXPConnectWrappedJS::GetInterfaceIID(nsIID** iid) {
MOZ_ASSERT(iid, "bad param");
*iid = AsXPCWrappedJS()->GetIID().Clone();
@ -662,11 +661,8 @@ nsresult nsXPCWrappedJS::DebugDump(int16_t depth) {
IsRootWrapper() ? "ROOT" : "non-root", mJSObj.get()));
const char* name = mInfo->Name();
XPC_LOG_ALWAYS(("interface name is %s", name));
char* iid = mInfo->IID().ToString();
XPC_LOG_ALWAYS(("IID number is %s", iid ? iid : "invalid"));
if (iid) {
free(iid);
}
auto iid = mInfo->IID().ToString();
XPC_LOG_ALWAYS(("IID number is %s", iid.get()));
XPC_LOG_ALWAYS(("nsXPTInterfaceInfo @ %p", mInfo));
if (!IsRootWrapper()) {

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

@ -1065,11 +1065,8 @@ void nsXPCWrappedJS::DebugDumpInterfaceInfo(const nsXPTInterfaceInfo* aInfo,
XPC_LOG_INDENT();
const char* name = aInfo->Name();
XPC_LOG_ALWAYS(("interface name is %s", name));
char* iid = aInfo->IID().ToString();
XPC_LOG_ALWAYS(("IID number is %s", iid ? iid : "invalid"));
if (iid) {
free(iid);
}
auto iid = aInfo->IID().ToString();
XPC_LOG_ALWAYS(("IID number is %s", iid.get()));
XPC_LOG_ALWAYS(("InterfaceInfo @ %p", aInfo));
uint16_t methodCount = 0;
if (depth) {

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

@ -39,7 +39,7 @@ ExtensionServiceWorkerInfo::GetScriptURL(nsAString& aScriptURL) {
NS_IMETHODIMP
ExtensionServiceWorkerInfo::GetClientInfoId(nsAString& aClientInfoId) {
MOZ_ASSERT(NS_IsMainThread());
aClientInfoId = NS_ConvertUTF8toUTF16(mClientInfo.Id().ToString());
aClientInfoId = NS_ConvertUTF8toUTF16(mClientInfo.Id().ToString().get());
return NS_OK;
}

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

@ -163,18 +163,10 @@ static const char gIDFormat[] =
"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
/*
* Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
* format. The string is allocated with moz_xmalloc and should be freed by
* the caller.
* Returns a managed string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
* format.
*/
char* nsID::ToString() const {
char* res = (char*)moz_xmalloc(NSID_LENGTH);
snprintf(res, NSID_LENGTH, gIDFormat, m0, (uint32_t)m1, (uint32_t)m2,
(uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2], (uint32_t)m3[3],
(uint32_t)m3[4], (uint32_t)m3[5], (uint32_t)m3[6], (uint32_t)m3[7]);
return res;
}
nsIDToCString nsID::ToString() const { return nsIDToCString(*this); }
void nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const {
SprintfLiteral(aDest, gIDFormat, m0, (uint32_t)m1, (uint32_t)m2,

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

@ -13,6 +13,10 @@
#define NSID_LENGTH 39
#ifndef XPCOM_GLUE_AVOID_NSPR
class nsIDToCString;
#endif
/**
* A "unique identifier". This is modeled after OSF DCE UUIDs.
*/
@ -69,11 +73,10 @@ struct nsID {
#ifndef XPCOM_GLUE_AVOID_NSPR
/**
* nsID string encoder. Returns an allocated string in
* {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format. Caller should free string.
* YOU SHOULD ONLY USE THIS IF YOU CANNOT USE ToProvidedString() BELOW.
* nsID string encoder. Returns a managed string in
* {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format.
*/
char* ToString() const;
nsIDToCString ToString() const;
/**
* nsID string encoder. Builds a string in

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

@ -657,11 +657,9 @@ nsComponentManagerImpl::GetClassObject(const nsCID& aClass, const nsIID& aIID,
nsresult rv;
if (MOZ_LOG_TEST(nsComponentManagerLog, LogLevel::Debug)) {
char* buf = aClass.ToString();
char buf[NSID_LENGTH];
aClass.ToProvidedString(buf);
PR_LogPrint("nsComponentManager: GetClassObject(%s)", buf);
if (buf) {
free(buf);
}
}
MOZ_ASSERT(aResult != nullptr);
@ -765,13 +763,11 @@ nsComponentManagerImpl::CreateInstance(const nsCID& aClass, const nsIID& aIID,
}
if (MOZ_LOG_TEST(nsComponentManagerLog, LogLevel::Warning)) {
char* buf = aClass.ToString();
char buf[NSID_LENGTH];
aClass.ToProvidedString(buf);
MOZ_LOG(nsComponentManagerLog, LogLevel::Warning,
("nsComponentManager: CreateInstance(%s) %s", buf,
NS_SUCCEEDED(rv) ? "succeeded" : "FAILED"));
if (buf) {
free(buf);
}
}
return rv;

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

@ -703,12 +703,7 @@ nsresult nsDiscriminatedUnion::ToString(nsACString& aOutString) const {
// nsID has its own text formatter.
case nsIDataType::VTYPE_ID: {
char* ptr = u.mIDValue.ToString();
if (!ptr) {
return NS_ERROR_OUT_OF_MEMORY;
}
aOutString.Assign(ptr);
free(ptr);
aOutString.Assign(u.mIDValue.ToString().get());
return NS_OK;
}

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

@ -27,10 +27,7 @@ TEST(nsID, StringConversion)
const char* idstr = ids[i];
ASSERT_TRUE(id.Parse(idstr));
char* cp = id.ToString();
ASSERT_NE(cp, nullptr);
ASSERT_STREQ(cp, ids[4 * (i / 4) + 3]);
free(cp);
auto cp = id.ToString();
ASSERT_STREQ(cp.get(), ids[4 * (i / 4) + 3]);
}
}