libstdc++/8716: std::string( NULL, 0 ) throws exception also on zero length
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
>Originator:     Joacim Zschimmer
>Release:        gcc 3.2
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.

Joacim Zschimmer
#include <string>
using namespace std;

int main( int, char** )
        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; }
Only check the (const char*)-Pointer, when length is not zero.

This is in libstdc++-v3/include/bits/basic_string.tcc line 143.

More information about the Gcc-bugs mailing list