Browse Source

Merge branch 'fix-numeric-conversion-out-of-bounds' of https://github.com/ixjf/moonsharp into pr/ixjf-set

Xanathar 6 years ago
parent
commit
7b8103d58b

+ 20 - 0
src/MoonSharp.Interpreter.Tests/EndToEnd/SimpleTests.cs

@@ -1532,5 +1532,25 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 //#endif
 //#endif
 //		}
 //		}
 
 
+		[Test]
+        	public void NumericConversionFailsIfOutOfBounds()
+        	{
+            		Script S = new Script();
+
+            		S.Globals["my_function_takes_byte"] = (Action<byte>)(p => { });
+
+            		try
+            		{
+                		S.DoString("my_function_takes_byte(2010191) -- a huge number that is definitely not a byte");
+
+                		Assert.Fail(); // ScriptRuntimeException should have been thrown, if it doesn't Assert.Fail should execute
+            		}
+            		catch (ScriptRuntimeException e)
+            		{
+                		//Assert.Pass(e.DecoratedMessage);
+            		}
+        	}
+
+
 	}
 	}
 }
 }

+ 35 - 23
src/MoonSharp.Interpreter/Interop/Converters/NumericConversions.cs

@@ -43,17 +43,25 @@ namespace MoonSharp.Interpreter.Interop.Converters
 		{
 		{
 			type = Nullable.GetUnderlyingType(type) ?? type;
 			type = Nullable.GetUnderlyingType(type) ?? type;
 
 
-			if (type == typeof(double)) return d;
-			if (type == typeof(sbyte)) return (sbyte)d;
-			if (type == typeof(byte)) return (byte)d;
-			if (type == typeof(short)) return (short)d;
-			if (type == typeof(ushort)) return (ushort)d;
-			if (type == typeof(int)) return (int)d;
-			if (type == typeof(uint)) return (uint)d;
-			if (type == typeof(long)) return (long)d;
-			if (type == typeof(ulong)) return (ulong)d;
-			if (type == typeof(float)) return (float)d;
-			if (type == typeof(decimal)) return (decimal)d;
+            		try
+            		{
+                		if (type == typeof(double)) return d;
+                		if (type == typeof(sbyte)) return Convert.ToSByte(d);
+                		if (type == typeof(byte)) return Convert.ToByte(d);
+                		if (type == typeof(short)) return Convert.ToInt16(d);
+                		if (type == typeof(ushort)) return Convert.ToUInt16(d);
+                		if (type == typeof(int)) return Convert.ToInt32(d);
+                		if (type == typeof(uint)) return Convert.ToUInt32(d);
+                		if (type == typeof(long)) return Convert.ToInt64(d);
+                		if (type == typeof(ulong)) return Convert.ToUInt64(d);
+                		if (type == typeof(float)) return Convert.ToSingle(d);
+                		if (type == typeof(decimal)) return Convert.ToDecimal(d);
+            		}
+            		catch (Exception)
+            		{
+            		    
+            		}
+
 			return d;
 			return d;
 		}
 		}
 
 
@@ -62,18 +70,22 @@ namespace MoonSharp.Interpreter.Interop.Converters
 		/// </summary>
 		/// </summary>
 		internal static double TypeToDouble(Type type, object d)
 		internal static double TypeToDouble(Type type, object d)
 		{
 		{
-			if (type == typeof(double)) return (double)d;
-			if (type == typeof(sbyte)) return (double)(sbyte)d;
-			if (type == typeof(byte)) return (double)(byte)d;
-			if (type == typeof(short)) return (double)(short)d;
-			if (type == typeof(ushort)) return (double)(ushort)d;
-			if (type == typeof(int)) return (double)(int)d;
-			if (type == typeof(uint)) return (double)(uint)d;
-			if (type == typeof(long)) return (double)(long)d;
-			if (type == typeof(ulong)) return (double)(ulong)d;
-			if (type == typeof(float)) return (double)(float)d;
-			if (type == typeof(decimal)) return (double)(decimal)d;
-			return (double)d;
+            		if (type != typeof(double) &&
+                		type != typeof(sbyte) &&
+                		type != typeof(byte) &&
+                		type != typeof(short) &&
+                		type != typeof(ushort) &&
+                		type != typeof(int) &&
+                		type != typeof(uint) &&
+                		type != typeof(long) &&
+                		type != typeof(ulong) &&
+                		type != typeof(float) &&
+                		type != typeof(decimal))
+            		{
+                		return (double)d;
+            		}
+
+			return Convert.ToDouble(d);
 		}
 		}
 
 
 
 

+ 7 - 2
src/MoonSharp.Interpreter/Interop/Converters/ScriptToClrConversions.cs

@@ -140,8 +140,13 @@ namespace MoonSharp.Interpreter.Interop.Converters
 						Type underType = Enum.GetUnderlyingType(desiredType);
 						Type underType = Enum.GetUnderlyingType(desiredType);
 						return NumericConversions.DoubleToType(underType, value.Number);
 						return NumericConversions.DoubleToType(underType, value.Number);
 					}
 					}
-					if (NumericConversions.NumericTypes.Contains(desiredType))
-						return NumericConversions.DoubleToType(desiredType, value.Number);
+                    			if (NumericConversions.NumericTypes.Contains(desiredType))
+                    			{
+                        			object d = NumericConversions.DoubleToType(desiredType, value.Number);
+                        			if (d.GetType() == desiredType)
+                            				return d;
+                        			break;
+                    			}
 					if (stringSubType != StringConversions.StringSubtype.None)
 					if (stringSubType != StringConversions.StringSubtype.None)
 						str = value.Number.ToString();
 						str = value.Number.ToString();
 					break;
 					break;