From patchwork Tue Jun 18 11:39:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1117812 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-503168-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="kQQv6McA"; 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 45SmNH4QJ8z9s3C for ; Tue, 18 Jun 2019 21:40:15 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=Q1udor7DstWH24QPPWXyu+Fl6WdjC1fpmeeWXta9MZpXstZ9annFk sG3PZmjjatvig2gUrFyPJq0l8uTnFBOvM9oK045289V9BgDGyCVjtZ8BmH9q2a06 6N+JWWsuPQpl5lVwCh6nDwJ6BfrMUBfpqkFeqsDBq8Sh9hzWix6xko= 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:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=Jw6i54hJcXtljF5yMj1d9DzrAZI=; b=kQQv6McAyKt1Y3WIKMKb ifq9vTm9c4dkJhmlDxbNb4ClhpY+CAkaqCOVhL1TXEjBQMjMuWPR6mWxr1E/kBhf Nf3UbduzvKITD9e9LqNs4Wg4bbgCq2QgO/hcA1B2Lnfj/pxNcP6EHHrOeDeRx4ja pQoUI6B2vzHmvJEKd7fkMYk= Received: (qmail 6730 invoked by alias); 18 Jun 2019 11:40:07 -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 6698 invoked by uid 89); 18 Jun 2019 11:40:07 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 18 Jun 2019 11:40:05 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6C94130860A7; Tue, 18 Jun 2019 11:39:59 +0000 (UTC) Received: from localhost (unknown [10.33.36.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1DAEC82A3E; Tue, 18 Jun 2019 11:39:58 +0000 (UTC) Date: Tue, 18 Jun 2019 12:39:58 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Avoid undefined behaviour in std::byte operators (LWG 2950) Message-ID: <20190618113958.GA26819@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.11.3 (2019-02-01) * include/c_global/cstddef (std::byte): Perform arithmetic operations in unsigned int to avoid promotion (LWG 2950). Tested x86_64-linux, committed to trunk. commit bfa356b2a9353d1f0b7ccc38f3787d5a4f3044ae Author: redi Date: Tue Jun 18 11:39:43 2019 +0000 Avoid undefined behaviour in std::byte operators (LWG 2950) * include/c_global/cstddef (std::byte): Perform arithmetic operations in unsigned int to avoid promotion (LWG 2950). git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@272415 138bc75d-0d04-0410-961f-82ee72b054a4 diff --git a/libstdc++-v3/include/c_global/cstddef b/libstdc++-v3/include/c_global/cstddef index 8c779ec354d..c94c938f6f3 100644 --- a/libstdc++-v3/include/c_global/cstddef +++ b/libstdc++-v3/include/c_global/cstddef @@ -120,71 +120,53 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template using __byte_op_t = typename __byte_operand<_IntegerType>::__type; - template - constexpr __byte_op_t<_IntegerType>& - operator<<=(byte& __b, _IntegerType __shift) noexcept - { return __b = byte(static_cast(__b) << __shift); } - template constexpr __byte_op_t<_IntegerType> operator<<(byte __b, _IntegerType __shift) noexcept - { return byte(static_cast(__b) << __shift); } - - template - constexpr __byte_op_t<_IntegerType>& - operator>>=(byte& __b, _IntegerType __shift) noexcept - { return __b = byte(static_cast(__b) >> __shift); } + { return (byte)(unsigned char)((unsigned)__b << __shift); } template constexpr __byte_op_t<_IntegerType> operator>>(byte __b, _IntegerType __shift) noexcept - { return byte(static_cast(__b) >> __shift); } - - constexpr byte& - operator|=(byte& __l, byte __r) noexcept - { - return __l = - byte(static_cast(__l) | static_cast(__r)); - } + { return (byte)(unsigned char)((unsigned)__b >> __shift); } constexpr byte operator|(byte __l, byte __r) noexcept - { - return - byte(static_cast(__l) | static_cast(__r)); - } - - constexpr byte& - operator&=(byte& __l, byte __r) noexcept - { - return __l = - byte(static_cast(__l) & static_cast(__r)); - } + { return (byte)(unsigned char)((unsigned)__l | (unsigned)__r); } constexpr byte operator&(byte __l, byte __r) noexcept - { - return - byte(static_cast(__l) & static_cast(__r)); - } - - constexpr byte& - operator^=(byte& __l, byte __r) noexcept - { - return __l = - byte(static_cast(__l) ^ static_cast(__r)); - } + { return (byte)(unsigned char)((unsigned)__l & (unsigned)__r); } constexpr byte operator^(byte __l, byte __r) noexcept - { - return - byte(static_cast(__l) ^ static_cast(__r)); - } + { return (byte)(unsigned char)((unsigned)__l ^ (unsigned)__r); } constexpr byte operator~(byte __b) noexcept - { return byte(~static_cast(__b)); } + { return (byte)(unsigned char)~(unsigned)__b; } + + template + constexpr __byte_op_t<_IntegerType>& + operator<<=(byte& __b, _IntegerType __shift) noexcept + { return __b = __b << __shift; } + + template + constexpr __byte_op_t<_IntegerType>& + operator>>=(byte& __b, _IntegerType __shift) noexcept + { return __b = __b >> __shift; } + + constexpr byte& + operator|=(byte& __l, byte __r) noexcept + { return __l = __l | __r; } + + constexpr byte& + operator&=(byte& __l, byte __r) noexcept + { return __l = __l & __r; } + + constexpr byte& + operator^=(byte& __l, byte __r) noexcept + { return __l = __l ^ __r; } template constexpr _IntegerType