From db3c3ba589cc85c354a76423ee6d138272a7c3f8 Mon Sep 17 00:00:00 2001 From: user1336 Date: Thu, 21 Dec 2017 17:50:32 +0100 Subject: [PATCH] Ensure HeaderDictionary store is initialized consistently (#979) --- .../HeaderDictionary.cs | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs b/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs index 841cfa2642..bc0b7a26ce 100644 --- a/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs +++ b/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs @@ -32,11 +32,19 @@ namespace Microsoft.AspNetCore.Http public HeaderDictionary(int capacity) { - Store = new Dictionary(capacity, StringComparer.OrdinalIgnoreCase); + EnsureStore(capacity); } private Dictionary Store { get; set; } + private void EnsureStore(int capacity) + { + if (Store == null) + { + Store = new Dictionary(capacity, StringComparer.OrdinalIgnoreCase); + } + } + /// /// Get or sets the associated value from the collection as a single string. /// @@ -72,11 +80,7 @@ namespace Microsoft.AspNetCore.Http } else { - if (Store == null) - { - Store = new Dictionary(1, StringComparer.OrdinalIgnoreCase); - } - + EnsureStore(1); Store[key] = value; } } @@ -173,10 +177,7 @@ namespace Microsoft.AspNetCore.Http throw new ArgumentNullException("The key is null"); } ThrowIfReadOnly(); - if (Store == null) - { - Store = new Dictionary(1, StringComparer.OrdinalIgnoreCase); - } + EnsureStore(1); Store.Add(item.Key, item.Value); } @@ -192,11 +193,7 @@ namespace Microsoft.AspNetCore.Http throw new ArgumentNullException(nameof(key)); } ThrowIfReadOnly(); - - if (Store == null) - { - Store = new Dictionary(1); - } + EnsureStore(1); Store.Add(key, value); }