Return-Path: Delivered-To: apmail-incubator-stdcxx-dev-archive@www.apache.org Received: (qmail 67131 invoked from network); 20 Sep 2007 22:52:59 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 20 Sep 2007 22:52:59 -0000 Received: (qmail 43889 invoked by uid 500); 20 Sep 2007 22:52:50 -0000 Delivered-To: apmail-incubator-stdcxx-dev-archive@incubator.apache.org Received: (qmail 43875 invoked by uid 500); 20 Sep 2007 22:52:50 -0000 Mailing-List: contact stdcxx-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-dev@incubator.apache.org Received: (qmail 43864 invoked by uid 99); 20 Sep 2007 22:52:50 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Sep 2007 15:52:50 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [208.30.140.160] (HELO moroha.quovadx.com) (208.30.140.160) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Sep 2007 22:52:51 +0000 Received: from qxvcexch01.ad.quovadx.com ([192.168.170.59]) by moroha.quovadx.com (8.13.6/8.13.6) with ESMTP id l8KMpia2006490 for ; Thu, 20 Sep 2007 22:51:44 GMT Received: from [10.70.3.113] ([10.70.3.113]) by qxvcexch01.ad.quovadx.com with Microsoft SMTPSVC(6.0.3790.1830); Thu, 20 Sep 2007 16:52:29 -0600 Message-ID: <46F2F9AE.3060201@roguewave.com> Date: Thu, 20 Sep 2007 16:52:30 -0600 From: Martin Sebor Organization: Rogue Wave Software, Inc. User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4 MIME-Version: 1.0 To: stdcxx-dev@incubator.apache.org Subject: Re: [PATCH] Add overflow checking to basic_string append and push_back References: <46F2D039.5070508@roguewave.com> In-Reply-To: Content-Type: multipart/mixed; boundary="------------020005000101050908080506" X-OriginalArrivalTime: 20 Sep 2007 22:52:29.0496 (UTC) FILETIME=[EC0A6380:01C7FBD8] X-Virus-Checked: Checked by ClamAV on apache.org --------------020005000101050908080506 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Travis Vitek wrote: > > > Martin, > > I think that you are right in the case of push_back, or when the string > to append is short. My last testcase didn't prove anything. This one > does. If you use the current trunk, this prints '4 aaaa' to the console. > i.e. appending 20 characters to a buffer with 240 should not get you 4, > should it? [...] > template > class Xallocator > { > public: > typedef unsigned char size_type; I suspect the problem might actually be here. Once you define size_type to a type with a more generous range the test case passes. I made this and a few other simplifying changes in the attached program (it's also possible that I messed something up in the process ;-) Martin --------------020005000101050908080506 Content-Type: text/x-c++; name="t.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="t.cpp" #include #include #include #include template struct Xallocator: std::allocator { typedef typename std::allocator::size_type size_type; Xallocator (): std::allocator() { } Xallocator (const Xallocator &rhs): std::allocator(rhs) { } template Xallocator (const Xallocator &rhs): std::allocator(rhs) { } template struct rebind { typedef Xallocator other; }; size_type max_size () const { return 255; } }; typedef std::basic_string, Xallocator > String; int main () { int failed = 0; try { String a (240, 'a'); String b (240, 'b'); // ensure that we will overflow assert (a.max_size () < a.size () + 20); a.append (b.c_str(), 20); std::printf ("%u %s\n", a.size (), a.c_str ()); failed += 1; } catch (const std::length_error&) { } catch (...) { failed += 1; } return failed; } --------------020005000101050908080506--