Skip to content

Fix SizeOptIListSelectIterator.ToList loop bound - #137127

Open
equmagg wants to merge 1 commit into
dotnet:mainfrom
equmagg:codex/fix-sizeopt-select-tolist
Open

equmagg wants to merge 1 commit into
dotnet:mainfrom
equmagg:codex/fix-sizeopt-select-tolist

Conversation

@equmagg

@equmagg equmagg commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

SizeOptIListSelectIterator<TSource, TResult>.ToList() checks the destination list’s Count immediately after constructing it with a capacity. Since Count is zero, the loop never executes and the override returns an empty list for a nonempty source.

This is a dead path only reachable via reflection,
but it is not documented as dead and might be used in the future, so better safe then sorry

repro

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

IEnumerable<int> query = new[] { 1, 2, 3 }.Select(x => x * 10);

MethodInfo method = query.GetType().GetMethod(
    "ToList",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
    ?? throw new InvalidOperationException("ToList override not found");

List<int> result = (List<int>)(method.Invoke(query, null)
    ?? throw new InvalidOperationException("ToList returned null"));

Console.WriteLine(query.GetType().Name);
Console.WriteLine($"[{string.Join(", ", result)}]"); // expect [10, 20, 30]

resolves #137126

@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 dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 7, 2026
@jkotas

jkotas commented Nov 7, 2026

Copy link
Copy Markdown
Member

This is a dead path only reachable via reflection

Is this path dead even in System.Linq.Enumerable.IsSizeOptimized=true configuration?

@equmagg

equmagg commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Yes, it is not called from the public surface, even with IsSizeOptimized=true

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.Linq 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.

Incorrect loop bound in SizeOptIListSelectIterator.ToList

2 participants