libstdc++/8716: std::string( NULL, 0 ) throws exception also on zero length

x@sources.redhat.com x@sources.redhat.com
Tue Nov 26 07:21:00 GMT 2002


>Number:         8716
>Category:       libstdc++
>Synopsis:       std::string( NULL, 0 ) throws exception also on zero length
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Nov 26 05:56:02 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Joacim Zschimmer
>Release:        gcc 3.2
>Organization:
>Environment:
Linux
>Description:
The constructor string::string(const char*,int) throws an exception if the length is zero and the pointer is null. The string-constructor should accept string(s,0), even if const char* s = NULL.

The constructor should check the pointer only if there are some bytes the pointer points to. With zero length there are no bytes and a NULL-pointer should be allowed. Throwing an exception on NULL is quite good (better then SEGV), but only if the pointer is really to be used.

Sincerly
Joacim Zschimmer
>How-To-Repeat:
#include <string>
using namespace std;

int main( int, char** )
{
    try
    {
        const char* s = NULL;
        string a = string( s, 0 );
        printf( "ok\n" );
        return 0;
    }
    catch( const exception& x ) { printf( "%s\n", x.what() ); return 1; }
}
>Fix:
Only check the (const char*)-Pointer, when length is not zero.

This is in libstdc++-v3/include/bits/basic_string.tcc line 143.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list