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) { : mActorID(aActorID), mEventTarget(aEventTarget) {
MOZ_ASSERT(mEventTarget); MOZ_ASSERT(mEventTarget);
FETCH_LOG(("FetchParentCSPEventListener [%p] actor ID: %s", this, FETCH_LOG(("FetchParentCSPEventListener [%p] actor ID: %s", this,
mActorID.ToString())); mActorID.ToString().get()));
} }
NS_IMETHODIMP FetchParent::FetchParentCSPEventListener::OnCSPViolationEvent( NS_IMETHODIMP FetchParent::FetchParentCSPEventListener::OnCSPViolationEvent(

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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