Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What should happen if QStringList erase function is fed a bad iterator?
Qt 6.11 is out! See what's new in the release blog

What should happen if QStringList erase function is fed a bad iterator?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Moschops
    wrote on last edited by
    #1

    I have a QStringList. Another function is generating an iterator that is passed to the erase function:

    @aQstringlist.erase(someIterator);@

    there is a good chance that sometimes aQstringlist will actually be of size zero, and the iterator is the off-the-end iterator (i.e. @aQstringlist.end() @).

    The documentation doesn't specify what the erase function would do in such a case; I am seeing intermittent segFaults that might indicate unspecified behaviour, or just being (un)lucky. Does anyone know what the official verdict is on what erase does in such a case?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are trying to delete a non-existant object thus the segmentation fault.

      Are you sure your design is right if one function can create something that makes the other crash ?

      At the very least, you should check that there's something to erase.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Moschops
        wrote on last edited by
        #3

        I have no idea if the design is right; it's uncommented, undocumented and inherited, and the original author has been locked out of the repository in self-defence :p

        Sounds like the QStringList erase function doesn't do any sanity-checking of its own then? That's fine, it's utterly reasonable; it's just not clear from the 4.8 documentation that this is the case :) I wasn't really expecting it to, but it's worth checking.

        Current thinking is that the original author meant to pass TWO iterators to erase; the second one being the end() iterator, such that if the first iterator is also off the end, nothing happens.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then this screams for refactoring !

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Moschops
            wrote on last edited by
            #5

            I'm very old, so I don't refactor; I just rewrite :p

            Passed the second iterator (end) as well, no more segFaults.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Sometimes it's the only thing to do...

              Great you found out !

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved