From 3dd0ddd578a4ba426a09069ab2eacdcfec5a86ab Mon Sep 17 00:00:00 2001 From: richamsft Date: Mon, 10 Nov 2014 14:56:25 -0800 Subject: [PATCH] Issue - ImmutableArray.CreateBuilder(-1); throws overflow exception but is expected to throw ArgumentOutOfRangeException. Fix - This fix checks the argument "capacity" for valid range such that ArgumentOutOfRangeException is thrown instead. Also, added a testcase for this scenario. --- .../Collections/Immutable/ImmutableArray`1+Builder.cs | 1 + .../tests/ImmutableArrayBuilderTest.cs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1+Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1+Builder.cs index 03b4d1e466b3..bfea4d97a46e 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1+Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1+Builder.cs @@ -39,6 +39,7 @@ public sealed class Builder : IList, IReadOnlyList /// The initial capacity of the internal array. internal Builder(int capacity) { + Requires.Range(capacity >= 0, "capacity"); this.elements = new RefAsValueType[capacity]; this.Count = 0; } diff --git a/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs index 9347a54603b7..89fd67bf1cb5 100644 --- a/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs @@ -20,6 +20,12 @@ public void CreateBuilderDefaultCapacity() Assert.NotSame(builder, ImmutableArray.CreateBuilder()); } + [Fact] + public void CreateBuilderInvalidCapacity() + { + Assert.Throws(() => ImmutableArray.CreateBuilder(-1)); + } + [Fact] public void NormalConstructionValueType() {