nuintun / qrcode

A pure JavaScript QRCode encode and decode library.
https://nuintun.github.io/qrcode
MIT License
194 stars 26 forks source link

Make sure to always return null on Decoder's `readVersion()` #320

Closed netux closed 11 months ago

netux commented 1 year ago

In some cases, the decoder would find a section that looks like a QR Code, but it's smaller than 21x21, causing the version number calculation in readVersion() to go below zero and making the function return undefined. Because down the line there is a check for null, but not undefined, this causes the decoder to crash on buildFunctionPatternMask(). I found this to be more common on particularily grainy images like the ones from a notebook webcam.

The fix is a simple nullish coalescence to always return null and have the decoder correctly report that there is no valid QR Code in the region scanned. I also checked no other paths of this function could return anything other than a valid Version object or null.

Stacktrace:

qrcode.js:4459 Uncaught TypeError: Cannot read properties of undefined (reading 'versionNumber')
    at buildFunctionPatternMask (qrcode.js:4459:38)
    at readCodewords (qrcode.js:4485:31)
    at decodeMatrix (qrcode.js:4668:21)
    at decode (qrcode.js:4696:18)
    at scan (qrcode.js:4887:21)
    at Decoder.decode (qrcode.js:4945:20)

Note the library was transpiled in my case so line numbers are different.

nuintun commented 11 months ago

PR outdated, I have manually merged into a new branch.