chillerlan / php-qrcode

A PHP QR Code generator and reader with a user-friendly API.
https://smiley.codes/qrcode/
Apache License 2.0
2.01k stars 302 forks source link

Bug in RegExp #282

Closed robodal closed 1 month ago

robodal commented 1 month ago

https://github.com/chillerlan/php-qrcode/blob/9964cf8ff1ad90d17c360bd320cf18e16cd59829/src/Data/AlphaNum.php#L30

https://onlinephp.io/c/215012

(bool)preg_match('#^[+-.]+$#', ',') = true BUT (bool)preg_match('#^[+.-]+$#', ',') = false

robodal commented 1 month ago

https://github.com/chillerlan/php-qrcode/blob/9964cf8ff1ad90d17c360bd320cf18e16cd59829/src/Data/AlphaNum.php#L39

codemasher commented 1 month ago

Oh, good catch. Some of those characters should be escaped, I guess I'll just regex that bit the regular way then.

codemasher commented 1 month ago

Ok, so I added a test case similar to the example you posted and it runs without issues with the charmap string linked above. However, the issue occurs if the minus within the charmap is moved to the end after the colon, in which case it is interpreted as meta-character. Either way i think I'm gonna change the regex to make it foolproof.

codemasher commented 1 month ago

This is it. The minus it kind of problematic within the regex, it has to be in a specific position to not cause problems apparently, but also it can't be escaped as this would break the regex too.

preg_match('/^[A-Z\d %$*+-.:\/]+$/', $string);