From patchwork Mon Jan 6 16:46:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 307372 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.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DE0342C00CA for ; Tue, 7 Jan 2014 03:46:44 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=fbYUpVNsyaoG363ipL8an5+W4k+K6sbgR2hc8kLWwkiFDq 2VWxz6xoPbDAcSZU+dwWgPVN+zVybmZk3HyDLOj0E8xoweG9+zrJnHkJbcYyznVh VtomPMH8bTXFtg8BwcBsnSfEbnw3zU/7IAOCyolTwSE11vnvOi1JKW1y4JBMs= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=OPWMyuiZrtODa5Z/wSFucmvl8FU=; b=hkVUp/CSyXA2gCPN4NvH 1rFPx7H3nkjTUoFwSirMOPlLBn7RWMe39pl7yGa4EO8gbPvz+fC7SnTVvw1r0QW0 UPRGGx2f7w+exGhiu6452WMeRUjkSfg7AbEHNZ3uyt/C9P+XWlD5TpL5/U+YGVf4 gEMzOzZMrZnyurEGnKO9RWk= Received: (qmail 31523 invoked by alias); 6 Jan 2014 16:46:37 -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 31502 invoked by uid 89); 6 Jan 2014 16:46:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ie0-f176.google.com Received: from mail-ie0-f176.google.com (HELO mail-ie0-f176.google.com) (209.85.223.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 06 Jan 2014 16:46:34 +0000 Received: by mail-ie0-f176.google.com with SMTP id at1so18266706iec.7 for ; Mon, 06 Jan 2014 08:46:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-type; bh=1/Ux6mNAzkrnzO7Cd1SF13FjzYvnGiTtNtgi3gUOXvM=; b=LrwAMOMIu4rg4X6ehmvw0TGSOZsOICXi3qU06QIOfeNN45oF+e6Or+grCialbMMI5G 6fxR4/9iX3Be9ZbMQhfmy/124MupLRkXfNRcbIDk2H04/hcbom32Y+nU0gsu9ebDn+Cy svAcZRs7ygQq+jNGkxN5lA6KQiQf0mugzyHo4+abWHkzzpHecpC0sotZ9tDy1sXo6l2Z HuYgHA4B1kNCNzygSE5sB8MIpwBTWosYhdt3exE/rIEx0crJFtynRawQ2rtqNtQBKsKF pQ9F+MttaE/FqKNV3Cj/LHT+bljXXPeLJeA/n4usM38HKSKAlbMxl8vTR/FHObr4uTPN +2Kg== X-Gm-Message-State: ALoCoQkGCJ04xMk8zluXYUT8F7xgE+twxOI6ky3f+djtf99V9lznM4qCj1fe9KxE6L+DstA3klP7mR3tUbGQu9uk88QkXJNcFaG8XGB7xAb3z39LwBUpfUcLQA3u3w8hUenukyiUmyTyN3aqkb7+ckymf0Hu5+frawroFtn0tAMfqSXEckLxoMiOBVHkie2W2+ogvT6HXxrtLe6cxUZxcisGNVV2bx2z+Q== X-Received: by 10.42.214.202 with SMTP id hb10mr3137638icb.76.1389026793116; Mon, 06 Jan 2014 08:46:33 -0800 (PST) MIME-Version: 1.0 Received: by 10.64.241.65 with HTTP; Mon, 6 Jan 2014 08:46:01 -0800 (PST) From: Paul Pluzhnikov Date: Mon, 6 Jan 2014 08:46:01 -0800 Message-ID: Subject: [google][4.8] Add more inexpensive debug checks to vector, bitvector, deque To: gcc-patches , "libstdc++" Greetings, For Google b/9127283, I've committed attached patch on google/gcc-4_8 branch. Related: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56109 This caught ~10 bugs for us. erase(end()) and pop_back() on empty vector appear to be most common. Index: libstdc++-v3/include/bits/stl_vector.h =================================================================== --- libstdc++-v3/include/bits/stl_vector.h (revision 206330) +++ libstdc++-v3/include/bits/stl_vector.h (working copy) @@ -1050,6 +1050,10 @@ void pop_back() { +#if __google_stl_debug_vector + if (this->empty()) + __throw_logic_error(__N("pop_back() on empty vector")); +#endif --this->_M_impl._M_finish; _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish); } @@ -1135,7 +1139,13 @@ */ void insert(iterator __position, size_type __n, const value_type& __x) - { _M_fill_insert(__position, __n, __x); } + { +#if __google_stl_debug_vector + if (__position < this->begin() || __position > this->end()) + __throw_out_of_range(__N("insert() at invalid position")); +#endif + _M_fill_insert(__position, __n, __x); + } /** * @brief Inserts a range into the %vector. @@ -1157,13 +1167,23 @@ void insert(iterator __position, _InputIterator __first, _InputIterator __last) - { _M_insert_dispatch(__position, __first, __last, __false_type()); } + { +#if __google_stl_debug_vector + if (__position < this->begin() || __position > this->end()) + __throw_out_of_range(__N("insert() at invalid position")); +#endif + _M_insert_dispatch(__position, __first, __last, __false_type()); + } #else template void insert(iterator __position, _InputIterator __first, _InputIterator __last) { +#if __google_stl_debug_vector + if (__position < this->begin() || __position > this->end()) + __throw_out_of_range(__N("insert() at invalid position")); +#endif // Check whether it's an integral type. If so, it's not an iterator. typedef typename std::__is_integer<_InputIterator>::__type _Integral; _M_insert_dispatch(__position, __first, __last, _Integral()); Index: libstdc++-v3/include/bits/stl_deque.h =================================================================== --- libstdc++-v3/include/bits/stl_deque.h (revision 206330) +++ libstdc++-v3/include/bits/stl_deque.h (working copy) @@ -1552,7 +1552,13 @@ */ void insert(iterator __position, size_type __n, const value_type& __x) - { _M_fill_insert(__position, __n, __x); } + { +#if __google_stl_debug_deque + if (__position < this->begin() || __position > this->end()) + __throw_logic_error("insert() at invalid position"); +#endif + _M_fill_insert(__position, __n, __x); + } /** * @brief Inserts a range into the %deque. @@ -1570,7 +1576,13 @@ void insert(iterator __position, _InputIterator __first, _InputIterator __last) - { _M_insert_dispatch(__position, __first, __last, __false_type()); } + { +#if __google_stl_debug_deque + if (__position < this->begin() || __position > this->end()) + __throw_logic_error("insert() at invalid position"); +#endif + _M_insert_dispatch(__position, __first, __last, __false_type()); + } #else template void Index: libstdc++-v3/include/bits/vector.tcc =================================================================== --- libstdc++-v3/include/bits/vector.tcc (revision 206330) +++ libstdc++-v3/include/bits/vector.tcc (working copy) @@ -107,6 +107,10 @@ vector<_Tp, _Alloc>:: insert(iterator __position, const value_type& __x) { +#if __google_stl_debug_vector + if (__position < this->begin() || __position > this->end()) + __throw_out_of_range(__N("insert() at invalid position")); +#endif const size_type __n = __position - begin(); if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage && __position == end()) @@ -134,6 +138,10 @@ vector<_Tp, _Alloc>:: erase(iterator __position) { +#if __google_stl_debug_vector + if (__position < this->begin() || __position >= this->end()) + __throw_out_of_range(__N("erase() at invalid position")); +#endif if (__position + 1 != end()) _GLIBCXX_MOVE3(__position + 1, end(), __position); --this->_M_impl._M_finish; @@ -146,6 +154,10 @@ vector<_Tp, _Alloc>:: erase(iterator __first, iterator __last) { +#if __google_stl_debug_vector + if (__first < this->begin() || __first > __last || __last > this->end()) + __throw_out_of_range("erase() invalid range"); +#endif if (__first != __last) { if (__last != end()) @@ -298,6 +310,10 @@ vector<_Tp, _Alloc>:: emplace(iterator __position, _Args&&... __args) { +#if __google_stl_debug_vector + if (__position < this->begin() || __position > this->end()) + __throw_out_of_range(__N("emplace() at invalid position")); +#endif const size_type __n = __position - begin(); if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage && __position == end()) Index: libstdc++-v3/include/bits/deque.tcc =================================================================== --- libstdc++-v3/include/bits/deque.tcc (revision 206330) +++ libstdc++-v3/include/bits/deque.tcc (working copy) @@ -193,6 +193,10 @@ deque<_Tp, _Alloc>:: erase(iterator __position) { +#if __google_stl_debug_deque + if (__position < this->begin() || __position >= this->end()) + __throw_logic_error("erase() at invalid position"); +#endif iterator __next = __position; ++__next; const difference_type __index = __position - begin(); @@ -216,6 +220,10 @@ deque<_Tp, _Alloc>:: erase(iterator __first, iterator __last) { +#if __google_stl_debug_deque + if (__first < this->begin() || __first > __last || __last > this->end()) + __throw_logic_error("erase() invalid range"); +#endif if (__first == __last) return __first; else if (__first == begin() && __last == end()) Index: libstdc++-v3/include/bits/stl_bvector.h =================================================================== --- libstdc++-v3/include/bits/stl_bvector.h (revision 206330) +++ libstdc++-v3/include/bits/stl_bvector.h (working copy) @@ -978,6 +978,10 @@ iterator insert(iterator __position, const bool& __x = bool()) { +#if __google_stl_debug_bvector + if (__position < this->begin() || __position > this->end()) + __throw_logic_error("insert() at invalid position"); +#endif const difference_type __n = __position - begin(); if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage && __position == end()) @@ -997,6 +1001,8 @@ #if __google_stl_debug_bvector if (!this->_M_is_valid()) __throw_logic_error("insert() on corrupt (dangling?) vector"); + if (__position < this->begin() || __position > this->end()) + __throw_logic_error("insert() at invalid position"); #endif _M_insert_dispatch(__position, __first, __last, __false_type()); } @@ -1009,6 +1015,8 @@ #if __google_stl_debug_bvector if (!this->_M_is_valid()) __throw_logic_error("insert() on corrupt (dangling?) vector"); + if (__position < this->begin() || __position > this->end()) + __throw_logic_error("insert() at invalid position"); #endif typedef typename std::__is_integer<_InputIterator>::__type _Integral; _M_insert_dispatch(__position, __first, __last, _Integral()); @@ -1021,6 +1029,8 @@ #if __google_stl_debug_bvector if (!this->_M_is_valid()) __throw_logic_error("insert() on corrupt (dangling?) vector"); + if (__position < this->begin() || __position > this->end()) + __throw_logic_error("insert() at invalid position"); #endif _M_fill_insert(__position, __n, __x); } @@ -1043,7 +1053,8 @@ erase(iterator __position) { #if __google_stl_debug_bvector - _M_range_check(__position - begin()); + if (__position < this->begin() || __position >= this->end()) + __throw_logic_error("erase() at invalid position"); #endif if (__position + 1 != end()) std::copy(__position + 1, end(), __position); @@ -1057,6 +1068,8 @@ #if __google_stl_debug_bvector if (!this->_M_is_valid()) __throw_logic_error("erase() on corrupt (dangling?) vector"); + if (__first < this->begin() || __first > __last || __last > this->end()) + __throw_logic_error("erase() invalid range"); #endif if (__first != __last) _M_erase_at_end(std::copy(__last, end(), __first)); 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 206330) +++ 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 *-*-* } 1290 } +// { dg-error "no matching" "" { target *-*-* } 1310 } #include #include Index: libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc (revision 206330) +++ 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 *-*-* } 1364 } +// { dg-error "no matching" "" { target *-*-* } 1384 } #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 206330) +++ 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 *-*-* } 1405 } +// { dg-error "no matching" "" { target *-*-* } 1425 } #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 206330) +++ 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 *-*-* } 1290 } +// { dg-error "no matching" "" { target *-*-* } 1310 } #include Index: libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/constructor_2_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/constructor_2_neg.cc (revision 206330) +++ libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/constructor_2_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1666 } +// { dg-error "no matching" "" { target *-*-* } 1678 } #include #include Index: libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/assign_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/assign_neg.cc (revision 206330) +++ libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/assign_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1733 } +// { dg-error "no matching" "" { target *-*-* } 1745 } #include Index: libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/insert_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/insert_neg.cc (revision 206330) +++ libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/insert_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1817 } +// { dg-error "no matching" "" { target *-*-* } 1829 } #include Index: libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/constructor_1_neg.cc =================================================================== --- libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/constructor_1_neg.cc (revision 206330) +++ libstdc++-v3/testsuite/23_containers/deque/requirements/dr438/constructor_1_neg.cc (working copy) @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1666 } +// { dg-error "no matching" "" { target *-*-* } 1678 } #include