[Mono-list] Bug 70588

R. C. James Harlow james@wrong.nu
Wed, 09 Feb 2005 02:18:19 +0000


--=-4zjQI1xYzmDbcGai9zo7
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Size of types aren't checked when an integer literal is stored in an
unsigned byte. Fixing that exposes a problem where an incorrect error is
given on similar overflows; this patch checks this and raises the right
error. A test case for this is also included.

What should I do in terms of bugzilla - should I create myself an
account and add this patch there?

Thanks,
	james.

--=-4zjQI1xYzmDbcGai9zo7
Content-Description: 
Content-Disposition: attachment; filename="bug 70588.patch"
Content-Type: text/x-patch; charset=us-ascii
Content-Transfer-Encoding: 7bit

Index: mcs/convert.cs
===================================================================
--- mcs/convert.cs	(revision 40317)
+++ mcs/convert.cs	(working copy)
@@ -606,7 +606,7 @@
 					if (value >= SByte.MinValue && value <= SByte.MaxValue)
 						return true;
 				} else if (target_type == TypeManager.byte_type){
-					if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
+					if (value >= Byte.MinValue && value <= Byte.MaxValue)
 						return true;
 				} else if (target_type == TypeManager.short_type){
 					if (value >= Int16.MinValue && value <= Int16.MaxValue)
@@ -1268,6 +1268,38 @@
 				      TypeManager.CSharpName (target));
 		}
 
+		static public bool ConversionOverflows(EmitContext ec, Expression expr, Type target_type) {
+			if (expr is IntConstant){
+				int value = ((IntConstant) expr).Value;
+
+				if (target_type == TypeManager.sbyte_type){
+					if (value < SByte.MinValue || value > SByte.MaxValue)
+						return true;
+				} else if (target_type == TypeManager.byte_type){
+					if (value < Byte.MinValue || value > Byte.MaxValue)
+						return true;
+				} else if (target_type == TypeManager.short_type){
+					if (value < Int16.MinValue || value > Int16.MaxValue)
+						return true;
+				} else if (target_type == TypeManager.ushort_type){
+					if (value < UInt16.MinValue || value > UInt16.MaxValue)
+						return true;
+				} else if (target_type == TypeManager.uint32_type){
+					if (value < 0)
+						return true;
+				} else if (target_type == TypeManager.uint64_type){
+					if (value < 0)
+						return true;
+				}
+			}
+			return false;
+		}
+		
+		static public void Error_ConversionOverflows (Location loc, Expression source, Type target) {
+			Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+				((IntConstant)source).Value, TypeManager.CSharpName (target));
+		}
+
 		/// <summary>
 		///   Attempts to implicitly convert `source' into `target_type', using
 		///   ImplicitConversion.  If there is no implicit conversion, then
Index: mcs/assign.cs
===================================================================
--- mcs/assign.cs	(revision 40317)
+++ mcs/assign.cs	(working copy)
@@ -462,6 +462,10 @@
 					//
 					if (Convert.ImplicitStandardConversionExists (ec, a.original_source, target_type))
 						return this;
+					else if (Convert.ConversionOverflows(ec, a.original_source, target_type)) {
+						Convert.Error_ConversionOverflows(loc, a.original_source, target_type);
+						return null;
+					}
 
 					//
 					// In the spec 2.4 they added: or if type of the target is int
Index: errors/cs0031-2.cs
===================================================================
--- errors/cs0031-2.cs	(revision 0)
+++ errors/cs0031-2.cs	(revision 0)
@@ -0,0 +1,12 @@
+
+
+using System;
+
+public class T {
+
+	public static void Main () {
+		byte b = 0;
+		b |= ~0x01;
+	}
+}
+
Index: errors/cs0031.cs
===================================================================
--- errors/cs0031.cs	(revision 40317)
+++ errors/cs0031.cs	(working copy)
@@ -1,14 +0,0 @@
-// cs0031-1.cs : Constant value '256' cannot be converted to a byte
-// Line : 7
-
-public class Blah {
-
-	public enum MyEnum : byte {
-		Foo = 256,
-		Bar
-	}
-
-	public static void Main ()
-	{
-	}
-}
Index: errors/cs0031-1.cs
===================================================================
--- errors/cs0031-1.cs	(revision 40317)
+++ errors/cs0031-1.cs	(working copy)
@@ -0,0 +1,14 @@
+// cs0031-1.cs : Constant value '256' cannot be converted to a byte
+// Line : 7
+
+public class Blah {
+
+	public enum MyEnum : byte {
+		Foo = 256,
+		Bar
+	}
+
+	public static void Main ()
+	{
+	}
+}


--=-4zjQI1xYzmDbcGai9zo7--