From patchwork Mon May 4 17:10:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 1282861 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=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=tTIWb847; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49G8Wv1Tl9z9sSg for ; Tue, 5 May 2020 03:11:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A0CC93893649; Mon, 4 May 2020 17:10:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by sourceware.org (Postfix) with ESMTPS id D80273892030; Mon, 4 May 2020 17:10:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D80273892030 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nathanmsidwell@gmail.com Received: by mail-qk1-x732.google.com with SMTP id q7so291028qkf.3; Mon, 04 May 2020 10:10:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:to:cc:from:subject:message-id:date:user-agent:mime-version :content-language; bh=o4+TMsX82oVWVSWBu3J5WFou3kTm6z4YYK+NItmH030=; b=tTIWb847u22MU3Mhay0YuijAbKUBFnsDdl95arotjHWsBAt+b4+cKoNqq0m1pp9gQ3 zM/DrLzNWaTqnR3xw9p2jjXtVtfXsM+CO/+dO2gYt0nzPp5eCixYLgV/fmwe2UX/2VLy T/U55WvVzsECeGTCC0inMymXjm88A61BOgvVA2kqow3/h9/boG/ROztfWNY0KBEaYGUi XgASByHV51d+SqADI32EIQZS/D9c5LJRF6JCjeLsSG3clAc99HVAn7ZgIwnKJtPqgZJV Fkig/v8ZipIHmlw1e/hwJ8BxdCN+gTHmd8VYw830iczU+0wURZgrmOuss1RCCr0bDF3x VnAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:to:cc:from:subject:message-id:date :user-agent:mime-version:content-language; bh=o4+TMsX82oVWVSWBu3J5WFou3kTm6z4YYK+NItmH030=; b=CGNQ7FaGpSdZrbuy5uKgiMqq7H8AVkfjTcA+xXdwakkDOfTKt2RcmrtbAhJd1OIAzZ hH3TwmW3aUgjZE/pYwO3n95bm1GxwkYl74wylYNvFWaZvvvkHXho1tPVwHck5YCPghZY 0jpmvRE6Yn0IIXIiNA9SIGa/KQTLv1DSx8cgOPkcmEyq0gMDP2b81MliXUR4olMF+pCB xTdEQskQQ641J2On2hd7kiLsRTA9pUvsu6Kaj3OqByXoxLKgQLyWoCBYxm3zNzvUPaAx ziWN2skh6gN/dLVmF46iqLJUboknc7KCiupT6nV+MMbNIXByuOvrK7vRGNQA8tWgNk4h GryA== X-Gm-Message-State: AGi0PuZrCts4fRhU+6NcB0hbVRd8dF35aI/RtAKEDq16/I6aG+oeMzba XNJCSO0XHN9OIFF7WufCDiE= X-Google-Smtp-Source: APiQypKit0x0Nq9ONicX+Gyv/uEEJL6TLCrRIRGD8Xysf0aXVVdDAmas1qq1kVP57FL33eFJ9p/nLA== X-Received: by 2002:ae9:c311:: with SMTP id n17mr159264qkg.201.1588612244135; Mon, 04 May 2020 10:10:44 -0700 (PDT) Received: from ?IPv6:2620:10d:c0a8:1102:a154:74c:e2c0:f823? ([2620:10d:c091:480::1:fb5b]) by smtp.googlemail.com with ESMTPSA id c68sm3173883qke.129.2020.05.04.10.10.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 May 2020 10:10:42 -0700 (PDT) To: "libstdc++@gcc.gnu.org" From: Nathan Sidwell Subject: [PR libstdc++/94747] negating a size_t does not show intent Message-ID: <7064cb4c-f45b-5e5b-c6eb-ff14170c4cb2@acm.org> Date: Mon, 4 May 2020 13:10:41 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: GCC Patches Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Pushed this patch to dynamic_cast. We negate an offsetof value, which although 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. nathan 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. diff --git i/libstdc++-v3/libsupc++/dyncast.cc w/libstdc++-v3/libsupc++/dyncast.cc index 1254471f6e8..7a5f483f9cf 100644 --- i/libstdc++-v3/libsupc++/dyncast.cc +++ w/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);