stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Farid Zaripov (JIRA)" <j...@apache.org>
Subject [jira] Updated: (STDCXX-514) basic_stringbuf<>::str() deallocating external buffer
Date Thu, 09 Aug 2007 14:37:42 GMT

     [ https://issues.apache.org/jira/browse/STDCXX-514?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Farid Zaripov updated STDCXX-514:
---------------------------------

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


  was:
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()

struct MyAlloc : std::allocator <char>
{
    static char      __buf [512];
    static size_type __used_n;

    pointer allocate (size_type __n, std::allocator<void>::const_pointer = 0) {
        assert (!__used_n);
	assert (sizeof (__buf) >= __n * sizeof (value_type));
        __used_n = __n;
        return __buf;
    }

    void deallocate (pointer __p, size_type __n)
    {
        if (__p) {
            assert (__buf == __p);
            assert (__used_n == __n);
	    __used_n = 0;
        }
    }
};

MyAlloc::size_type MyAlloc::__used_n = 0;
char MyAlloc::__buf [512];

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

    sbuf.pubsetbuf (extbuf, sizeof (extbuf));
    
    // stdcxx extension used: basic_stringbuf::str (const char*, size_t)
    sbuf.str ("abcdef", 6);

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

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



Simplified test case.

> basic_stringbuf<>::str() deallocating external buffer
> -----------------------------------------------------
>
>                 Key: STDCXX-514
>                 URL: https://issues.apache.org/jira/browse/STDCXX-514
>             Project: C++ Standard Library
>          Issue Type: Bug
>          Components: 27. Input/Output
>    Affects Versions: 4.1.3
>         Environment: All
>            Reporter: Farid Zaripov
>            Assignee: Farid Zaripov
>             Fix For: 4.2
>
>
> 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
> ------------------

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message