Skip to content

Fix ParamName in guard of Base64.GetMaxEncodedToUtf8Length - #131085

Closed
lilinus wants to merge 1 commit into
dotnet:mainfrom
lilinus:fix-base64-encodedlength-paramname
Closed

Fix ParamName in guard of Base64.GetMaxEncodedToUtf8Length#131085
lilinus wants to merge 1 commit into
dotnet:mainfrom
lilinus:fix-base64-encodedlength-paramname

Conversation

@lilinus

@lilinus lilinus commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Without fix ParamName of the exception was (uint)bytesLength

Also fix same issue for Base64Url.GetEncodedLength

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 20, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "3499bfadbec56338cc12deccfd58740adc237a4c",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "ea3f7f141e0596cab37785d305910e64d031ab29",
  "last_reviewed_commit": "3499bfadbec56338cc12deccfd58740adc237a4c",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "ea3f7f141e0596cab37785d305910e64d031ab29",
  "last_recorded_worker_run_id": "29755369720",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "3499bfadbec56338cc12deccfd58740adc237a4c",
      "review_id": 4736503634
    }
  ]
}

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Holistic Review

Motivation: Base64.GetEncodedLength (surfaced through GetMaxEncodedToUtf8Length) and Base64Url.GetEncodedLength validate their input with ArgumentOutOfRangeException.ThrowIfGreaterThan<uint>((uint)bytesLength, MaximumEncodeLength). Because that overload derives paramName from CallerArgumentExpression, the thrown exception's ParamName was the literal expression "(uint)bytesLength" rather than the public parameter name bytesLength. That is confusing to callers and inconsistent with the non-NET fallback path, which already threw with nameof(bytesLength).

Approach: The fix passes nameof(bytesLength) explicitly to ThrowIfGreaterThan in both the Base64 and Base64Url encoders, so ParamName is now the correct bytesLength. The tests in Base64EncoderUnitTests and Base64UrlEncoderUnitTests are updated to assert the expected ParamName (Assert.Throws<ArgumentOutOfRangeException>("bytesLength", ...)) for both the overflow and negative-input cases, locking in the corrected behavior.

Summary: This is a minimal, correct, and well-targeted bug fix. The change aligns the #if NET validation path with the existing non-NET fallback (throw new ArgumentOutOfRangeException(nameof(bytesLength))), so both paths now report the same ParamName. The updated assertions provide regression coverage. The ParamName value is a technically observable change, but moving from an internal expression string to the documented parameter name is a strict improvement and not a meaningful compatibility concern. No functional behavior, thrown exception type, or valid-input results change. I found no correctness, performance, or convention issues. LGTM.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 39.6 AIC · ⌖ 9.7 AIC · ⊞ 10K

Assert.Throws<ArgumentOutOfRangeException>(() => Base64.GetMaxEncodedToUtf8Length(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => Base64.GetMaxEncodedToUtf8Length(int.MinValue));
Assert.Throws<ArgumentOutOfRangeException>("bytesLength", () => Base64.GetMaxEncodedToUtf8Length(-1));
Assert.Throws<ArgumentOutOfRangeException>("bytesLength", () => Base64.GetMaxEncodedToUtf8Length(int.MinValue));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like Base64.GetMaxEncodedToUtf8Length's parameter is called just length

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I missed that detail

@lilinus lilinus closed this Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Memory community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants