ImmutableSortedSet<T>.Builder: enumerating through IEnumerable<T> doesn't throw on modification and can return wrong items
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 78/100
Línea de trabajo
Empieza en src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs, en la implementación de IEnumerable<T>.GetEnumerator() enlazada en la incidencia, y compárala después con los enumeradores comprobados de los demás generadores inmutables. Ejecuta las pruebas de System.Collections.Immutable.Tests; se considera terminado cuando la enumeración mediante IEnumerable<T> lanza InvalidOperationException después de una modificación del generador, igual que la enumeración directa.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description
ImmutableSortedSet.Builder's IEnumerable.GetEnumerator() returns this.Root.GetEnumerator() and doesn't pass the builder, so the enumerator never checks the version:
foreach over the builder itself throws if you modify it, but going through IEnumerable (LINQ, an IEnumerable parameter, etc.) doesn't. And since the builder mutates its own nodes in place, the enumeration can return items that were removed, return an item twice, or skip items that are still there.
Reproduction Steps
using System.Collections.Immutable;
var builder = ImmutableSortedSet.CreateBuilder<int>();
builder.Add(0);
builder.Add(2);
builder.Add(4);
foreach (int x in (IEnumerable<int>)builder)
{
Console.Write(x + " ");
if (x == 0)
builder.Remove(2);
}
Expected behavior
InvalidOperationException after the Remove, like with foreach (int x in builder).
Actual behavior
No exception, and it prints "0 2 0 4". The builder contains "0 4" afterwards.
With 0 2 4 6 8 and the same Remove(2) it prints "0 2 6 8", so 4 is skipped even though it's still in the set.
I ran this with different mutations (Add, Remove, Clear, set operations, KeyComparer) at every position for sizes 1-48. It never throws through IEnumerable. For a builder filled with Add, about 14% of runs (2,450 of 17,640) gave duplicates, skipped items or wrong order. For a builder from ToBuilder() that wasn't modified before enumerating, you just get the old contents.
Regression?
I don't think so. Same result on .NET 10.0.11 and current main, and this line hasn't changed since the code was added to the repo in 2014 (ebabdf94973). I didn't try older releases or .NET Framework.
Known Workarounds
foreach over the builder directly (not through IEnumerable), or call ToImmutable() and enumerate that.
Configuration
.NET 10.0.11, and a local build of main (614e7dafc09)
macOS, ARM64
Other information
The other builders (ImmutableList, ImmutableHashSet, ImmutableDictionary, ImmutableSortedDictionary) go through their checked enumerator here. Doing the same fixes it:
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return this.GetEnumerator();
}
I tried that locally. All the cases above throw with it, and System.Collections.Immutable.Tests passes. I can send a PR if that's the fix you want.
- Lenguaje dominante
- C#
- Estrellas
- 18.4k
- Forks
- 5.8k
- Merge medio
- 2 d 12 h
- PR fusionados (30 d)
- 581
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de dotnet/runtime
-
Incorrect loop bound in SizeOptIListSelectIterator.ToListPosiblemente ocupada @equmagg la tomó hace 2 días. Abiertoarea-System.Collections untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
dotnet/runtime#137126 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
DbTransaction.SupportsSavepoints docs say Release/ReleaseAsync are expected to throw, but they are no-ops by designPosiblemente ocupada @alaricd la tomó hace 3 días. Abiertoarea-System.Transactions untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
dotnet/runtime#137040 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the filePosiblemente ocupada @makazeu la tomó hace 3 días. Abiertoarea-Extensions-FileSystem
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
dotnet/runtime#136989 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Configuration binder source generator emits unqualified List<T> when binding arrays without implicit usingsPosiblemente ocupada @makazeu la tomó hace 3 días. Abiertoarea-Extensions-Configuration bug source-generator
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
dotnet/runtime#136972 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[wasi] .NET 10: WasmEnableSIMD with AOT fails because WasiApp.targets has -msimd128 commented outAbiertoarea-Build-mono untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
Todos los issues de dotnet/runtime
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
dotnet/wpf#12108 · 3 reacciones ·
Los mantenedores suelen responder en 1 día
-
:watch: Not Triaged dotnet/svc
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
area:build type:chore
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
csharp good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
microsoft/fluentui-blazor#5610 · 1 comentario ·
Los mantenedores suelen responder en 1 día