Skip to content

fix: unify the exceptions for content that does not fit and bad sizes - #511

Merged
guitarrapc merged 3 commits into
mainfrom
fix/unify-argument-exceptions
Nov 26, 2026
Merged

guitarrapc merged 3 commits into
mainfrom
fix/unify-argument-exceptions

Conversation

@guitarrapc

Copy link
Copy Markdown
Owner

Summary

The exceptions for "content does not fit" and for invalid sizes consistent across the three symbologies. It also caps the GS1 Digital Link URI buffer, which could rent gigabytes and overflow int on very long input.

Intent

  • Exception types: an exception type is public API that no compiler checks, and it had drifted.
    • Standard QR threw InvalidOperationException for content past version 40, while Micro QR and rMQR threw ArgumentException.
    • 1.x kept this difference on purpose to protect a released contract. 2.0.0 is the breaking release, so this is the last chance to align them.
  • ParamName: some errors still named requestedVersion, a parameter removed in 1.x.
  • Decoder size errors: the matrix decoders reported a non-positive size as "buffer too small", even when the buffer was large enough.
  • GS1 URI buffer: it was sized at three times the text length. Text length doesn't bound the URI, though: an AI repeated with the same value is written once.

Before / After

Case Before After
QR content past version 40 InvalidOperationException ArgumentException, ParamName null
Content that doesn't fit a version, range or rMQR height set in options QR options, Micro QR / rMQR requestedVersion or null ArgumentException, ParamName options in all three
Undefined RmQRVersion ParamName requestedVersion ParamName version, like height and fitStrategy
Matrix TryDecode with size ≤ 0 ArgumentException "Module buffer too small: required 9" ArgumentOutOfRangeException naming size (width / height for rMQR), as the image overloads already did
GetMaxDecodedLength with an undefined version Three different wordings One wording: "Version must be … but was …"
GS1 Digital Link, 400M characters of valid data Rents 2,400 MB before any check Rents nothing
GS1 Digital Link, over 715,827,882 characters Buffer size overflows int and picks the 512-character stack buffer Capped at 7,090 (QR) / 362 (rMQR) characters

The QR overflow message now says how much version 40 holds, in the same style as Micro QR and rMQR:

Content is too long for QR code: 7090 data units in Numeric mode (ECI: Default), but ECC level L fits at most 7089 (version 40). Shorten the content, lower the ECC level, or split it across symbols with CreateStructuredAppend.

Changes

  • QRCodeGenerator: the default, FNC1 and Optimal paths now share one ContentTooLong helper. It finds the version 40 maximum by bisection, on the failure path only, so the success path does no extra work. The maximums match the standard capacity table, including the UTF-8 BOM case.
  • MicroQRCodeGenerator and RmQRVersionSelector: errors for a version, range or height set in options name options. An undefined rMQR version names version.
  • Decoders: the matrix overloads check size ≤ 0 separately from the buffer length, so a short buffer still names modules. The XML docs list the new exception.
  • GS1:
    • UriBufferLength computes in long and caps at the symbol's capacity + 1. The buffer is sliced to that exact length.
    • A URI longer than any symbol holds is reported as content that doesn't fit: ArgumentException with no ParamName, after the options are checked. The size forms return false.
    • Data errors are still reported first, because the builder checks the data before it writes.
  • XML docs: the generator and GS1 wrapper docs no longer promise InvalidOperationException.
  • Tests:
    • New ErrorContractTest measures each case through the text, byte and destination overloads of all three symbologies.
    • 32 existing tests that pinned the old contract are updated.
    • New GS1 tests cover the buffer-length boundaries and URIs longer than any symbol.
  • Docs: migration.md notes the behavior changes. The specs (qrcode-symbologies.md, standardqr-encoder.md, qrcode-gs1-data.md) record the decisions and measurements.

Breaking changes

  • Code that catches InvalidOperationException for Standard QR content past version 40 must catch ArgumentException, or call TryGetRequiredBufferSize first.
  • Code that reads ParamName requestedVersion from Micro QR or rMQR errors now sees options or version.
  • ArgumentOutOfRangeException derives from ArgumentException, so existing catch (ArgumentException) blocks around the matrix decoders still work.

🤖 Generated with Claude Code

guitarrapc and others added 3 commits November 26, 2026 20:24
Standard QR threw InvalidOperationException for content past version 40,
while Micro QR and rMQR threw ArgumentException. All three now throw
ArgumentException. Its ParamName is "options" when a version, a range or
an rMQR height narrowed the versions, and null when nothing did. Micro QR
and rMQR no longer report the removed "requestedVersion" parameter. The
Standard QR message now says how much version 40 holds.

The decoders' matrix overloads reported a non-positive size as a buffer
too small. They now throw ArgumentOutOfRangeException naming the size, as
the image overloads do, and GetMaxDecodedLength words an undefined
version the same way in all three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The URI buffer was sized at three characters per character of text, so
400 million characters of valid data rented 2,400 MB before any check.
Past 715,827,882 characters the size overflowed int, went negative and
picked the 512-character stack buffer. The text does not bound the URI,
since an AI repeated with one value is written once.

The buffer now stops one character past the symbol's capacity: 7,090 for
Standard QR and 362 for rMQR, which always fits the stack. A URI that
fills it fits no symbol, so the Create forms throw ArgumentException with
no ParamName after the options are checked, and the size forms return
false. The builder checks the data before it writes, so a data error is
still reported first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An undefined RmQRVersion reported ParamName "requestedVersion", the name
of a parameter removed in 1.x. It now reports "version", the option
member, as an undefined height or fit strategy reports its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@guitarrapc
guitarrapc merged commit 90c32ab into main Nov 26, 2026
28 checks passed
@guitarrapc
guitarrapc deleted the fix/unify-argument-exceptions branch November 26, 2026 18:14
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.

1 participant