Bug 1445113 - Disallow duplicate static atoms. r=froydnj draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 29 Mar 2018 05:54:10 +1100
changeset 774059 05a27f73ba2564afe23a264c086291e2da1a8eb4
parent 774058 e98070bc698788008ad7b5e46150ba3b2e73e5ca
push id104367
push usernnethercote@mozilla.com
push dateWed, 28 Mar 2018 19:03:50 +0000
reviewersfroydnj
bugs1445113
milestone61.0a1
Bug 1445113 - Disallow duplicate static atoms. r=froydnj This will allow us to work with direct pointers to static atoms that are initialized at compile-time, rather than pointers to pointers to static atoms that are initialized at runtime. MozReview-Commit-ID: K04pEicuqu3
xpcom/ds/nsAtomTable.cpp
--- a/xpcom/ds/nsAtomTable.cpp
+++ b/xpcom/ds/nsAtomTable.cpp
@@ -661,30 +661,27 @@ nsAtomTable::RegisterStaticAtoms(const n
     MOZ_ASSERT(NS_strlen(atom->String()) == atom->GetLength());
 
     AtomTableKey key(atom);
     nsAtomSubTable& table = SelectSubTable(key);
     MutexAutoLock lock(table.mLock);
     AtomTableEntry* he = table.Add(key);
 
     if (he->mAtom) {
-      // Disallow creating a dynamic atom, and then later, while the dynamic
-      // atom is still alive, registering that same atom as a static atom.  It
-      // causes subtle bugs, and we're programming in C++ here, not Smalltalk.
-      if (!he->mAtom->IsStatic()) {
-        nsAutoCString name;
-        he->mAtom->ToUTF8String(name);
-        MOZ_CRASH_UNSAFE_PRINTF(
-          "Static atom registration for %s should be pushed back", name.get());
-      }
-      // XXX: A duplicate static atom! Bug 1445113 will disallow this case.
-      *atomp = static_cast<nsStaticAtom*>(he->mAtom);
-    } else {
-      he->mAtom = *atomp = const_cast<nsStaticAtom*>(atom);
+      // There are two ways we could get here.
+      // - Register two static atoms with the same string.
+      // - Create a dynamic atom and then register a static atom with the same
+      //   string while the dynamic atom is alive.
+      // Both cases can cause subtle bugs, and are disallowed. We're
+      // programming in C++ here, not Smalltalk.
+      nsAutoCString name;
+      he->mAtom->ToUTF8String(name);
+      MOZ_CRASH_UNSAFE_PRINTF("Atom for '%s' already exists", name.get());
     }
+    he->mAtom = *atomp = const_cast<nsStaticAtom*>(atom);
   }
 }
 
 void
 RegisterStaticAtoms(const nsStaticAtomSetup* aSetup, uint32_t aCount)
 {
   MOZ_ASSERT(gAtomTable);
   gAtomTable->RegisterStaticAtoms(aSetup, aCount);