Issue #2928 - Add extra checks to nsExpirationTracker.

Turn some asserts into early exits and add sanity checks.
Also check main thread access for the fake mutex and report.
This commit is contained in:
Moonchild 2026-02-05 13:44:21 +01:00 committed by OwnedByWuigi
commit ad21e2d1f4

View file

@ -127,9 +127,15 @@ public:
*/
nsresult AddObjectLocked(T* aObj, const AutoLock& aAutoLock)
{
if (NS_WARN_IF(!aObj)) {
// Invalid object to add
return NS_ERROR_UNEXPECTED;
}
nsExpirationState* state = aObj->GetExpirationState();
NS_ASSERTION(!state->IsTracked(),
"Tried to add an object that's already tracked");
if (NS_WARN_IF(state->IsTracked())) {
// Tried to add an object that's already tracked.
return NS_ERROR_UNEXPECTED;
}
nsTArray<T*>& generation = mGenerations[mNewestGeneration];
uint32_t index = generation.Length();
if (index > nsExpirationState::MAX_INDEX_IN_GENERATION) {
@ -156,8 +162,15 @@ public:
*/
void RemoveObjectLocked(T* aObj, const AutoLock& aAutoLock)
{
if (NS_WARN_IF(!aObj)) {
// Invalid object to remove
return;
}
nsExpirationState* state = aObj->GetExpirationState();
NS_ASSERTION(state->IsTracked(), "Tried to remove an object that's not tracked");
if (NS_WARN_IF(!state->IsTracked())) {
// Tried to remove an object that's not tracked
return;
}
nsTArray<T*>& generation = mGenerations[state->mGeneration];
uint32_t index = state->mIndexInGeneration;
NS_ASSERTION(generation.Length() > index &&
@ -458,11 +471,13 @@ class nsExpirationTracker : protected ::detail::SingleThreadedExpirationTracker<
Lock mLock;
AutoLock FakeLock() {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
return AutoLock(mLock);
}
Lock& GetMutex() override
{
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
return mLock;
}