<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>31657</bug_id>
          
          <creation_ts>2009-11-18 18:43:07 -0800</creation_ts>
          <short_desc>MessageQueue::removeIf() has a bug that leads to failed assertions</short_desc>
          <delta_ts>2011-07-27 09:18:50 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>New Bugs</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Dumitru Daniliuc">dumi</reporter>
          <assigned_to name="Dumitru Daniliuc">dumi</assigned_to>
          <cc>dglazkov</cc>
    
    <cc>kbalazs</cc>
    
    <cc>michaeln</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>164872</commentid>
    <comment_count>0</comment_count>
    <who name="Dumitru Daniliuc">dumi</who>
    <bug_when>2009-11-18 18:43:07 -0800</bug_when>
    <thetext>MessageQueue::removeIf() has a bug:

DequeConstIterator&lt;DataType*&gt; found = m_queue.end();   // Adds &apos;found&apos; to the list of iterators of &apos;m_queue&apos;
while ((found = m_queue.findIf(predicate) != m_queue.end()) {   // operator=() checks that found.m_queue is not NULL
    DataType* message = *found;
    m_queue.remove(found);    // triggers m_queue.invalidateIteratorators() which sets it.m_queue = NULL for all current iterators of &apos;m_queue&apos;, including &apos;found&apos;
    delete message;
}

So the first time the loop is run, everything is fine. But then the element to which &apos;found&apos; points is removed from the list. This triggers Deque::invalidateIterators(), which sets found.m_queue = NULL. The second time &apos;found = m_queue.findIf(predicate)&apos; is executed, DequeConstIterator::operator=() is called, which eventually calls DequeIteratorBase::operator=(), which ASSERTs that &apos;found.m_queue&apos; is not NULL. This ASSERT fails. So any time this method is called with a predicate that matches at least one element in the queue, we get an assertion failure.

Solution: Do not carry over the same DequeConstIterator variable from one run of the loop to the other, i.e. declare &apos;found&apos; inside the loop.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>164875</commentid>
    <comment_count>1</comment_count>
      <attachid>43477</attachid>
    <who name="Dumitru Daniliuc">dumi</who>
    <bug_when>2009-11-18 18:52:31 -0800</bug_when>
    <thetext>Created attachment 43477
patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>164893</commentid>
    <comment_count>2</comment_count>
      <attachid>43477</attachid>
    <who name="Dmitry Titov">dimich</who>
    <bug_when>2009-11-18 21:23:06 -0800</bug_when>
    <thetext>Comment on attachment 43477
patch

Cool catch! r=me

Please consider also this shape of the loop, it seems a bit simpler (if you agree, it can be changed on landing):

while (true) {
    DequeConstIterator&lt;DataType*&gt; found = m_queue.findIf(predicate);
    if (found == m_queue.end())
        break;

    DataType* message = *found;
    m_queue.remove(found);
    delete message;
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>165039</commentid>
    <comment_count>3</comment_count>
    <who name="Dumitru Daniliuc">dumi</who>
    <bug_when>2009-11-19 11:51:35 -0800</bug_when>
    <thetext>(In reply to comment #2)
&gt; (From update of attachment 43477 [details])
&gt; Cool catch! r=me
&gt; 
&gt; Please consider also this shape of the loop, it seems a bit simpler (if you
&gt; agree, it can be changed on landing):
&gt; 
&gt; while (true) {
&gt;     DequeConstIterator&lt;DataType*&gt; found = m_queue.findIf(predicate);
&gt;     if (found == m_queue.end())
&gt;         break;
&gt; 
&gt;     DataType* message = *found;
&gt;     m_queue.remove(found);
&gt;     delete message;
&gt; }

Changing the loop as you suggested. Wasn&apos;t sure if &apos;while (true)&apos; was acceptable in WebKit code.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>165047</commentid>
    <comment_count>4</comment_count>
    <who name="Dumitru Daniliuc">dumi</who>
    <bug_when>2009-11-19 12:00:24 -0800</bug_when>
    <thetext>Landed as r51198.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>442966</commentid>
    <comment_count>5</comment_count>
    <who name="Balazs Kelemen">kbalazs</who>
    <bug_when>2011-07-27 09:18:50 -0700</bug_when>
    <thetext>I have found a similar bug: https://bugs.webkit.org/show_bug.cgi?id=65263</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>43477</attachid>
            <date>2009-11-18 18:52:31 -0800</date>
            <delta_ts>2009-11-18 21:23:05 -0800</delta_ts>
            <desc>patch</desc>
            <filename>patch</filename>
            <type>text/plain</type>
            <size>1663</size>
            <attacher name="Dumitru Daniliuc">dumi</attacher>
            
              <data encoding="base64">SW5kZXg6IEphdmFTY3JpcHRDb3JlL0NoYW5nZUxvZwo9PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci0tLSBKYXZhU2NyaXB0
Q29yZS9DaGFuZ2VMb2cJKHJldmlzaW9uIDUxMTY2KQorKysgSmF2YVNjcmlwdENvcmUvQ2hhbmdl
TG9nCSh3b3JraW5nIGNvcHkpCkBAIC0xLDMgKzEsMTUgQEAKKzIwMDktMTEtMTggIER1bWl0cnUg
RGFuaWxpdWMgIDxkdW1pQGNocm9taXVtLm9yZz4KKworICAgICAgICBSZXZpZXdlZCBieSBOT0JP
RFkgKE9PUFMhKS4KKworICAgICAgICBGaXhpbmcgYSBidWcgaW4gTWVzc2FnZVF1ZXVlOjpyZW1v
dmVJZigpIHRoYXQgbGVhZHMgdG8gYW4KKyAgICAgICAgYXNzZX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9xdWV1
ZS5yZW1vdmUoZm91bmQpOwotICAgICAgICAgICAgZGVsZXRlIG1lc3NhZ2U7CisgICAgICAgIC8v
IFNlZSBidWcgMzE2NTcgZm9yIHdoeSB0aGlzIGxvb3AgbG9va3Mgc28gd2VpcmQKKyAgICAgICAg
Ym9vbCBkb25lID0gZmFsc2U7CisgICAgICAgIHdoaWxlICghZG9uZSkgeworICAgICAgICAgICAg
RGVxdWVDb25zdEl0ZXJhdG9yPERhdGFUeXBlKj4gZm91bmQgPSBtX3F1ZXVlLmZpbmRJZihwcmVk
aWNhdGUpOworICAgICAgICAgICAgaWYgKGZvdW5kICE9IG1fcXVldWUuZW5kKCkpIHsKKyAgICAg
ICAgICAgICAgICBEYXRhVHlwZSogbWVzc2FnZSA9ICpmb3VuZDsKKyAgICAgICAgICAgICAgICBt
X3F1ZXVlLnJlbW92ZShmb3VuZCk7CisgICAgICAgICAgICAgICAgZGVsZXRlIG1lc3NhZ2U7Cisg
ICAgICAgICAgICB9IGVsc2UKKyAgICAgICAgICAgICAgICBkb25lID0gdHJ1ZTsKICAgICAgICB9
CiAgICAgfQogCg==
</data>
<flag name="review"
          id="25190"
          type_id="1"
          status="+"
          setter="dimich"
    />
    <flag name="commit-queue"
          id="25191"
          type_id="3"
          status="-"
          setter="dumi"
    />
          </attachment>
      

    </bug>

</bugzilla>