Browse Source

[php] properly quote bytes with zero byte for Mysql & SQLite (fixes #4489)

Alexander Kuzmenko 8 years ago
parent
commit
c27219448a

+ 1 - 7
std/php/db/PDO.hx

@@ -138,7 +138,7 @@ private class PDOConnection implements Connection {
 
 	public function quote( s : String ) {
 		if( s.indexOf("\000") >= 0 )
-			return "x'"+base16_encode(s)+"'";
+			return "x'"+untyped __call__('bin2hex', s)+"'";
 		return pdo.quote(s);
 	}
 
@@ -170,12 +170,6 @@ private class PDOConnection implements Connection {
 	public function rollback() {
 		pdo.rollBack();
 	}
-
-	function base16_encode(str : String) {
-		str = untyped __call__("unpack", "H"+(2 * str.length), str);
-		str = untyped __call__("chunk_split", untyped str[1]);
-		return str;
-	}
 }
 
 private class TypeStrategy {

+ 10 - 0
std/php7/Global.hx

@@ -841,6 +841,16 @@ extern class Global {
 	**/
 	static function hexdec( hex_string:String ) : Int;
 
+	/**
+		@see http://php.net/manual/en/function.bin2hex.php
+	**/
+	static function bin2hex( str:String ) : String;
+
+	/**
+		@see http://php.net/manual/en/function.hex2bin.php
+	**/
+	static function hex2bin( str:String ) : EitherType<String,Bool>;
+
 	/**
 		@see http://php.net/manual/en/function.serialize.php
 	**/

+ 1 - 0
std/php7/_std/sys/db/Mysql.hx

@@ -77,6 +77,7 @@ private class MysqlConnection implements Connection {
 	}
 
 	public function quote( s : String ) : String {
+		if (s.indexOf("\000") >= 0) return "x'" + Global.bin2hex(s) + "'";
 		return "'" + db.escape_string(s) + "'";
 	}
 

+ 1 - 0
std/php7/_std/sys/db/Sqlite.hx

@@ -53,6 +53,7 @@ private class SQLiteConnection implements Connection {
 	}
 
 	public function quote( s : String ) : String {
+		if (s.indexOf("\000") >= 0) return "x'" + Global.bin2hex(s) + "'";
 		return "'" + SQLite3.escapeString(s) + "'";
 	}
 

+ 1 - 0
tests/unit/src/unit/MySpodClass.hx

@@ -22,6 +22,7 @@ import sys.db.Types;
 
 	public var data:SData<Array<ComplexClass>>;
 	public var anEnum:SEnum<SpodEnum>;
+	public var bytes:SBytes<2>;
 }
 
 @:keep class NullableSpodClass extends Object

+ 3 - 0
tests/unit/src/unit/TestSpod.hx

@@ -57,6 +57,7 @@ class TestSpod extends Test
 		scls.enumFlags = EnumFlags.ofInt(0);
 		scls.enumFlags.set(FirstValue);
 		scls.enumFlags.set(ThirdValue);
+		scls.bytes = Bytes.ofString("\000a");
 
 		scls.data = [new ComplexClass( { name:"test", array:["this", "is", "a", "test"] } )];
 		scls.anEnum = SecondValue;
@@ -410,6 +411,8 @@ class TestSpod extends Test
 		eq(cls1.anEnum, SecondValue,pos());
 		t((cls1.anEnum is SpodEnum),pos());
 
+		eq("\000a", cls1.bytes.toString());
+
 		eq(cls1, MySpodClass.manager.select($anEnum == SecondValue),pos());
 
 		//test create a new class