Bug 34524 - Use of invalidated std::string iterators not caught in debug mode
Summary: Use of invalidated std::string iterators not caught in debug mode
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-12-18 19:43 UTC by Eelis
Modified: 2007-12-18 20:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eelis 2007-12-18 19:43:03 UTC
In the following code, libstdc++'s debug mode does not catch the use of a potentially invalidated std::string iterator.

  #define _GLIBCXX_DEBUG
  #define _GLIBCXX_DEBUG_PEDANTIC

  #include <string>
  #include <vector>
  #include <iostream>

  int main()
  {
    typedef std::string S;

    S s (3, 'x');
    S::iterator i = s.begin(); ++i;
    s.push_back('y');
    std::cout << *i << std::endl; // just outputs 'x'
  }

Since the push_back may invalidate i (per 21.3 para 5, 4th item), libstdc++'s debug mode should emit an error and abort.

If I change the typedef to std::vector<char>, then the indicated line /does/ cause libstdc++ to emit an error ("attempt to dereference a singular iterator") and abort.
Comment 1 Paolo Carlini 2007-12-18 20:00:29 UTC
Did you read the documentation?

  http://gcc.gnu.org/onlinedocs/libstdc++/ext/debug_mode.html

in a nutshell, our design doesn't provide safe iterators for basic_string.
Comment 2 Eelis 2007-12-18 20:16:31 UTC
My apologies.