This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Avoiding string segfaults


Hello all,

This is my first post on this list. Sorry if it's not the right place
for my query.

Look at this simple code :

 1 #include <string>
 2 #include <iostream>
 3  
 4 int main(int argc, char* argv[])
 5 {
 6     string s1("some string") ;
 7     string s2((char*)0) ;
 8  
 9     cout << "s1 = [" << s1 << "]" << endl ;
10     cout << "s2 = [" << s2 << "]" << endl ;
11 }

It will segfault before displaying anything, due to line 7. This occures
when I try to build a string from a null pointer.
Looking at the code for the string class (basic_string actually), I didn't
find any check against a null-pointer when building a string from a
char*.
Is this the wanted behaviour ? Would it be bad to build an empty string
from a null pointer ?

Sorry for bothering you, if this has already been discussed and closed.

Regards,

-- 
   .~.          __________________________  
   /V\   Linux /                          \        ___ 
  // \\  Power |        Yves BAILLY       |      /"   "\
 /(   )\       | kafka.fr@netcourrier.com |     ( @ _ @ )
- ^^-^^ -------\__________________________/----oOO (_) OOo------
LUG Dijon : http://www.coagul.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]