This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/54924] New: Warn for std::string constructor with wrong size
- From: "david at doublewise dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sun, 14 Oct 2012 17:06:23 +0000
- Subject: [Bug c++/54924] New: Warn for std::string constructor with wrong size
- Auto-submitted: auto-generated
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.