From b91ef224d2da7c335115a1e6d008d0b615fd944f Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Thu, 4 Jun 2026 14:31:12 -0700 Subject: [PATCH] Fix CA2022 warnings: avoid inexact Stream.Read calls Replace Stream.Read with Stream.ReadExactly where the caller expects all requested bytes (GitRepo, EnlistmentHydrationSummary, ReusableMemoryStream). Suppress CA2022 in GitIndexParser.ReadNextPage where partial last-page reads are intentional. Assisted-by: Claude Opus 4.6 Signed-off-by: Tyrie Vella --- GVFS/GVFS.Common/Git/GitRepo.cs | 2 +- .../HealthCalculator/EnlistmentHydrationSummary.cs | 2 +- GVFS/GVFS.UnitTests/Mock/ReusableMemoryStream.cs | 2 +- .../Projection/GitIndexProjection.GitIndexParser.cs | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/GVFS/GVFS.Common/Git/GitRepo.cs b/GVFS/GVFS.Common/Git/GitRepo.cs index d88ebbd89..6f01922e4 100644 --- a/GVFS/GVFS.Common/Git/GitRepo.cs +++ b/GVFS/GVFS.Common/Git/GitRepo.cs @@ -153,7 +153,7 @@ private static bool ReadLooseObjectHeader(Stream input, out long size) size = 0; byte[] buffer = new byte[5]; - input.Read(buffer, 0, buffer.Length); + input.ReadExactly(buffer, 0, buffer.Length); if (!Enumerable.SequenceEqual(buffer, LooseBlobHeader)) { return false; diff --git a/GVFS/GVFS.Common/HealthCalculator/EnlistmentHydrationSummary.cs b/GVFS/GVFS.Common/HealthCalculator/EnlistmentHydrationSummary.cs index a2f83afd4..5dbe7c335 100644 --- a/GVFS/GVFS.Common/HealthCalculator/EnlistmentHydrationSummary.cs +++ b/GVFS/GVFS.Common/HealthCalculator/EnlistmentHydrationSummary.cs @@ -196,7 +196,7 @@ internal static int GetIndexFileCount(GVFSEnlistment enlistment, PhysicalFileSys * the 4 bytes at offsets 8-11 of the index file. */ indexFile.Position = 8; var bytes = new byte[4]; - indexFile.Read( + indexFile.ReadExactly( bytes, // Destination buffer offset: 0, // Offset in destination buffer, not in indexFile count: 4); diff --git a/GVFS/GVFS.UnitTests/Mock/ReusableMemoryStream.cs b/GVFS/GVFS.UnitTests/Mock/ReusableMemoryStream.cs index 5afa60627..dfd70a943 100644 --- a/GVFS/GVFS.UnitTests/Mock/ReusableMemoryStream.cs +++ b/GVFS/GVFS.UnitTests/Mock/ReusableMemoryStream.cs @@ -67,7 +67,7 @@ public string ReadAt(long position, long length) this.Position = position; byte[] bytes = new byte[length]; - this.Read(bytes, 0, (int)length); + this.ReadExactly(bytes, 0, (int)length); this.Position = lastPosition; diff --git a/GVFS/GVFS.Virtualization/Projection/GitIndexProjection.GitIndexParser.cs b/GVFS/GVFS.Virtualization/Projection/GitIndexProjection.GitIndexParser.cs index 382a05945..4f83ff0fd 100644 --- a/GVFS/GVFS.Virtualization/Projection/GitIndexProjection.GitIndexParser.cs +++ b/GVFS/GVFS.Virtualization/Projection/GitIndexProjection.GitIndexParser.cs @@ -391,7 +391,11 @@ private FileSystemTaskResult ParseIndex( private void ReadNextPage() { + // Last page may be smaller than PageSize; partial fill is safe because + // the parser stops after entryCount entries and never reads stale bytes. +#pragma warning disable CA2022 // Avoid inexact read this.indexStream.Read(this.page, 0, PageSize); +#pragma warning restore CA2022 this.nextByteIndex = 0; }