From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 126235 invoked by alias); 15 Dec 2018 18:10:20 -0000 Mailing-List: contact libc-stable-help@sourceware.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: List-Archive: Sender: libc-stable-owner@sourceware.org Received: (qmail 126214 invoked by uid 89); 15 Dec 2018 18:10:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.2 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: 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; Sat, 15 Dec 2018 18:10:18 +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 83ABBC049599 for ; Sat, 15 Dec 2018 18:10:16 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-116-74.ams2.redhat.com [10.36.116.74]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3DA0B608E0 for ; Sat, 15 Dec 2018 18:10:16 +0000 (UTC) Received: by oldenburg2.str.redhat.com (Postfix, from userid 1000) id 4F33F84EF444; Sat, 15 Dec 2018 19:10:14 +0100 (CET) Date: Mon, 01 Jan 2018 00:00:00 -0000 To: libc-stable@sourceware.org Subject: [2.28 COMMITTED] support: Do not require overflow builtin in support/blob_repeat.c User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20181215181014.4F33F84EF444@oldenburg2.str.redhat.com> From: Florian Weimer X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Sat, 15 Dec 2018 18:10:16 +0000 (UTC) X-IsSubscribed: yes X-SW-Source: 2018-12/txt/msg00019.txt.bz2 It is only available in GCC 5 and later. Tested-by: Romain Naour (cherry picked from commit 0c1719e65b2a5a80331d4f635612799f853b0479) 2018-12-15 Florian Weimer * support/blob_repeat.c (check_mul_overflow_size_t): New function. (minimum_stride_size): Use it. (support_blob_repeat_allocate): Likewise. diff --git a/support/blob_repeat.c b/support/blob_repeat.c index 16c1e448b9..718846d81d 100644 --- a/support/blob_repeat.c +++ b/support/blob_repeat.c @@ -34,6 +34,26 @@ optimization because mappings carry a lot of overhead. */ static const size_t maximum_small_size = 4 * 1024 * 1024; +/* Set *RESULT to LEFT * RIGHT. Return true if the multiplication + overflowed. See . */ +static inline bool +check_mul_overflow_size_t (size_t left, size_t right, size_t *result) +{ +#if __GNUC__ >= 5 + return __builtin_mul_overflow (left, right, result); +#else + /* size_t is unsigned so the behavior on overflow is defined. */ + *result = left * right; + size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2); + if (__glibc_unlikely ((left | right) >= half_size_t)) + { + if (__glibc_unlikely (right != 0 && *result / right != left)) + return true; + } + return false; +#endif +} + /* Internal helper for fill. */ static void fill0 (char *target, const char *element, size_t element_size, @@ -118,8 +138,8 @@ minimum_stride_size (size_t page_size, size_t element_size) common multiple, it appears only once. Therefore, shift one factor. */ size_t multiple; - if (__builtin_mul_overflow (page_size >> common_zeros, element_size, - &multiple)) + if (check_mul_overflow_size_t (page_size >> common_zeros, element_size, + &multiple)) return 0; return multiple; } @@ -255,7 +275,7 @@ support_blob_repeat_allocate (const void *element, size_t element_size, size_t count) { size_t total_size; - if (__builtin_mul_overflow (element_size, count, &total_size)) + if (check_mul_overflow_size_t (element_size, count, &total_size)) { errno = EOVERFLOW; return (struct support_blob_repeat) { 0 };