Skip to content

Optimize rawData QrCodeData constructor and improve input validation - #709

Merged
Shane32 merged 1 commit into
Shane32:masterfrom
KrisVandermotten:ctor-raw-data
Oct 31, 2026
Merged

Shane32 merged 1 commit into
Shane32:masterfrom
KrisVandermotten:ctor-raw-data

Conversation

@KrisVandermotten

@KrisVandermotten KrisVandermotten commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Input validation in the QRCodeData(byte[] rawData, Compression compressMode) constructor was improved to reject invalid data.

Additional checks:

  • The fourth header byte must be 0.
  • The fifth header byte must be a valid side length for an existing QR or Micro QR version.
  • The data must contain at least side length * side length bits.

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

  • Bug Fixes
    • Improved handling of QR code data with incomplete or invalid headers, signatures, payloads, or matrix dimensions.
    • Corrected matrix decoding to use only the data required for the declared QR code size, improving reliability when reading encoded data.

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a3a54e56-0f7b-420d-b803-5f1116bdf71d
Commits

Reviewing files that changed from the base of the PR and between d050cc7 and 0ce1ee0.

Files selected for processing: 1
  • QRCoder/QRCodeData.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

The raw-data constructor now validates the header signature, payload length, and supported matrix dimensions. It then reads only the bits needed to build the matrix.

Changes: Raw QR data parsing

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: Low

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: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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, signatu…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread QRCoder/QRCodeData.cs
Comment on lines +105 to +108
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.");
@Shane32
Shane32 merged commit a8fbae6 into Shane32:master Oct 31, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants