Browse Source

[php] throw if invalid json string supplied to Json.parse() (fixes #4592)

Alexander Kuzmenko 8 years ago
parent
commit
fd22bf5d16
3 changed files with 21 additions and 0 deletions
  1. 3 0
      std/php/_std/haxe/Json.hx
  2. 3 0
      std/php7/_std/haxe/Json.hx
  3. 15 0
      tests/unit/src/unit/issues/Issue4592.hx

+ 3 - 0
std/php/_std/haxe/Json.hx

@@ -42,6 +42,9 @@ class Json {
 
 	static function phpJsonDecode(json:String):Dynamic {
 		var val = untyped __call__("json_decode", json);
+		if (val == null && untyped __php__("json_last_error() != JSON_ERROR_NONE")) {
+			throw untyped __call__("json_last_error_msg");
+		}
 		return convertAfterDecode(val);
 	}
 

+ 3 - 0
std/php7/_std/haxe/Json.hx

@@ -45,6 +45,9 @@ class Json {
 
 	static function phpJsonDecode(json:String):Dynamic {
 		var value = Global.json_decode(json);
+		if (value == null && Global.json_last_error() != Const.JSON_ERROR_NONE) {
+			throw Global.json_last_error_msg();
+		}
 		return convertAfterDecode(value);
 	}
 

+ 15 - 0
tests/unit/src/unit/issues/Issue4592.hx

@@ -0,0 +1,15 @@
+package unit.issues;
+
+import haxe.Json;
+
+class Issue4592 extends unit.Test {
+	function test() {
+		var json =  'hello":"world"';
+		try {
+			Json.parse(json);
+			t(false);
+		} catch(e:Dynamic) {
+			t(true);
+		}
+	}
+}