This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/54924] New: Warn for std::string constructor with wrong size


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54924

             Bug #: 54924
           Summary: Warn for std::string constructor with wrong size
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: david@doublewise.net


The constructor for std::string that takes an array of char and a size assumes
that the array of char you pass in is at least as large as the size you
specify. In other words, std::string str('0', 100) is undefined behavior. As I
show in this example, the real issue can be much more subtle if escape
characters are involved:

http://stackoverflow.com/questions/164168/how-do-you-construct-a-stdstring-with-an-embedded-null/12884464#12884464

It would be nice if gcc warned when the size specified in the constructor
exceeds the size of the array passed as the first argument.


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