From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 45C96382E90F for ; Thu, 26 May 2022 21:32:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 45C96382E90F 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-638-kj-UPI8uPzipV-p4tqqwDw-1; Thu, 26 May 2022 17:32:16 -0400 X-MC-Unique: kj-UPI8uPzipV-p4tqqwDw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4FBBC858EFE; Thu, 26 May 2022 21:32:16 +0000 (UTC) Received: from localhost (unknown [10.33.36.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 18C0A1121315; Thu, 26 May 2022 21:32:16 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix narrowing conversions for 16-bit size_t [PR105681] Date: Thu, 26 May 2022 22:32:15 +0100 Message-Id: <20220526213215.1314091-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 May 2022 21:32:19 -0000 Tested x86_64-linux, bootstrapped msp430-elf, pushed to trunk. -- >8 -- On a 16-bit target such as msp430 we get errors about narrowing long values to size_t, which is only 16-bit. When --enable-libstdcxx-pch is used the header breaks the build because of these narrowing errors. libstdc++-v3/ChangeLog: PR libstdc++/105681 * include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp: Limit ga_sizes array to values that fit in size_t. * include/ext/random [__SIZE_WIDTH < 32] (sfmt86243) (sfmt86243_64, sfmt132049, sfmt132049_64, sfmt216091) (sfmt216091_64): Do not declare. --- .../hash_prime_size_policy_imp.hpp | 18 +++++++++++++++--- libstdc++-v3/include/ext/random | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp b/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp index d53392c8211..43d72768206 100644 --- a/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp +++ b/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp @@ -46,15 +46,23 @@ namespace detail { enum { + num_distinct_sizes_16_bit = 14, num_distinct_sizes_32_bit = 30, num_distinct_sizes_64_bit = 62, - num_distinct_sizes = sizeof(std::size_t) != 8 ? - num_distinct_sizes_32_bit : num_distinct_sizes_64_bit, + // The number of values is limited by the width of size_t. + // Maybe we could just use (__SIZE_WIDTH__ - 2) here. +#if __SIZE_WIDTH__ >= 64 + num_distinct_sizes = num_distinct_sizes_64_bit +#elif __SIZE_WIDTH__ >= 32 + num_distinct_sizes = num_distinct_sizes_32_bit +#else + num_distinct_sizes = num_distinct_sizes_16_bit +#endif }; // Originally taken from the SGI implementation; acknowledged in the docs. // Further modified (for 64 bits) from tr1's hashtable. - static const std::size_t g_a_sizes[num_distinct_sizes_64_bit] = + static const std::size_t g_a_sizes[num_distinct_sizes] = { /* 0 */ 5ul, /* 1 */ 11ul, @@ -70,6 +78,7 @@ namespace detail /* 11 */ 14033ul, /* 12 */ 28411ul, /* 13 */ 57557ul, +#if __SIZE_WIDTH__ >= 32 /* 14 */ 116731ul, /* 15 */ 236897ul, /* 16 */ 480881ul, @@ -86,6 +95,7 @@ namespace detail /* 27 */ 1164186217ul, /* 28 */ 2364114217ul, /* 29 */ 4294967291ul, +#if __SIZE_WIDTH__ >= 64 /* 30 */ (std::size_t)8589934583ull, /* 31 */ (std::size_t)17179869143ull, /* 32 */ (std::size_t)34359738337ull, @@ -118,6 +128,8 @@ namespace detail /* 59 */ (std::size_t)4611686018427387847ull, /* 60 */ (std::size_t)9223372036854775783ull, /* 61 */ (std::size_t)18446744073709551557ull, +#endif +#endif }; } // namespace detail diff --git a/libstdc++-v3/include/ext/random b/libstdc++-v3/include/ext/random index b7155767b02..50505b876cc 100644 --- a/libstdc++-v3/include/ext/random +++ b/libstdc++-v3/include/ext/random @@ -346,6 +346,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION 0xa3ac4000U, 0xecc1327aU> sfmt44497_64; +#if __SIZE_WIDTH__ >= 32 typedef simd_fast_mersenne_twister_engine sfmt216091_64; +#endif // __SIZE_WIDTH__ >= 32 #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -- 2.34.3