mongoutils::str::stripTrailing accesses invalid iterators

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • 2.7.8
    • Affects Version/s: 2.0.6, 2.1.1
    • Component/s: Internal Code
    • ALL
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      This is risky if std::string decides to reallocate to a smaller buffer when a string shrinks below some threshold of allocated space. A preferable implementation would walk backwards through the string until the first non-whitespace character, and then use erase() to erase a whole range.

      From coverity:

      API usage errors
      Undefined behavior may result; the program may crash or subtly misbehave.
      An invalid or past-the-end iterator is being used
      
      Defect 10072 (STATIC_C)
        Checker INVALIDATE_ITERATOR (subcategory none)
        File /src/mongo/util/mongoutils/str.h
        Function mongoutils::str::stripTrailing(std::basic_string<char, std::char_traits<char>, std::allocator<char>>&, const char *)
          /src/mongo/util/mongoutils/str.h, line: 209
          "erase" invalidates iterator "i".
                              s.erase(i);
          /src/mongo/util/mongoutils/str.h, line: 206
          Using invalid iterator "i".
                      while( s.begin() != i ) {
      

            Assignee:
            Unassigned
            Reporter:
            Andy Schwerin
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: