From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 0AD1E385B835 for ; Sat, 11 Apr 2020 01:08:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0AD1E385B835 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark@simark.ca Received: from [10.0.0.11] (unknown [192.222.164.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 7F6541E5F9; Fri, 10 Apr 2020 21:08:00 -0400 (EDT) Subject: Re: [PATCH] Undefined behavior reported in copy_bitwise From: Simon Marchi To: krokus , gdb-patches@sourceware.org References: <2ea2b4f7-b9f2-99c4-b301-e35d9f265690@simark.ca> <6e630686-ab85-1ef2-c078-a2b72f85eeb8@simark.ca> Message-ID: Date: Fri, 10 Apr 2020 21:08:00 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <6e630686-ab85-1ef2-c078-a2b72f85eeb8@simark.ca> Content-Type: text/plain; charset=utf-8 Content-Language: fr Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-24.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 11 Apr 2020 01:08:03 -0000 On 2020-04-10 5:24 p.m., Simon Marchi wrote: > On 2020-04-10 4:56 p.m., krokus via Gdb-patches wrote: >> Simon, >> >>> Could you please provide a ChangeLog entry for this, with the name and >>> email address you'd like to have in there? >> >> Will do. >> Do I need to update the ChangeLog myself or you could do this with my entry? >> I'm not set up for repo commit access. >> > > Just provide it here in the email body, I'll take care of formatting and pushing > the patch. Thanks, I've pushed the patch as below. I've modified the ChangeLog entry a bit to more clearly state what changed in the affected function, but that's it. >From cf83625da29d1239e97f1eb4d145f347cb741889 Mon Sep 17 00:00:00 2001 From: Artur Shepilko Date: Fri, 10 Apr 2020 10:56:43 -0400 Subject: [PATCH] gdb: fix undefined behavior reported in copy_bitwise gdb version 9.1, built with clang 8.0.0 on Ubuntu 18.04 (x86_64); --enable-ubsan (for clang's undefined behavior sanitizer) Executing command; `maint selftest copy_bitwise` bombs in runtime error: ../../gdb/utils.c:3432:28: runtime error: left shift of negative value -1 Closer look reveals the offending shift: `(~0 << nbits)`, apparently 0 is treated as signed int, resulting in negative complement. Explicitly stating it unsigned 0U fixes it and the `copy_bitwise` test passes ok. --- gdb/ChangeLog | 5 +++++ gdb/utils.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 661a41467bbe..81102ee569bc 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2020-04-10 Artur Shepilko + + * utils.c (copy_bitwise): Use unsigned 0 constant as operand of + bit shift. + 2020-04-10 Tom Tromey * symfile.c (symbol_file_add_separate): Preserve OBJF_MAINLINE. diff --git a/gdb/utils.c b/gdb/utils.c index bda6bbf5b0e7..f5b20331b1e2 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3433,7 +3433,7 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset, buf |= *source << avail; buf &= (1 << nbits) - 1; - *dest = (*dest & (~0 << nbits)) | buf; + *dest = (*dest & (~0U << nbits)) | buf; } } -- 2.26.0