Repository navigation
Optimize rawData QrCodeData constructor and improve input validation - #709
Conversation
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Validate and decode raw QR data QRCoder/QRCodeData.cs |
The constructor validates the header, signature, payload length, and allowed Micro QR and standard QR side lengths. It calculates the version from the side length and reads the required matrix bits directly from the payload. The decompression comment also changed; behavior is unchanged. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested reviewers: shane32, codebude
Merge Risk: 
Minimal · up to 0ce1e
-
No confirmed issue currently prevents merging after normal checks.
Pre-merge checks: 
5
-
Full details: Title check
-
Explanation
The title names the main change: optimizing the rawData QRCodeData constructor and improving its input validation. The raw summary confirms both parts. The constructor now checks header bytes, signature bytes, side lengths, and payload size. Matrix decoding now reads bytes directly instead of through a Queue<bool>. The title's 'Optimize' wording covers the decoding change, and the validation wording covers the new checks. The title is specific and matches the PR objectives (PR
#709).
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
There was a problem hiding this comment.
🟡 Changes recommended
The new input-validation guards lack tests for invalid headers, invalid side lengths, truncated payloads, and valid boundary cases.
1 open issue
What changed in this PR
This PR strengthens raw QR data validation and reduces decoding allocations without changing the serialized format. It complements #708’s separate write-path optimization.
Changes:
- Validate the fourth signature byte and supported QR/Micro QR dimensions.
- Reject payloads too short for the declared matrix size.
- Decode bits directly into the module matrix, removing the intermediate
Queue<bool>.
| File | Description |
|---|---|
QRCoder/QRCodeData.cs |
Adds constructor validation and direct bit decoding for raw QR data. |
Review effort: Balanced
| int byteLength = (sideLen * sideLen + 7) >>> 3; | ||
| if (rawData.Length < 5 + byteLength) | ||
| { | ||
| var b = rawData[j]; | ||
| for (int i = 7; i >= 0; i--) | ||
| { | ||
| modules.Enqueue((b & (1 << i)) != 0); | ||
| } | ||
| throw new InvalidDataException("Invalid raw data file. Not enough data."); |

Summary
Input validation in the
QRCodeData(byte[] rawData, Compression compressMode)constructor was improved to reject invalid data.Additional checks:
Reading the data has been optimized by removing the intermediate
Queue<bool>.Test plan
Existing tests already cover the constructor.
This PR only changes reading raw data. #708 optimizes writing the data. By keeping these in separate PRs, we ensure the data format is not modified.
Summary by CodeRabbit