Ver código fonte

:octocat: do not throw in QRDataModeInterface::validateString() (#182)

smiley 2 anos atrás
pai
commit
cc58c403f8

+ 10 - 2
src/Data/Hanzi.php

@@ -12,6 +12,7 @@ namespace chillerlan\QRCode\Data;
 
 
 use chillerlan\QRCode\Common\{BitBuffer, Mode};
 use chillerlan\QRCode\Common\{BitBuffer, Mode};
 
 
+use Throwable;
 use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
 use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
 	mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
 	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
 	 * checks if a string qualifies as Hanzi/GB2312
 	 */
 	 */
 	public static function validateString(string $string):bool{
 	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){
 		if($len < 2 || $len % 2 !== 0){
 			return false;
 			return false;

+ 10 - 2
src/Data/Kanji.php

@@ -12,6 +12,7 @@ namespace chillerlan\QRCode\Data;
 
 
 use chillerlan\QRCode\Common\{BitBuffer, Mode};
 use chillerlan\QRCode\Common\{BitBuffer, Mode};
 
 
+use Throwable;
 use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
 use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
 	mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
 	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
 	 * checks if a string qualifies as SJIS Kanji
 	 */
 	 */
 	public static function validateString(string $string):bool{
 	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){
 		if($len < 2 || $len % 2 !== 0){
 			return false;
 			return false;

+ 8 - 0
tests/Data/ByteTest.php

@@ -40,4 +40,12 @@ final class ByteTest extends DataInterfaceTestAbstract{
 		$this::markTestSkipped('N/A (binary mode)');
 		$this::markTestSkipped('N/A (binary mode)');
 	}
 	}
 
 
+	/**
+	 * @inheritDoc
+	 */
+	public function testBinaryStringInvalid():void{
+		/** @noinspection PhpUnitTestFailedLineInspection */
+		$this::markTestSkipped('N/A (binary mode)');
+	}
+
 }
 }

+ 11 - 0
tests/Data/DataInterfaceTestAbstract.php

@@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase;
 use chillerlan\QRCode\Data\{Hanzi, QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
 use chillerlan\QRCode\Data\{Hanzi, QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
 use ReflectionClass;
 use ReflectionClass;
 
 
+use function hex2bin;
 use function str_repeat;
 use function str_repeat;
 
 
 /**
 /**
@@ -98,6 +99,16 @@ abstract class DataInterfaceTestAbstract extends TestCase{
 		$this::assertSame($expected, $this->FQN::validateString($string));
 		$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
 	 * returns versions within the version breakpoints 1-9, 10-26 and 27-40
 	 */
 	 */