Skip to content

Fix buffer copying for HybridCachePayload.#7343

Merged
cincuranet merged 2 commits intomainfrom
fix-hc-copy
Feb 27, 2026
Merged

Fix buffer copying for HybridCachePayload.#7343
cincuranet merged 2 commits intomainfrom
fix-hc-copy

Conversation

@cincuranet
Copy link
Contributor

@cincuranet cincuranet commented Feb 26, 2026

Fixes #7342.

Microsoft Reviewers: Open in CodeFlow

@cincuranet cincuranet requested a review from a team as a code owner February 26, 2026 12:22
Copilot AI review requested due to automatic review settings February 26, 2026 12:22
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 critical bug in HybridCache's payload parsing logic that caused an ArgumentException when retrieving cached entries with multiple tags. The issue occurred when parsing cached data with 2+ tags while passing empty knownTags, causing all tags to be moved into a pending tags buffer. The code incorrectly used Array.CopyTo() to copy from an oversized ArrayPool-rented buffer to an exact-sized destination array, attempting to copy more elements than the destination could hold.

Changes:

  • Fixed buffer copying in HybridCachePayload.TryParse to use Array.Copy with explicit count instead of CopyTo
  • Added comprehensive test coverage for the scenario with 2 and 3 pending tags

Reviewed changes

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

File Description
src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/HybridCachePayload.cs Changed line 312 from pendingTagBuffer.CopyTo(final, 0) to Array.Copy(pendingTagBuffer, final, pendingTagsCount) to copy only the used portion of the rented buffer
test/Libraries/Microsoft.Extensions.Caching.Hybrid.Tests/PayloadTests.cs Added RoundTrip_WithPendingTags_WhenKnownTagsMismatch theory test with 2 and 3 tag scenarios to verify the fix

Copy link
Member

@rosebyte rosebyte left a comment

Choose a reason for hiding this comment

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

LGTM

@cincuranet cincuranet merged commit 40b4575 into main Feb 27, 2026
6 checks passed
@cincuranet cincuranet deleted the fix-hc-copy branch February 27, 2026 10:07
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.

HybridCache: tags parsing issue breaks remove by tags behavior

4 participants