Skip to content

Fix handling of non-ASCII data in indefinite strings - #131235

Merged
bartonjs merged 1 commit into
dotnet:mainfrom
bartonjs:cbor_indef_nonascii
Jul 23, 2026
Merged

bartonjs merged 1 commit into
dotnet:mainfrom
bartonjs:cbor_indef_nonascii

Conversation

@bartonjs

Copy link
Copy Markdown
Member

No description provided.

@bartonjs bartonjs added this to the 11.0.0 milestone Jul 22, 2026
@bartonjs bartonjs self-assigned this Jul 22, 2026
Copilot AI review requested due to automatic review settings July 22, 2026 20:48
@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-formats-cbor, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

Copilot AI 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.

Pull request overview

This PR fixes CborReader.TryReadTextString for indefinite-length, concatenated text strings containing non-ASCII UTF-8 data by advancing the destination span by the actual number of chars written per chunk (rather than the chunk’s byte length). This brings the TryRead* behavior in line with ReadTextString for multi-byte UTF-8 sequences.

Changes:

  • Fix span-advancement logic in TryReadIndefiniteLengthTextStringConcatenated to slice by chunkCharsWritten instead of the chunk byte length.
  • Extend text-string test vectors to include additional non-ASCII cases (Latin-1, German umlaut, Cyrillic) for both definite and indefinite-length concatenated strings.
  • Add/expand tests to validate TryReadTextString parity with ReadTextString for several error/edge cases (non-canonical lengths, invalid data, size-too-large, empty input, invalid UTF-8 chunking).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.TextString.cs Adds broader non-ASCII and indefinite-length concatenation coverage; extends parity checks for TryReadTextString across existing scenarios.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs Correctly advances the destination span by the number of decoded chars per UTF-8 chunk when concatenating indefinite-length text strings.

@bartonjs
bartonjs merged commit b59463f into dotnet:main Jul 23, 2026
90 of 94 checks passed
@bartonjs
bartonjs deleted the cbor_indef_nonascii branch July 23, 2026 02:02
@bartonjs

Copy link
Copy Markdown
Member Author

/backport to release/9.0-staging

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/9.0-staging (link to workflow run)

@bartonjs

Copy link
Copy Markdown
Member Author

/backport to release/8.0-staging

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/8.0-staging (link to workflow run)

@bartonjs

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@bartonjs backporting to release/9.0-staging failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix handling of non-ASCII data in indefinite strings
Using index info to reconstruct a base tree...
M	src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
CONFLICT (content): Merge conflict in src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix handling of non-ASCII data in indefinite strings
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@github-actions

Copy link
Copy Markdown
Contributor

@bartonjs backporting to release/8.0-staging failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix handling of non-ASCII data in indefinite strings
Using index info to reconstruct a base tree...
M	src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
M	src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.TextString.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
CONFLICT (content): Merge conflict in src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
Auto-merging src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.TextString.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix handling of non-ASCII data in indefinite strings
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@github-actions

Copy link
Copy Markdown
Contributor

@bartonjs backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix handling of non-ASCII data in indefinite strings
Using index info to reconstruct a base tree...
M	src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
CONFLICT (content): Merge conflict in src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix handling of non-ASCII data in indefinite strings
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-rc1 Jul 24, 2026
PranavSenthilnathan pushed a commit that referenced this pull request Aug 21, 2026
…ngs (#131278)

Backport of #131235 to release/10.0

## Customer Impact

- [x] Customer reported
- [ ] Found internally

When reading an indefinite-length encoded text string, TryReadTextString
moves the write position forward the number of bytes read, not the
number of chars written, so non-ASCII data gets exceptions that a
precisely-sized buffer is too small (despite having had the size
pre-validated).

## Regression

- [ ] Yes
- [x] No

It seems to have had this behavior since the method was introduced, in
.NET 5.

## Testing

This change includes tests that exercise the pattern better, including
having a non-ASCII character in something other than the final position
in a string.

## Risk

Low. Existing test coverage shows no regressions for pure-ASCII and
Unicode-final-only, new test coverage shows Unicode-before-final works
correctly.

## Package authoring no longer needed in .NET 9

**IMPORTANT**: Starting with .NET 9, you no longer need to edit a NuGet
package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older
versions.
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants