Skip to content

Fix BitArray stale bits after shrinking and growing - #137149

Open
makazeu wants to merge 3 commits into
dotnet:mainfrom
makazeu:fix-bitarray-l40
Open

makazeu wants to merge 3 commits into
dotnet:mainfrom
makazeu:fix-bitarray-l40

Conversation

@makazeu

@makazeu makazeu commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Fixes finding L40 from #134839: growing BitArray.Length beyond its backing storage can restore bits removed by an earlier shrink, instead of initializing new elements to false.

Small reductions in length retain the backing array. ClearHighExtraBits() clears unused bits in the last valid 32-bit word, but later words may still hold old values. Growth within capacity already clears them; growth beyond capacity only called Array.Resize, copying the stale data. PopCount() also counted this retained data after shrinking because it scanned the entire backing array.

Reproduction and results

using System;
using System.Collections;
using System.Linq;

BitArray bits = new BitArray(800, true);
bits.Length = 10;
Console.WriteLine(bits.PopCount());
bits.Length = 1000;
Console.WriteLine(Enumerable.Range(0, bits.Length).Count(i => bits[i]));
Console.WriteLine(bits.PopCount());

Measured using baseline and patched CoreLib with the same Release host:

Operation Before: set bits / PopCount() After: set bits / PopCount()
Shrink from 800 to 10 10 / 778 10 / 10
Then grow to 1000 778 / 778 10 / 10
Separately shrink to 10, then grow to 500 10 / 10 10 / 10

The 778 set bits are the 10 retained bits plus 768 stale bits at positions 32–799. Growth to 500 stays within capacity and already works correctly.

Changes

  • Clear bytes beyond the old logical length before resizing, preserving existing shrinking and growth-within-capacity behavior.
  • Limit PopCount() to the logical length's 32-bit-aligned storage.
  • Add regression coverage for both growth paths, zero length, 32-bit boundaries, and shrinking that reallocates storage.

Validation

CoreLib builds and the full System.Collections.Tests suite passed in Release and Checked configurations. Regression tests fail against baseline CoreLib and pass with the fix.

Relates to #134839 (L40 only).

Note

This pull request was prepared with GitHub Copilot.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 8, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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-collections
See info in area-owners.md if you want to be subscribed.

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The growth branch now clears retained bytes before Array.Resize copies them, while PopCount() is bounded by the logical length rather than the backing array. The regression cases cover growth both within and beyond capacity, zero length, and the 32-bit boundary, so they exercise the two places where stale bits could surface. I reviewed the diff and tests; I did not run them locally.

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

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants