From patchwork Wed Feb 3 16:25:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Clifton X-Patchwork-Id: 10704 Received: (qmail 112876 invoked by alias); 3 Feb 2016 16:25:54 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 112866 invoked by uid 89); 3 Feb 2016 16:25:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Round, rounding, nearest 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 03 Feb 2016 16:25:52 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 0370AC0AC927; Wed, 3 Feb 2016 16:25:51 +0000 (UTC) Received: from littlehelper.redhat.com (vpn1-6-166.ams2.redhat.com [10.36.6.166]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u13GPnfJ025022 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 3 Feb 2016 11:25:50 -0500 From: Nick Clifton To: vapier@gentoo.org Cc: gdb-patches@sourceware.org Subject: RFA: Fix sim scache handling to use unsigned values. Date: Wed, 03 Feb 2016 16:25:48 +0000 Message-ID: <87r3gtkbg3.fsf@redhat.com> MIME-Version: 1.0 Hi Mike, A recent bug report on the binutils list pointed out that a construct like this: for (i = 1; i; i <<= 1) can have undefined behaviour if the loop variable is signed and the shift operation moves a 1 into the sign bit: lists.gnu.org/archive/html/bug-binutils/2016-02/msg00006.html A quick scan of the simulator sources found one place where this happens, in common/cgen-scache.c:scache_option_handler(). Looking at the code here it seemed to me that the simplest fix is to switch to using an unsigned int for the parsed value of the scache size. (A negative scache size does not make any sense right ?) I wondered if it would be worth using "unsigned long" instead of "unsigned int", since there is no strtou() function, but this seemed like overkill. So I created the patch below. It builds OK, but I am not sure how to test it. Any suggestions ? Cheers Nick diff --git a/sim/common/cgen-scache.c b/sim/common/cgen-scache.c index 3a79514..cd1aa11 100644 --- a/sim/common/cgen-scache.c +++ b/sim/common/cgen-scache.c @@ -121,24 +121,26 @@ scache_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, { if (arg != NULL) { - int n = strtol (arg, NULL, 0); + unsigned int n = (unsigned int) strtoul (arg, NULL, 0); if (n < MIN_SCACHE_SIZE) { - sim_io_eprintf (sd, "invalid scache size `%d', must be at least 4", n); + sim_io_eprintf (sd, "invalid scache size `%u', must be at least %u", + n, MIN_SCACHE_SIZE); return SIM_RC_FAIL; } /* Ensure it's a multiple of 2. */ if ((n & (n - 1)) != 0) { - sim_io_eprintf (sd, "scache size `%d' not a multiple of 2\n", n); - { - /* round up to nearest multiple of 2 */ - int i; - for (i = 1; i < n; i <<= 1) - continue; - n = i; - } - sim_io_eprintf (sd, "rounding scache size up to %d\n", n); + unsigned int i; + sim_io_eprintf (sd, "scache size `%u' not a multiple of 2\n", n); + /* Round up to nearest multiple of 2. */ + for (i = 1; i && i < n; i <<= 1) + continue; + if (i) + { + n = i; + sim_io_eprintf (sd, "rounding scache size up to %u\n", n); + } } if (cpu == NULL) STATE_SCACHE_SIZE (sd) = n;