Răsfoiți Sursa

[eval] validate on Bytes.toString

closes #10623
Simon Krajewski 3 ani în urmă
părinte
comite
15a7dfb44d

+ 6 - 1
src/macro/eval/evalStdLib.ml

@@ -440,7 +440,12 @@ module StdBytes = struct
 	)
 
 	let toString = vifun0 (fun vthis ->
-		(create_unknown (Bytes.to_string (this vthis)))
+		let this = this vthis in
+		try
+			UTF8.validate (Bytes.unsafe_to_string this);
+			(create_unknown (Bytes.to_string this))
+		with UTF8.Malformed_code ->
+			exc_string "Invalid string"
 	)
 end
 

+ 7 - 0
tests/misc/projects/Issue10623/Main.hx

@@ -0,0 +1,7 @@
+class Main {
+	static function main() {
+		var data = haxe.io.Bytes.alloc(2);
+		data.set(0, 0xFF);
+		data.toString().substr(0);
+	}
+}

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

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

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

@@ -0,0 +1,2 @@
+Uncaught exception Invalid string
+Main.hx:5: characters 3-18 : Called from here