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

QJsonArray and QJsonObject const iterators allow modification

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: In Progress
    • Priority: P2: Important
    • Resolution: Unresolved
    • Affects Version/s: 6.2.2
    • Fix Version/s: None
    • Component/s: Core: Other
    • Labels:
      None
    • Platform/s:
      All

      Description

      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.

        Attachments

          Issue Links

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

            Activity

              People

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

                Dates

                Created:
                Updated:

                  Gerrit Reviews

                  There are no open Gerrit changes