From kde-commits Sat May 22 17:41:22 2010 From: Maks Orlovich Date: Sat, 22 May 2010 17:41:22 +0000 To: kde-commits Subject: KDE/kdelibs/kjs Message-Id: <20100522174123.04498AC8BC () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=127454974508831 SVN commit 1129509 by orlovich: Fix a string internment bug that occurs when garbage collection must run to allocate the StringImp. M +1 -0 CompileState.h M +12 -8 interpreter.cpp --- trunk/KDE/kdelibs/kjs/CompileState.h #1129508:1129509 @@ -337,6 +337,7 @@ } inline OpValue OpValue::immValue(JSValue* in) { + assert(in); OpValue res; initImm(&res, OpType_value); res.value.wide.valueVal = in; --- trunk/KDE/kdelibs/kjs/interpreter.cpp #1129508:1129509 @@ -948,16 +948,20 @@ StringImp* Interpreter::internString(const UString& literal) { - std::pair p = - s_internedStrings->add(literal.rep(), std::make_pair((StringImp*)(0), 1)); + InternedStringsTable::iterator i = s_internedStrings->find(literal.rep()); - if (p.second) // actually added.. - p.first.values()->first = static_cast(jsOwnedString(literal)); - else - ++p.first.values()->second; // just bump the ref count - - return p.first.values()->first; + if (i == s_internedStrings->end()) { + // Need to add. Note: we can't use ->add() above to avoid a double-hash + // as creation of a StringImp may cause a GC, which in turn may + // rearrange the hashtable, invalidating the iterator. + StringImp* si = static_cast(jsOwnedString(literal)); + s_internedStrings->add(literal.rep(), std::make_pair(si, 1)); + return si; + } else { + ++i.values()->second; // just bump the ref count + return i.values()->first; } +} void Interpreter::releaseInternedString(const UString& literal) {