From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21534 invoked by alias); 14 Oct 2012 17:06:43 -0000 Received: (qmail 21471 invoked by uid 48); 14 Oct 2012 17:06:24 -0000 From: "david at doublewise dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/54924] New: Warn for std::string constructor with wrong size Date: Sun, 14 Oct 2012 17:06:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: david at doublewise dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-10/txt/msg01315.txt.bz2 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.