This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Prevent possible undefined behaviour computing the size of the scache by usingunsigned integers inst


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=13754e4c3d1b78945ecba225216f29d71334b77d

commit 13754e4c3d1b78945ecba225216f29d71334b77d
Author: Nick Clifton <nickc@redhat.com>
Date:   Thu Feb 4 16:27:06 2016 +0000

    Prevent possible undefined behaviour computing the size of the scache by usingunsigned integers instead of signed integers.
    
    	* cgen-scache.c (scache_option_handler): Prevent possible
    	undefined behaviour computing the size of the scache by using
    	unsigned integers instead of signed integers.

Diff:
---
 sim/common/ChangeLog     |  6 ++++++
 sim/common/cgen-scache.c | 24 +++++++++++++-----------
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/sim/common/ChangeLog b/sim/common/ChangeLog
index fe25803..0483f8a 100644
--- a/sim/common/ChangeLog
+++ b/sim/common/ChangeLog
@@ -1,3 +1,9 @@
+2016-02-04  Nick Clifton  <nickc@redhat.com>
+
+	* cgen-scache.c (scache_option_handler): Prevent possible
+	undefined behaviour computing the size of the scache by using
+	unsigned integers instead of signed integers.
+
 2016-01-17  Joel Brobecker  <brobecker@adacore.com>
 
 	* sim-fpu.c: Minor comment fixes throughout.
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;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]