diff --git a/src/Data/Hanzi.php b/src/Data/Hanzi.php index fdc30228b..45da1e5d0 100644 --- a/src/Data/Hanzi.php +++ b/src/Data/Hanzi.php @@ -12,6 +12,7 @@ namespace chillerlan\QRCode\Data; use chillerlan\QRCode\Common\{BitBuffer, Mode}; +use Throwable; use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding, mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen; @@ -81,8 +82,15 @@ final class Hanzi extends QRDataModeAbstract{ * checks if a string qualifies as Hanzi/GB2312 */ public static function validateString(string $string):bool{ - $string = self::convertEncoding($string); - $len = strlen($string); + + try{ + $string = self::convertEncoding($string); + } + catch(Throwable $e){ + return false; + } + + $len = strlen($string); if($len < 2 || $len % 2 !== 0){ return false; diff --git a/src/Data/Kanji.php b/src/Data/Kanji.php index 99ee791d4..d79287fa8 100644 --- a/src/Data/Kanji.php +++ b/src/Data/Kanji.php @@ -12,6 +12,7 @@ namespace chillerlan\QRCode\Data; use chillerlan\QRCode\Common\{BitBuffer, Mode}; +use Throwable; use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding, mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen; @@ -79,8 +80,15 @@ final class Kanji extends QRDataModeAbstract{ * checks if a string qualifies as SJIS Kanji */ public static function validateString(string $string):bool{ - $string = self::convertEncoding($string); - $len = strlen($string); + + try{ + $string = self::convertEncoding($string); + } + catch(Throwable $e){ + return false; + } + + $len = strlen($string); if($len < 2 || $len % 2 !== 0){ return false; diff --git a/tests/Data/ByteTest.php b/tests/Data/ByteTest.php index 66f5ed008..214914717 100644 --- a/tests/Data/ByteTest.php +++ b/tests/Data/ByteTest.php @@ -40,4 +40,12 @@ final class ByteTest extends DataInterfaceTestAbstract{ $this::markTestSkipped('N/A (binary mode)'); } + /** + * @inheritDoc + */ + public function testBinaryStringInvalid():void{ + /** @noinspection PhpUnitTestFailedLineInspection */ + $this::markTestSkipped('N/A (binary mode)'); + } + } diff --git a/tests/Data/DataInterfaceTestAbstract.php b/tests/Data/DataInterfaceTestAbstract.php index b130b8664..1020f033f 100644 --- a/tests/Data/DataInterfaceTestAbstract.php +++ b/tests/Data/DataInterfaceTestAbstract.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; use chillerlan\QRCode\Data\{Hanzi, QRCodeDataException, QRData, QRDataModeInterface, QRMatrix}; use ReflectionClass; +use function hex2bin; use function str_repeat; /** @@ -98,6 +99,16 @@ abstract class DataInterfaceTestAbstract extends TestCase{ $this::assertSame($expected, $this->FQN::validateString($string)); } + /** + * Tests if a binary string is properly validated as false + * + * @see https://github.com/chillerlan/php-qrcode/issues/182 + */ + public function testBinaryStringInvalid():void{ + /** @noinspection PhpUndefinedMethodInspection */ + $this::assertFalse($this->FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1'))); + } + /** * returns versions within the version breakpoints 1-9, 10-26 and 27-40 */