public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* RFA: Fix sim scache handling to use unsigned values.
@ 2016-02-03 16:25 Nick Clifton
  2016-02-03 18:55 ` Mike Frysinger
  0 siblings, 1 reply; 2+ messages in thread
From: Nick Clifton @ 2016-02-03 16:25 UTC (permalink / raw)
  To: vapier; +Cc: gdb-patches

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;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: RFA: Fix sim scache handling to use unsigned values.
  2016-02-03 16:25 RFA: Fix sim scache handling to use unsigned values Nick Clifton
@ 2016-02-03 18:55 ` Mike Frysinger
  0 siblings, 0 replies; 2+ messages in thread
From: Mike Frysinger @ 2016-02-03 18:55 UTC (permalink / raw)
  To: Nick Clifton; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1014 bytes --]

On 03 Feb 2016 16:25, Nick Clifton wrote:
>   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 ?

seems fine
-mike

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-02-03 18:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-03 16:25 RFA: Fix sim scache handling to use unsigned values Nick Clifton
2016-02-03 18:55 ` Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).