From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2066) id 97132394C03F; Tue, 12 May 2020 06:48:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97132394C03F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1589266097; bh=ALttz7RegoMf7uO2IMV5WsXFYhErGCHsNNHpHkW9Ki4=; h=From:To:Subject:Date:From; b=kkUpiqwGPGVRuFNW8jG4MBipDb8CZIEMFZi+wCm/xdkIkY5TEJZi/J/VQ/Po6/9uW 6bLyJrtknBMr/i/28UgL5Z3QTYgsWhPoZSYWcjE6aI1vtZYyAWrxyDIpogy9l0kGqh x4K9L44LlZmpQst5FvdGTTIM8GmA0GEAn/qgmTOw= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jiu Fu Guo To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/guojiufu/heads/guojiufu-branch)] libstdc++: Avoid negating a size_t [pr 94747] X-Act-Checkin: gcc X-Git-Author: Nathan Sidwell X-Git-Refname: refs/users/guojiufu/heads/guojiufu-branch X-Git-Oldrev: bb6ce5422066b434f51f6475335788541fd82543 X-Git-Newrev: e6b31fc717207565f144aaefa608344789221f07 Message-Id: <20200512064817.97132394C03F@sourceware.org> Date: Tue, 12 May 2020 06:48:17 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 May 2020 06:48:17 -0000 https://gcc.gnu.org/g:e6b31fc717207565f144aaefa608344789221f07 commit e6b31fc717207565f144aaefa608344789221f07 Author: Nathan Sidwell Date: Mon May 4 10:06:40 2020 -0700 libstdc++: Avoid negating a size_t [pr 94747] Although the code here is well formed, it doesn't show intent well. The reason checkers trigger on this is that it is a cause of real bugs. So, negate a ptrdiff_t instead. * libsupc++/dyncast.cc (__dynamic_cast): Cast offsetof to ptrdiff_t before negation, to show intent more clearly. Diff: --- libstdc++-v3/ChangeLog | 6 ++++++ libstdc++-v3/libsupc++/dyncast.cc | 11 ++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0624bb733dd..739ab9eeb29 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,9 @@ +2020-05-04 Nathan Sidwell + + PR libstdc++/94747 + * libsupc++/dyncast.cc (__dynamic_cast): Cast offsetof to + ptrdiff_t before negation, to show intent more clearly. + 2020-05-04 Jonathan Wakely PR libstdc++/94936 diff --git a/libstdc++-v3/libsupc++/dyncast.cc b/libstdc++-v3/libsupc++/dyncast.cc index 1254471f6e8..7a5f483f9cf 100644 --- a/libstdc++-v3/libsupc++/dyncast.cc +++ b/libstdc++-v3/libsupc++/dyncast.cc @@ -49,8 +49,8 @@ __dynamic_cast (const void *src_ptr, // object started from { const void *vtable = *static_cast (src_ptr); const vtable_prefix *prefix = - adjust_pointer (vtable, - -offsetof (vtable_prefix, origin)); + (adjust_pointer + (vtable, -ptrdiff_t (offsetof (vtable_prefix, origin)))); const void *whole_ptr = adjust_pointer (src_ptr, prefix->whole_object); const __class_type_info *whole_type = prefix->whole_type; @@ -63,8 +63,8 @@ __dynamic_cast (const void *src_ptr, // object started from // segfault later trying to use a vbase offset that doesn't exist. const void *whole_vtable = *static_cast (whole_ptr); const vtable_prefix *whole_prefix = - adjust_pointer (whole_vtable, - -offsetof (vtable_prefix, origin)); + (adjust_pointer + (whole_vtable, -ptrdiff_t (offsetof (vtable_prefix, origin)))); if (whole_prefix->whole_type != whole_type) return NULL; @@ -75,7 +75,8 @@ __dynamic_cast (const void *src_ptr, // object started from if (contained_public_p (result.dst2src)) // Src is known to be a public base of dst. return const_cast (result.dst_ptr); - if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst))) + if (contained_public_p (__class_type_info::__sub_kind + (result.whole2src & result.whole2dst))) // Both src and dst are known to be public bases of whole. Found a valid // cross cast. return const_cast (result.dst_ptr);