From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 63900 invoked by alias); 27 Aug 2018 14:57:31 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 63043 invoked by uid 89); 27 Aug 2018 14:56:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=*endaddr X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.47.80) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 27 Aug 2018 14:56:26 +0000 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 40DCC48E59 for ; Mon, 27 Aug 2018 09:56:25 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id uIwLfCA9iSjJAuIwLfrTZB; Mon, 27 Aug 2018 09:56:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=+j0nqvtJ4gyAjthbEGCOSAo90SI+4rqF8W31EiLCLQI=; b=smzw97YTko17NVt/UK7WymtEi8 HsyQpzcBuVL4ryxKb0QNcVxhZ4vuHTi6UBuE35FmrgMmPHuVzDYdIT+sXbz7ve1POIofSIrY8qRs0 ZTxOJJONwUIZ7c7p+ZGO6EVoV; Received: from 75-166-85-72.hlrn.qwest.net ([75.166.85.72]:54030 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fuIwK-000csy-Kr; Mon, 27 Aug 2018 09:56:25 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 3/9] Avoid undefined behavior in extract_integer Date: Mon, 27 Aug 2018 14:57:00 -0000 Message-Id: <20180827145620.11055-4-tom@tromey.com> In-Reply-To: <20180827145620.11055-1-tom@tromey.com> References: <20180827145620.11055-1-tom@tromey.com> X-SW-Source: 2018-08/txt/msg00650.txt.bz2 -fsanitize=undefined showed that extract_integer could left-shift a negative value, which is undefined. This patch fixes the problem by doing all the work in an unsigned type, and then using a static_cast at the end of the function. This relies on implementation-defined behavior, but I tend to think we are on safe ground there. (Also, if need be, violations of this could probably be detected, either by configure or by a static_assert.) ChangeLog 2018-08-27 Tom Tromey * findvar.c (extract_integer): Do work in an unsigned type and cast at the end. --- gdb/ChangeLog | 5 +++++ gdb/findvar.c | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/gdb/findvar.c b/gdb/findvar.c index 9256833ab60..f2b84db82a1 100644 --- a/gdb/findvar.c +++ b/gdb/findvar.c @@ -50,7 +50,7 @@ template T extract_integer (const gdb_byte *addr, int len, enum bfd_endian byte_order) { - T retval = 0; + typename std::make_unsigned::type retval = 0; const unsigned char *p; const unsigned char *startaddr = addr; const unsigned char *endaddr = startaddr + len; @@ -86,7 +86,7 @@ That operation is not available on integers of more than %d bytes."), for (; p >= startaddr; --p) retval = (retval << 8) | *p; } - return retval; + return static_cast (retval); } /* Explicit instantiations. */ -- 2.13.6