This is the mail archive of the binutils@sourceware.org mailing list for the binutils 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]

Re: [PATCH] warn about out of range shift counts


>>> Alan Modra <amodra@bigpond.net.au> 16.04.09 09:57 >>>
>On Wed, Apr 15, 2009 at 12:36:55PM +0100, Jan Beulich wrote:
>> gas/
>> 2009-04-15  Jan Beulich  <jbeulich@novell.com>
>> 
>> 	* expr.c: Include limits.h if available, and #define CHAR_BITS
>> 	otherwise.
>> 	(expr): Check range of shift count when evaluating a constant
>> 	expression.
>
>You are including limits.h too early.  It must be somwhere after as.h
>if you expect HAVE_LIMITS_H to be defined.  Otherwise OK.

Ah, okay, never realized that. My general perception is that system
headers should be included before local ones, but that would require that
config.h be included from the compiler command line.

This is the patch I'll commit then:

gas/
2009-04-16  Jan Beulich  <jbeulich@novell.com>

	* expr.c: Include limits.h if available, and #define CHAR_BITS
	otherwise.
	(expr): Check range of shift count when evaluating a constant
	expression.

--- 2009-04-15/gas/expr.c	2008-01-07 14:44:01.000000000 +0100
+++ 2009-04-15/gas/expr.c	2009-04-16 10:43:37.000000000 +0200
@@ -31,6 +31,13 @@
 #include "safe-ctype.h"
 #include "obstack.h"
 
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
+
 static void floating_constant (expressionS * expressionP);
 static valueT generic_bignum_to_int32 (void);
 #ifdef BFD64
@@ -1779,6 +1786,14 @@ expr (int rankarg,		/* Larger # is highe
 	      as_warn (_("division by zero"));
 	      v = 1;
 	    }
+	  if ((valueT) v >= sizeof(valueT) * CHAR_BIT
+	      && (op_left == O_left_shift || op_left == O_right_shift))
+	    {
+	      as_warn_value_out_of_range (_("shift count"), v, 0,
+					  sizeof(valueT) * CHAR_BIT - 1,
+					  NULL, 0);
+	      resultP->X_add_number = v = 0;
+	    }
 	  switch (op_left)
 	    {
 	    default:			abort ();




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