C++ String Bug

Ross Smith ross.s@ihug.co.nz
Fri Sep 18 18:22:00 GMT 1998


From: Karl Nelson <kenelson@ece.ucdavis.edu>
>
> While tracing down an error in the Gtk-- source (a C++ widget
> library), I discovered that a certain call resulted in a segfault
> under the condition of passing a NULL pointer to it.  I traced it
> back to the string header files.  Any passing of a (char*)0 to a
> function which takes type string resulted on a seg fault on my out
> of the box Redhat 5.1 system using egcs.

>From the draft standard [lib.string.cons] (I don't believe this has been
changed in the final standard):

  basic_string(const charT* s, size_type n,
               const Allocator& a = Allocator());
  Requires:
    s shall not be a null pointer and n < npos.
  Throws:
    out_of_range if n == npos.
  Effects:
    Constructs an object of class basic_string and determines its
    initial string value from the array of charT of length n whose
    first element is designated by s, as indicated in Table 5:

Passing a null pointer to the string constructor invokes undefined
behaviour, which can certainly include a segfault.

--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
                               * * * * *
      "Screw up your courage. You've screwed up everything else."





More information about the Gcc-bugs mailing list