[SERVER-6062] mongoutils::str::stripTrailing accesses invalid iterators Created: 11/Jun/12  Updated: 25/Oct/14  Resolved: 17/Oct/14

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 2.0.6, 2.1.1
Fix Version/s: 2.7.8

Type: Bug Priority: Major - P3
Reporter: Andy Schwerin Assignee: Unassigned
Resolution: Done Votes: 0
Labels: pull-request
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

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 ) {



 Comments   
Comment by Githook User [ 17/Oct/14 ]

Author:

{u'username': u'Michael-karn-ivanov', u'name': u'Michael Ivanov', u'email': u'michael.karn.ivanov@gmail.com'}

Message: SERVER-6062 clean up of str::stripTrailing. added unit test.

Closes #795

Signed-off-by: Benety Goh <benety@mongodb.com>
Branch: master
https://github.com/mongodb/mongo/commit/a868d509e98c217edeb51e42623725272e114eea

Generated at Thu Feb 08 03:10:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.