Skip to content

Commit

Permalink
Fix bug in decodeJson() function (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
crtlib authored Dec 1, 2021
1 parent 4293060 commit d0b9a61
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DataTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public static function decodeStringOff(int $type, string $str, int &$off) /* : m

private static function decodeJson(string $data): string
{
if (!\strncmp(self::ENCODED_JSON_PREFIX, $data, \strlen(self::ENCODED_JSON_PREFIX))) {
if (\strncmp(self::ENCODED_JSON_PREFIX, $data, \strlen(self::ENCODED_JSON_PREFIX)) !== 0) {
return $data; // Data was not base-64 encoded.
}

Expand Down
13 changes: 13 additions & 0 deletions test/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,17 @@ public function testInsertSelect()
yield $transaction->rollback();
}
}

public function testJsonDecoding()
{
/** @var Link $db */
$db = yield $this->getLink("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=test");

/** @var ResultSet $resultset */
$resultset = yield $db->execute("SELECT a FROM test.json");
$this->assertInstanceOf(ResultSet::class, $resultset);

yield $resultset->advance();
$this->assertSame(["a" => '{"key": "value"}'], $resultset->getCurrent());
}
}
2 changes: 2 additions & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
$db->query("CREATE DATABASE test");
$db->query("CREATE TABLE test.main (a INT(11), b INT(11))");
$db->query("INSERT INTO test.main VALUES (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)");
$db->query("CREATE TABLE test.json (a JSON)");
$db->query("INSERT INTO test.json VALUES ('{\"key\": \"value\"}')");
$db->close();

print "\r";
Expand Down

0 comments on commit d0b9a61

Please sign in to comment.