Skip to content

Fix ANSI parameter MAX promotion for multibyte encodings - #4824

Open
jtheisen wants to merge 1 commit into
dotnet:mainfrom
jtheisen:dev/automation/fix-utf8-parameter-promotion
Open

jtheisen wants to merge 1 commit into
dotnet:mainfrom
jtheisen:dev/automation/fix-utf8-parameter-promotion

Conversation

@jtheisen

Copy link
Copy Markdown

AI Disclaimer

I'm the original reporter of this issue and the test case 3 years ago. AI (Codex/6.1-Sol) now helped me create this PR which apparently fixes this issue, but I can't say that I properly understand it. However, I thought it could be at least a helpful starting point.

Description

ANSI parameter metadata can be invalid when a value fits within 8,000 characters but exceeds 8,000 bytes in the connection encoding. For example, 4,001 é characters occupy 8,002 UTF-8 bytes. The driver currently selects non-MAX metadata using the character count, causing SQL Server to reject the RPC with an invalid data length or metadata length error.

This change counts transmitted ANSI bytes using the connection’s negotiated encoding before selecting MAX metadata. The calculation respects parameter size and offset. Public APIs and the character-based semantics of SqlParameter.Size remain unchanged.

Adds unit regression coverage and SQL Server round-trip tests. Database tests use unique database names and clean up their resources.

Issues

Fixes #2035.

Testing

  • Added 14 unit cases covering encoded byte boundaries, explicit and inferred sizes, truncation, offsets, nulls, and SqlString values.
  • Added six SQL Server regression cases covering text, stored procedure, and prepared commands with synchronous and asynchronous execution.
  • Validated against SQL Server 2022 on GitHub’s Ubuntu runner:
    • Without the fix: all six cases reproduced the expected SQL Server TDS metadata error.
    • With the fix: all six cases passed, with no skips.

Both comparison workflows report success when their expected outcomes are verified; the baseline’s green status means the expected failures were confirmed.

Additional local validation:

  • 1,502 unit tests and 206 parameter functional tests passed. The Windows-only integrated authentication test was excluded on Linux.
  • All 19 wire-protocol scenarios passed, including replay of the original EF-generated command and parameters.
  • Driver builds passed for net10.0 and net462.

The fork-specific comparison workflows are separate from this PR. Windows runtime validation remains for upstream CI.

Use the connection encoding to count transmitted ANSI bytes before selecting MAX metadata. This prevents UTF-8 values below 8000 characters but above 8000 bytes from producing invalid VARCHAR declarations and RPC framing.

Cover encoded byte boundaries, inferred sizes, truncation, offsets and SqlString values with unit regressions. Add SQL Server round-trip tests for text, stored procedure and prepared commands with sync and async execution.

Validation: 1502 unit tests and 206 parameter functional tests passed; the Windows-only integrated authentication test was excluded on Linux. All 19 wire scenarios passed, including replay of the original EF command. Driver builds passed for net10.0 and net462. Live SQL Server tests compile but remain unverified locally.

Fixes dotnet#2035
@jtheisen
jtheisen requested a review from a team as a code owner October 10, 2026 00:02
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@jtheisen

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Bug in SQL Server provider when using UTF8 collations

1 participant