From patchwork Wed Sep 4 20:53:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 272718 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "www.sourceware.org", Issuer "StartCom Class 1 Primary Intermediate Server CA" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 2991F2C00B6 for ; Thu, 5 Sep 2013 06:53:50 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type; q=dns; s=default; b=eNvPXJvU3387w6jxgRxCXyZYdZbn5wh9XM1hRaGtA+qflnvzcD z6c8G6O9Ks2iCDeYDlHj84dPMpSGcOXG6avPUx5mWLC0a237gFEvFwi/oWHn8Dhm OvR9Hp3dv7aNLT1b3RnW0L4Q+rCAeJoy1NxgS3WfZDiY9G5TI+ECWh8UY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type; s= default; bh=KjbbT0//UCE8ebVzagPKqqic/TU=; b=P/vqt6+TTn1w6rklZfL/ UNkRbZEch8B+t0QKGaBw2eF5FFqvMpwoz3cGDPFhVAzfdo2d24ygXk7S9mHGuB2E gAJ4XPHZC22fXNnvg0eMjLlHP1jEDlCtpsO6VQucZgsaj4Hnq3cprhg9rAZnfBqt /F1jqSx4NbY8sP5hhmIkWgw= Received: (qmail 886 invoked by alias); 4 Sep 2013 20:53:41 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 863 invoked by uid 89); 4 Sep 2013 20:53:41 -0000 Received: from mail-vc0-f201.google.com (HELO mail-vc0-f201.google.com) (209.85.220.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 04 Sep 2013 20:53:41 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=3.0 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_SOFTFAIL autolearn=ham version=3.3.2 X-HELO: mail-vc0-f201.google.com Received: by mail-vc0-f201.google.com with SMTP id lf11so115154vcb.4 for ; Wed, 04 Sep 2013 13:53:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-type; bh=qLCOAVYEZQ5TNrZK2Y6qt+0zk2zArEF0d0BTWJe+J/U=; b=eIY2Kg7sv2tg11LFQRj2zR/IK+mq0456B1OFRTuMBnss8c2sZpQGNafZK6fQ2C8ixf 33JFD22kvSCIzTSADrQomRorJv5ng30Bythxl9F5zMgDWGgMSomNBCZi4HgjItylaCgn E1PPXm8mmheh1J/OaqJ4jUHqRMuSEb4k/cPrW0y7+7R96IKIhi6Efo1p2x95gP5c6xCO HGtxQfEaB6PKgE7/9u8IQWnp08/JSbcHmG8EV4zFlep6NfCa8BbdWkpgiGSWKvyrmS+q 5gUs0tuyN3eHFrMAsC2UgVLba16NBM47Bv8JmWrHTR8bcqnvWOPFu2CJ3PCTNJaM5up+ LBaA== X-Gm-Message-State: ALoCoQldzTpGqHMJ1L1aU7EysZGULgJswCDAa8mXaI0yFlbKT0QjR9BIarMoWtajs8rMe/3k1J2LEjyWsyD+4x/dU3LvKxyY+oTIpaSeTJgcnHyMFrlXl7kEBTxpOoIsgkdWhSk+/3iBCf5fasuygFLZt48FT2G/I6m60b2pMMbzdl6UmiAK6k3r+UEnjXv4/4heiB/VA+CwXgrk+jjXzXUrLA120jiehdr7Qqn/WTaXKUm5VdL68P8= X-Received: by 10.236.145.34 with SMTP id o22mr1758227yhj.22.1378328012631; Wed, 04 Sep 2013 13:53:32 -0700 (PDT) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id k45si1317188yhn.4.1969.12.31.16.00.00 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Wed, 04 Sep 2013 13:53:32 -0700 (PDT) Received: from elbrus2.mtv.corp.google.com (elbrus2.mtv.corp.google.com [172.17.128.95]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id 6B4E05A4291; Wed, 4 Sep 2013 13:53:32 -0700 (PDT) Received: by elbrus2.mtv.corp.google.com (Postfix, from userid 74925) id F0C041C0854; Wed, 4 Sep 2013 13:53:31 -0700 (PDT) From: Paul Pluzhnikov To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Cc: ppluzhnikov@google.com Subject: [patch] Make vector::at() assertion message more useful (try #2) Date: Wed, 04 Sep 2013 13:53:31 -0700 Message-ID: MIME-Version: 1.0 Greetings, This is a followup to: http://gcc.gnu.org/ml/libstdc++/2013-08/msg00096.html Without this patch, the user on vector::at out of bounds sees: terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check Aborted (core dumped) With the patch: terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 2) Aborted (core dumped) I am not at all sure the names I choose here are good ones. Suggestions welcome. I also shudder at the idea of repeating _M_range_check code in e.g. string::at(), and elsewhere. Perhaps we need a snprintf_lite, that only understands '%zu' and literal characters, e.g.: snprintf_lite(__s, sizeof(__s), _N("vector::_M_range_check: __n (which is %zu) >= " "this->size() (which is %zu)"), __n, this->size()); [The patch also doesn't include libstdc++-v3/libsupc++/Makefile.in, which I'll regenerate before submitting.] [Please CC me on any replies.] --- Paul Pluzhnikov 2013-09-04 Paul Pluzhnikov * libstdc++-v3/config/abi/pre/gnu.ver: Add _ZN9__gnu_cxx13concat_size_tEPcm * libstdc++-v3/include/bits/stl_vector.h (_M_range_check): Print additional assertion details * libstdc++-v3/libsupc++/Makefile.am: Add support.cc * libstdc++-v3/libsupc++/support.cc: New * libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc: Adjust. * testsuite/23_containers/vector/requirements/dr438/insert_neg.cc: Likewise. * testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc: Likewise. * testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc: Likewise. Index: libstdc++-v3/config/abi/pre/gnu.ver =================================================================== --- libstdc++-v3/config/abi/pre/gnu.ver (revision 202262) +++ libstdc++-v3/config/abi/pre/gnu.ver (working copy) @@ -1365,6 +1365,9 @@ # std::get_unexpected() _ZSt14get_unexpectedv; + # __gnu_cxx::concat_size_t(char*, unsigned long) + _ZN9__gnu_cxx13concat_size_tEPcm; + } GLIBCXX_3.4.19; # Symbols in the support library (libsupc++) have their own tag. Index: libstdc++-v3/include/bits/stl_vector.h =================================================================== --- libstdc++-v3/include/bits/stl_vector.h (revision 202262) +++ libstdc++-v3/include/bits/stl_vector.h (working copy) @@ -63,6 +63,10 @@ #include #endif +namespace __gnu_cxx { + int concat_size_t(char *, size_t); +} + namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_CONTAINER @@ -791,7 +795,26 @@ _M_range_check(size_type __n) const { if (__n >= this->size()) - __throw_out_of_range(__N("vector::_M_range_check")); + { + const char __p[] = __N("vector::_M_range_check: __n (which is "); + const char __q[] = __N(") >= this->size() (which is "); + + // Enough space for __p, __q, size and __n (in decimal). + const int __alloc_size = + sizeof(__p) + sizeof(__q) + 3 * 2 * sizeof(size_type) + 10; + + char *__s = static_cast(__builtin_alloca(__alloc_size)); + char *__ps = __s; + __builtin_memcpy(__ps, __p, sizeof(__p)); + __ps += sizeof(__p) - 1; + __ps += __gnu_cxx::concat_size_t(__ps, __n); + __builtin_memcpy(__ps, __q, sizeof(__q)); + __ps += sizeof(__q) - 1; + __ps += __gnu_cxx::concat_size_t(__ps, this->size()); + *(__ps++) = __N(')'); + *(__ps++) = '\0'; + __throw_out_of_range(__s); + } } public: Index: libstdc++-v3/libsupc++/Makefile.am =================================================================== --- libstdc++-v3/libsupc++/Makefile.am (revision 202262) +++ libstdc++-v3/libsupc++/Makefile.am (working copy) @@ -91,6 +91,7 @@ pointer_type_info.cc \ pure.cc \ si_class_type_info.cc \ + support.cc \ tinfo.cc \ tinfo2.cc \ vec.cc \ Index: libstdc++-v3/libsupc++/support.cc =================================================================== --- libstdc++-v3/libsupc++/support.cc (revision 0) +++ libstdc++-v3/libsupc++/support.cc (revision 0) @@ -0,0 +1,53 @@ +// Debugging support -*- C++ -*- + +// Copyright (C) 2013 Free Software Foundation, Inc. +// +// This file is part of GCC. +// +// GCC is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// GCC is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// . + +#include + +namespace std { + template + int + __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit, + ios_base::fmtflags __flags, bool __dec); +} + +namespace __gnu_cxx { + +// Exported routine to append decimal representation of __val to the given +// buffer, which must be at least 21 characters long in 64-bit mode. +// Does not NUL-terminate the output buffer. +// Returns number of characters appended. + int concat_size_t(char *__buf, size_t __val) + { + // Long enough for decimal representation. + int __ilen = 3 * sizeof(__val); + char *__cs = static_cast(__builtin_alloca(__ilen)); + int __len = std::__int_to_char(__cs + __ilen, __val, + std::__num_base::_S_atoms_out, + std::ios_base::dec, true); + __builtin_memcpy(__buf, __cs + __ilen - __len, __len); + return __len; + } + +} // __gnu_cxx Index: libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc (revision 202262) +++ libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1308 } +// { dg-error "no matching" "" { target *-*-* } 1331 } #include Index: libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc (revision 202262) +++ libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1349 } +// { dg-error "no matching" "" { target *-*-* } 1372 } #include Index: libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc (revision 202262) +++ libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1234 } +// { dg-error "no matching" "" { target *-*-* } 1257 } #include Index: libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc (revision 202262) +++ libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1234 } +// { dg-error "no matching" "" { target *-*-* } 1257 } #include #include