From patchwork Wed Jan 20 17:23:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 1429338 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; 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=R6aBLYxX; dkim-atps=neutral Received: from sourceware.org (unknown [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 4DLXS53zvZz9sCD for ; Thu, 21 Jan 2021 04:23:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82076396E475; Wed, 20 Jan 2021 17:23:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qv1-xf33.google.com (mail-qv1-xf33.google.com [IPv6:2607:f8b0:4864:20::f33]) by sourceware.org (Postfix) with ESMTPS id 344EB385782D for ; Wed, 20 Jan 2021 17:23:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 344EB385782D 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-qv1-xf33.google.com with SMTP id l14so11245792qvh.2 for ; Wed, 20 Jan 2021 09:23:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=RwDoT3q/GlShqWPsBx6m8D0MOdUrrsDmvmkSzVOQE/k=; b=R6aBLYxXZofCO1WTobdsa5gRSwGCu/7aamCeL7MK7VbLAcr/1LTKHQsLlZYGdSZGRE rAFuIwTwkGtn5LwUOyjK691hV+4bjfxJbOHESxAzlxyUftPhRlVbolVQQEGmQGks6QVs /1Qy8RpfRqJ/f8E6o3dvQR+dzzHmwdiKUDvrKXQ/FGPPCXjbiauNNiuS7dJumi3RGDTc XWLh0v/27z10JkArMFwrSH7I/rxyYN+hwwK4RlKqClAu7bo4aqmKzOxgyW7Ee0uc3rR0 0QuGSG/FuCKygLd+76wwzq7nEum+4MQZXT9JQ2kMdqJnc1aLYFNtlcsEUmrnlOHJlNz/ riZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:to:from:subject:message-id:date :user-agent:mime-version:content-language; bh=RwDoT3q/GlShqWPsBx6m8D0MOdUrrsDmvmkSzVOQE/k=; b=iPpO28DlAKxaGMY9WgX/msiUkOmeyu+avktikT1h3nRdqAFZMb1So6FwELWDWa+K8I yNOogGfuOfcGbg7kSKGSZnJ/pG/Xz5+r+p1mcLpY+lLhL965raazEaQsjpfWPWzdo2hn EH+WGpQFJA9fzbkhn95mcvSNfGmeaK7D8R3IY7XTnHheXuif6y4g+x4nLad9L/SPcGXK MwWxTTAriecmmVW8D73HIJZ6SFEyodjZta6FkfEvdAkEiKkN/ME9kzlVJL6kQoaCYVMI 0TXp1DlAloV6u0W1/YDTVZN3BWeVqkdzLCtJMF3F7Iiq7HtV/u2cvHtD5G6gUCmMQ3mZ H0tw== X-Gm-Message-State: AOAM533OPrCQsXm6+VWqCNPoJoCSeK14yCqJCuNp9PKD87YYkeE1VfnP pTGvULfRiOwaGfZnQd272aM= X-Google-Smtp-Source: ABdhPJwGVB6r6CQHET0f+DjNIry4UMbKq3Uo9mcCEsOVedAL1cfx/Gs55770G/qH9gIJocsZN4xIIQ== X-Received: by 2002:a0c:b48c:: with SMTP id c12mr10314794qve.9.1611163418769; Wed, 20 Jan 2021 09:23:38 -0800 (PST) Received: from ?IPv6:2620:10d:c0a8:1102:8589:5b92:16d9:3ee0? ([2620:10d:c091:480::1:a161]) by smtp.googlemail.com with ESMTPSA id k64sm1767881qkc.110.2021.01.20.09.23.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Jan 2021 09:23:37 -0800 (PST) To: GCC Patches From: Nathan Sidwell Subject: c++: Avoid UB in signed shift [PR 98625] Message-ID: <87d03737-1a59-45f8-4f41-9ea103c6b096@acm.org> Date: Wed, 20 Jan 2021 12:23:36 -0500 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.6 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: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Thanks Jakub & Martin for unconfusing me about where the UB was! I'd forgotten that left shifting a negative value is UB until C++20. Insert some casts to do unsigned shifts. PR c++/98625 gcc/cp/ * module.cc (bytes_in::i, bytes_in::wi): Avoid left shift of signed type. diff --git i/gcc/cp/module.cc w/gcc/cp/module.cc index 1fd0bcfe3eb..8f9c7940ef8 100644 --- i/gcc/cp/module.cc +++ w/gcc/cp/module.cc @@ -881,9 +881,12 @@ bytes_in::i () v &= 0xf; if (v & 0x8) v |= -1 ^ 0x7; + /* unsigned necessary due to left shifts of -ve values. */ + unsigned uv = unsigned (v); if ((ptr = read (++bytes))) while (bytes--) - v = (v << 8) | (*ptr++ & 0xff); + uv = (uv << 8) | (*ptr++ & 0xff); + v = int (uv); } else if (v & 0x40) v |= -1 ^ 0x3f; @@ -972,9 +975,12 @@ bytes_in::wi () v &= 0xf; if (v & 0x8) v |= -1 ^ 0x7; + /* unsigned necessary due to left shifts of -ve values. */ + unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v; if ((ptr = read (++bytes))) while (bytes--) - v = (v << 8) | (*ptr++ & 0xff); + uv = (uv << 8) | (*ptr++ & 0xff); + v = (HOST_WIDE_INT) uv; } else if (v & 0x40) v |= -1 ^ 0x3f;