Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-97752

QHash: non-readonly iteration access destroys iterator

    XMLWordPrintable

Details

    • All
    • d1626ca6b0 (qt/qtbase/dev) d1626ca6b0 (qt/tqtc-qtbase/dev) b968f37b08 (qt/qtbase/6.3) 4e30362dd0 (qt/qtbase/6.2) 4e30362dd0 (qt/tqtc-qtbase/6.2) b968f37b08 (qt/tqtc-qtbase/6.3)

    Description

      Example:

          QHash<QString, QString> hash;
          for (const QString & key: QStringList{ "a","b","c","d","e","f","g","h" })
          {
              hash.insert(key, "value of " + key);
          }
      
          for (auto it = hash.keyBegin(); it != hash.keyEnd(); ++it)
          {
              qWarning() << *it << " --> " << hash[*it];
          }
      

      generates the following output (and is aborted if address sanitizer is active):

      "g"  -->  ""
      "a"  -->  "value of a"
      "c"  -->  "value of c"
      "e"  -->  "value of e"
      "d"  -->  "value of d"
      "h"  -->  "value of h"
      "f"  -->  "value of f"
      "b"  -->  "value of b"
      

      Problem:

      • non-const operator[] calls d->findOrInsert(key)
      • findOrInsert() increases hash size, even if key exists
      • the realloc destroys the iterator

      Expected:

      Only resize hash if the key is not found and must be inserted.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            laknoll Lars Knoll
            jgrill Joachim Grill
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes