Do not use AlphaNum when string contains comma#313
Do not use AlphaNum when string contains comma#313codemasher merged 3 commits intochillerlan:mainfrom
Conversation
Using dash in this context will create a range between `+` and `.` which allows the following characters: `+`, `,`, `-`, `.`. The ASCII range is as follow: + (43), , (44), - (45), . (46). Comma is not supported and will then crash in `write()`.
| ['abc', false], | ||
| ['ÄÖÜ', false], | ||
| [',', true], | ||
| [',', false], |
There was a problem hiding this comment.
I think perhaps the tests should actually try to generate a QR with this data too. As it will crash in AlphaNum::ord(). Would have revealed this bug earlier.
There was a problem hiding this comment.
DataInterfaceTestAbstract
public function testValidateString(string $string, bool $expected):void{
$this::assertSame($expected, $this->dataMode::validateString($string));
try {
$qrCode = new QRCode;
$qrCode->addSegment(static::getDataModeInterface($string));
$qrCode->renderMatrix($qrCode->getQRMatrix());
self::assertTrue($expected);
} catch(\Exception){
self::assertFalse($expected);
}
}
This can also reveal the real bug while in main.
|
Oh that's a good one, thank you! I think I should check other regular expressions here too at this point. I couldn't even tell you why I checked the comma for Adding an extra check running the encoder/decoder is a great idea, so I've tried this and gave me bit of a headache: public function testValidateString(string $string, bool $expected):void{
$this::assertSame($expected, $this->dataMode::validateString($string));
// back out on potentially invalid strings
if($expected === false){
return;
}
$bitBuffer = new BitBuffer;
$dataModeInterface = static::getDataModeInterface($string);
// check if the vailidated string encodes without error
$dataModeInterface->write($bitBuffer, 5);
$bitBuffer->rewind();
// decode and check against the given string
$decoded = $dataModeInterface::decodeSegment($bitBuffer, $bitBuffer->read(4));
$this::assertSame($string, $decoded);
}Which throws an error with the |
|
Ah nevermind, it was just my brain that is goo currently. the 4 bits i read from the BitBuffer are the data mode, not the version number. public function testValidateString(string $string, bool $expected):void{
$this::assertSame($expected, $this->dataMode::validateString($string));
// back out on potentially invalid strings
if($expected === false){
return;
}
$bitBuffer = new BitBuffer;
$dataModeInterface = static::getDataModeInterface($string);
// check if the validated string encodes without error
$dataModeInterface->write($bitBuffer, 5);
$bitBuffer->rewind();
// read 4 bits (data mode indicator)
$bitBuffer->read(4);
// decode and check against the given string
$decoded = $dataModeInterface::decodeSegment($bitBuffer, 5);
$this::assertSame($string, $decoded);
} |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #313 +/- ##
=========================================
Coverage 91.31% 91.31%
Complexity 1218 1218
=========================================
Files 57 57
Lines 3119 3119
=========================================
Hits 2848 2848
Misses 271 271 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thank you for your contribution! (the recently failed CI was presented by Codacy!) |
Proposed changes
Using dash in this context will create a range between
+and.which allows the following characters:+,,,-,..The ASCII range is as follow: + (43), , (44), - (45), . (46).
Comma is not supported and will then crash in
write().Types of changes
What types of changes does your code introduce?
Checklist: