Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-514

basic_stringbuf<>::str() deallocating external buffer

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • None
    • 27. Input/Output
    • None
    • All

    Description

      The program below aborts due to deallocating the external buffer extbuf.

      test.cpp:
      ------------------
      #include <sstream> // for stringbuf
      #include <memory> // for allocator

      #include <cassert> // for assert()

      char extbuf [3];

      struct MyAlloc : std::allocator <char>
      {
      pointer allocate (size_type __n, std::allocator<void>::const_pointer = 0)

      { return new char [__n]; }

      void deallocate (pointer __p, size_type)

      { assert (extbuf != __p); delete [] __p; }

      };

      int main ()
      {
      std::basic_stringbuf <char, std::char_traits <char>, MyAlloc> sbuf;

      sbuf.pubsetbuf (extbuf, sizeof (extbuf));

      const char* str = "abcdef";
      sbuf.str (str);
      assert (sbuf.str () == str);

      return 0;
      }
      ------------------

      The test output:
      ------------------
      test: test.cpp:16: void MyAlloc::deallocate(char*, unsigned int): Assertion `extbuf != __p' failed.
      Aborted
      ------------------

      Attachments

        Activity

          People

            farid Farid Zaripov
            farid Farid Zaripov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: