From 2373e190d38e3bfd1ebcdf359fe191e62fd6971f Mon Sep 17 00:00:00 2001 From: roytam1 Date: Thu, 8 Jan 2026 00:13:35 +0800 Subject: [PATCH] ported from UXP: Issue #2858 - Hack around the circular calls to `nsPrincipal::Write()` (99c31714) --- caps/nsPrincipal.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/caps/nsPrincipal.cpp b/caps/nsPrincipal.cpp index 6c5d359b0..36adb9f59 100644 --- a/caps/nsPrincipal.cpp +++ b/caps/nsPrincipal.cpp @@ -471,11 +471,16 @@ nsPrincipal::Write(nsIObjectOutputStream* aStream) rv = aStream->WriteStringZ(suffix.get()); NS_ENSURE_SUCCESS(rv, rv); - nsCSPContext* testcsp = static_cast(mCSP.get()); - if(testcsp) { - int csprefcnt = testcsp->AddRef(); - testcsp->Release(); - if(csprefcnt > 10) return NS_OK; + // This is an ugly hack to prevent re-entrancy calls into nsPrincipal::Write() + // because of repeat CSP processing. + // We refcount the context and if found 10+ refcounts on the same, skip writing + // the mCSP compound object. + // TODO: figure out exactly why this happens and fix the root cause. + nsCSPContext* CSPContext = static_cast(mCSP.get()); + if(CSPContext) { + int8_t CSPDepth = CSPContext->AddRef(); + CSPContext->Release(); + if(CSPDepth > 10) return NS_OK; } rv = NS_WriteOptionalCompoundObject(aStream, mCSP,