From patchwork Fri Nov 22 17:38:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Fran=C3=A7ois_Dumont?= X-Patchwork-Id: 1199564 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-514425-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Jc03rQ9O"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="St+g4Qdn"; dkim-atps=neutral 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 47KNvs15P9z9sPV for ; Sat, 23 Nov 2019 04:39:02 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=o+NlXvul3THka+oYMKs5nRk/NV3vRoJfwm9num68fNxe6w4NWZ Yrs2sIgFTEb2tzfMQ7cxHbAKvHRCJVEhlVPlCFutpw1bjlczfNEktd9DCS2SVWns k1sGqWCADRO+rIlX6dl9OFTcJyv4pAtQcwJKCz62BcpvDrOMgl1ppoJV4= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=Xetk1jnHvCIQ8eDPx8APYNeYBzI=; b=Jc03rQ9OGHmFt9S+JWAA 2pOuP8GcaDb+B8oNL9KkZhK1Kq2a0t5eOd0fJSvUjjWxkKTK43K8lSJGqAr/pFhN aKUw5nAq6PtsG0XLfnzsjYNnzOQ2yC65Cz+pKjC8wZGYdx3+FS9fNFgPquQD0cAC /ThiEJ5MbhRmXsX2fcLJ5iI= Received: (qmail 23329 invoked by alias); 22 Nov 2019 17:38:47 -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 23313 invoked by uid 89); 22 Nov 2019 17:38:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.6 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=qualification X-HELO: mail-wr1-f41.google.com Received: from mail-wr1-f41.google.com (HELO mail-wr1-f41.google.com) (209.85.221.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 22 Nov 2019 17:38:45 +0000 Received: by mail-wr1-f41.google.com with SMTP id s5so9612824wrw.2; Fri, 22 Nov 2019 09:38:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=fx/WbEFfygOilBIHUkpz80jkbpGIXkL+2MDaqEPDgsk=; b=St+g4QdnlFfcM2Y3jASYgeOiHdWBNr0R+IeP2mkqmdoIkhH3LTXCopV8v2ONb+hlxO xUD8qDXbSLh9zx3/SVEnfi+0J4TtO/Qe6mDktZr7vAH1o/meXLH/pbG2wHXtwh8C5hYc ok6RgRjcYS99EHBoVIhH8I1bovyUxe9oPjZmUwPdGYVAk2e99pAX7yfcEH/tD4Mv2vAj jmPVu8eM+ofLGA9R9cf5f/lw+6PnkQRRUUTCPtSHI5pP46KzQhFndmVWIyESbzCckk61 rr+aaxIkDFaCZBoEj+Ur9oQXXuW2Dqf4lA5auhrXNHd1S8tMi6fjuZiMw3SbjLgU3+Lz uRoA== Received: from [192.168.42.160] ([92.184.97.131]) by smtp.googlemail.com with ESMTPSA id g11sm8349789wro.2.2019.11.22.09.38.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Nov 2019 09:38:41 -0800 (PST) To: "libstdc++@gcc.gnu.org" , gcc-patches From: =?utf-8?q?Fran=C3=A7ois_Dumont?= Subject: [PATCH][_GLIBCXX_DEBUG] Improve valid_range check Message-ID: <4c480186-d9eb-9749-88aa-6a9a6e5eec3e@gmail.com> Date: Fri, 22 Nov 2019 18:38:39 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 Hi     I noticed that we are not checking that iterators are not singular in valid_range. Moreover __check_singular signature for pointers is not intercepting all kind of pointers in terms of qualification.     I'd like to commit it next week but considering we are in stage 3 I need proper acceptance.     * include/debug/functions.h: Remove include.     (__check_singular_aux, __check_singular): Move...     * include/debug/helper_functions.h:     (__check_singular_aux, __check_singular): ...here.     (__valid_range_aux): Adapt to use latter.     * testsuite/25_algorithms/copy/debug/2_neg.cc: New. Tested under Linux x86_64 normal and debug modes. François diff --git a/libstdc++-v3/include/debug/functions.h b/libstdc++-v3/include/debug/functions.h index 8c385b87244..12df745b573 100644 --- a/libstdc++-v3/include/debug/functions.h +++ b/libstdc++-v3/include/debug/functions.h @@ -29,7 +29,6 @@ #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H #define _GLIBCXX_DEBUG_FUNCTIONS_H 1 -#include // for __addressof #include // for less #if __cplusplus >= 201103L @@ -49,23 +48,6 @@ namespace __gnu_debug template struct _Is_contiguous_sequence : std::__false_type { }; - // An arbitrary iterator pointer is not singular. - inline bool - __check_singular_aux(const void*) { return false; } - - // We may have an iterator that derives from _Safe_iterator_base but isn't - // a _Safe_iterator. - template - inline bool - __check_singular(const _Iterator& __x) - { return __check_singular_aux(std::__addressof(__x)); } - - /** Non-NULL pointers are nonsingular. */ - template - inline bool - __check_singular(const _Tp* __ptr) - { return __ptr == 0; } - /* Checks that [first, last) is a valid range, and then returns * __first. This routine is useful when we can't use a separate * assertion statement because, e.g., we are in a constructor. diff --git a/libstdc++-v3/include/debug/helper_functions.h b/libstdc++-v3/include/debug/helper_functions.h index c3e7478f649..5a858754875 100644 --- a/libstdc++-v3/include/debug/helper_functions.h +++ b/libstdc++-v3/include/debug/helper_functions.h @@ -29,6 +29,7 @@ #ifndef _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H #define _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H 1 +#include // for __addressof #include // for iterator_traits, // categories and _Iter_base #include // for __is_integer @@ -112,6 +113,23 @@ namespace __gnu_debug __get_distance(_Iterator __lhs, _Iterator __rhs) { return __get_distance(__lhs, __rhs, std::__iterator_category(__lhs)); } + // An arbitrary iterator pointer is not singular. + inline bool + __check_singular_aux(const void*) { return false; } + + // We may have an iterator that derives from _Safe_iterator_base but isn't + // a _Safe_iterator. + template + inline bool + __check_singular(_Iterator const& __x) + { return __check_singular_aux(std::__addressof(__x)); } + + /** Non-NULL pointers are nonsingular. */ + template + inline bool + __check_singular(_Tp* const& __ptr) + { return __ptr == 0; } + /** We say that integral types for a valid range, and defer to other * routines to realize what to do with integral types instead of * iterators. @@ -138,14 +156,23 @@ namespace __gnu_debug inline bool __valid_range_aux(_InputIterator __first, _InputIterator __last, std::input_iterator_tag) - { return true; } + { + if (__first != __last) + return !__check_singular(__first) && !__check_singular(__last); + + return true; + } template _GLIBCXX_CONSTEXPR inline bool __valid_range_aux(_InputIterator __first, _InputIterator __last, std::random_access_iterator_tag) - { return __first <= __last; } + { + return + __valid_range_aux(__first, __last, std::input_iterator_tag{}) + && __first <= __last; + } /** We have iterators, so figure out what kind of iterators they are * to see if we can check the range ahead of time. @@ -167,6 +194,9 @@ namespace __gnu_debug typename _Distance_traits<_InputIterator>::__type& __dist, std::__false_type) { + if (!__valid_range_aux(__first, __last, std::input_iterator_tag{})) + return false; + __dist = __get_distance(__first, __last); switch (__dist.second) { diff --git a/libstdc++-v3/testsuite/25_algorithms/copy/debug/2_neg.cc b/libstdc++-v3/testsuite/25_algorithms/copy/debug/2_neg.cc new file mode 100644 index 00000000000..8bbf873de96 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy/debug/2_neg.cc @@ -0,0 +1,37 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library 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. + +// This library 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. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 25.2.1 [lib.alg.copy] Copy. + +// { dg-do run { xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include + +void +test01() +{ + int arr[] = { 0, 1, 2, 3, 4 }; + std::copy((int*)0, arr + 5, arr); +} + +int +main() +{ + test01(); + return 0; +}