Bläddra i källkod

[eval] catch Vector constructor with size < 0

closes #10804
Simon Krajewski 2 år sedan
förälder
incheckning
6066953146

+ 2 - 0
src/macro/eval/evalContext.ml

@@ -403,6 +403,8 @@ let exc v = throw v null_pos
 
 let exc_string str = exc (vstring (EvalString.create_ascii str))
 
+let exc_string_p str p = throw (vstring (EvalString.create_ascii str)) p
+
 let error_message = exc_string
 
 let flush_core_context f =

+ 4 - 2
src/macro/eval/evalEmitter.ml

@@ -92,11 +92,13 @@ let emit_const v _ = v
 let emit_new_array env =
 	encode_array_instance (EvalArray.create [||])
 
-let emit_new_vector_int i env =
+let emit_new_vector_int i p env =
+	if i < 0 then exc_string_p "Vector size must be >= 0" p;
 	encode_vector_instance (Array.make i vnull)
 
 let emit_new_vector exec p env =
-	encode_vector_instance (Array.make (decode_int_p (exec env) p) vnull)
+	let i = decode_int_p (exec env) p in
+	emit_new_vector_int i p env
 
 let emit_special_instance f execs env =
 	let vl = List.map (apply env) execs in

+ 1 - 1
src/macro/eval/evalJit.ml

@@ -507,7 +507,7 @@ and jit_expr jit return e =
 	| TNew({cl_path=["eval"],"Vector"},_,[e1]) ->
 		begin match e1.eexpr with
 			| TConst (TInt i32) ->
-				emit_new_vector_int (Int32.to_int i32)
+				emit_new_vector_int (Int32.to_int i32) e1.epos
 			| _ ->
 				let exec1 = jit_expr jit false e1 in
 				emit_new_vector exec1 e1.epos

+ 3 - 0
tests/misc/projects/Issue10804/Main.hx

@@ -0,0 +1,3 @@
+function main() {
+	new haxe.ds.Vector(-1);
+}

+ 7 - 0
tests/misc/projects/Issue10804/Main2.hx

@@ -0,0 +1,7 @@
+function getMinusOne() {
+	return -1;
+}
+
+function main() {
+	new haxe.ds.Vector(getMinusOne());
+}

+ 2 - 0
tests/misc/projects/Issue10804/compile-fail.hxml

@@ -0,0 +1,2 @@
+--main Main
+--interp

+ 1 - 0
tests/misc/projects/Issue10804/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:2: characters 21-23 : Uncaught exception Vector size must be >= 0

+ 2 - 0
tests/misc/projects/Issue10804/compile2-fail.hxml

@@ -0,0 +1,2 @@
+--main Main2
+--interp

+ 1 - 0
tests/misc/projects/Issue10804/compile2-fail.hxml.stderr

@@ -0,0 +1 @@
+Main2.hx:6: characters 21-34 : Uncaught exception Vector size must be >= 0