Skip to content

Conversation

@vcsjones
Copy link
Member

The first issue is that Reset did not correctly set _definiteLength back after a reset. If allowMultipleRootLevelValues from the constructor is false, the default, it should be initialized to 1. However Reset set it as null, which behaves as if allowMultipleRootLevelValues is true. This sets _definiteLength back to the same way the constructor does.

The second is that _currentIndefiniteLengthStringRanges is not cleared during Reset. This means if a Reset occurred while in the middle of writing an indefinite length value and convertIndefiniteLengthEncodings is true, the next time an encode tried to re-encode segments as definite encoding where the list indicated would be pointing to incorrect locations.

Fixes #121183
Fixes #121182

The first issue is that Reset did not correctly set _definiteLength back after a reset. If allowMultipleRootLevelValues from the constructor is false, the default, it should be initialized to 1. However Reset set it as null, which behaves as if allowMultipleRootLevelValues is true. This sets _definiteLength back to the same way the constructor does.

The second is that _currentIndefiniteLengthStringRanges is not cleared during Reset. This means if a Reset occurred while in the middle of writing an indefinite length value and convertIndefiniteLengthEncodings is true, the next time an encode tried to re-encode segments as definite encoding where the list indicated would be pointing to incorrect locations.
Copilot AI review requested due to automatic review settings October 30, 2025 01:59
@dotnet-policy-service
Copy link
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.

Copy link
Contributor

Copilot AI left a comment

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 a bug in the CborWriter.Reset() method where two important writer state fields were not being properly reset. The fix ensures that after calling Reset(), the writer behaves correctly for both multiple root level value restrictions and indefinite-length string encoding conversions.

Key changes:

  • Fixed _definiteLength to properly restore the initial state based on AllowMultipleRootLevelValues setting
  • Added clearing of _currentIndefiniteLengthStringRanges to prevent stale data from affecting subsequent writes
  • Added two test cases to verify the reset behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.cs Fixed Reset() to properly restore _definiteLength based on writer configuration and clear indefinite-length string tracking data
src/libraries/System.Formats.Cbor/tests/Writer/CborWriterTests.cs Added test coverage for Reset() preserving multiple root level restrictions and clearing indefinite-length string ranges

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Thanks

@vcsjones vcsjones enabled auto-merge (squash) October 30, 2025 13:27
@vcsjones
Copy link
Member Author

/ba-g Android and Windows NAOT pipelines are timing out, but other pipelines validate the behavior.

@vcsjones vcsjones merged commit 814894c into dotnet:main Oct 31, 2025
88 of 94 checks passed
@vcsjones vcsjones deleted the fix-cbor-reset branch October 31, 2025 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CborWriter.Reset does not clear _currentIndefiniteLengthStringRanges CborWriter.Reset incorrectly resets _definiteLength

3 participants