From patchwork Mon Nov 28 17:00:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61174 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 756AF381D463 for ; Mon, 28 Nov 2022 17:01:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 756AF381D463 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669654879; bh=B6uX56eJAOTsyy4R2Z99GaF7ULEv9fyeCj7eLutG6Z4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hqEfhcM/DsnL/Yy5zfkNd4QHDpJ9fW1ZVlgS/7nExLGVKv1kTml1iz3mjoroL4PPH yVV+CDImOTkFU7gLjxOnrih0aBuEgGlSYXNjb3LYPx+avdHmDGS1EF2BNmjmEzCkEc Wr8ZxQOPYpF+aDR1TcE84zk37Wmr8ZACKuUbSGBo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 75FEE3858418 for ; Mon, 28 Nov 2022 17:00:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 75FEE3858418 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-152-3ps_McqNMtSEZ4k3g_8Y8A-1; Mon, 28 Nov 2022 12:00:09 -0500 X-MC-Unique: 3ps_McqNMtSEZ4k3g_8Y8A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A070780252E; Mon, 28 Nov 2022 17:00:09 +0000 (UTC) Received: from localhost (unknown [10.33.36.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68D78C15BA4; Mon, 28 Nov 2022 17:00:08 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix _Hash_bytes for I16LP32 targets [PR107885] Date: Mon, 28 Nov 2022 17:00:05 +0000 Message-Id: <20221128170005.61262-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux, built on msp430-elf and h8300-elf. Pushed to trunk. -- >8 -- For H8/300 size_t is 32 bits wide, but (unsigned char)buf[2] << 16 promotes to int which is only 16 bits wide. The shift is then undefined. This fixes it by converting to size_t before shifting. libstdc++-v3/ChangeLog: PR libstdc++/107885 * libsupc++/hash_bytes.cc (_Hash_bytes): Convert to size_t instead of implicit integer promotion to 16 bits. --- libstdc++-v3/libsupc++/hash_bytes.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/libsupc++/hash_bytes.cc b/libstdc++-v3/libsupc++/hash_bytes.cc index ffdd04f7602..67e2dbb1a0f 100644 --- a/libstdc++-v3/libsupc++/hash_bytes.cc +++ b/libstdc++-v3/libsupc++/hash_bytes.cc @@ -90,17 +90,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION len -= 4; } + size_t k; // Handle the last few bytes of the input array. switch(len) { case 3: - hash ^= static_cast(buf[2]) << 16; + k = static_cast(buf[2]); + hash ^= k << 16; [[gnu::fallthrough]]; case 2: - hash ^= static_cast(buf[1]) << 8; + k = static_cast(buf[1]); + hash ^= k << 8; [[gnu::fallthrough]]; case 1: - hash ^= static_cast(buf[0]); + k = static_cast(buf[0]); + hash ^= k; hash *= m; };