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

QJsonArray and QJsonObject const iterators allow modification

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2: Important P2: Important
    • 6.4.0
    • 6.2.2
    • Core: Other
    • None
    • All
    • ddba24535fb5732c3cb757414cf1a393bd98f693

      The operator*() for QJsonArray::const_iterator and QJsonObject::const_iterator is defined as follows:

      inline const QJsonValueRef operator*() const { return item; }
      

      Since the const qualifier on the return type by value is irrelevant, this means we can modify a JSON array or object even when it is const. This compiles without warning and modifies the array:

      const QJsonArray array = ...;
      for (auto v : array)
          v = 0;
      

      I'm not sure how this could be fixed. In Qt 5, this operator*() returned a QJsonValue, so the potential modification was limited to the value v. Of course the fix in the user code could be to write for (const auto v : array) instead.

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

            thiago Thiago Macieira
            bjorn Thorbjørn Lindeijer
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes