ArgumentException in Database.LoadFile


I have received some error reports from a project that uses ZoneInfo where Database.LoadFile fails as follows:
Exception: System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary
2.Add(TKey key, TValue value)
at PublicDomain.ZoneInfo.Database.LoadFile(String filename)
at PublicDomain.ZoneInfo.Database.LoadFiles(String directory)
I haven't been able to reproduce the problem in normal use, but I can if I call Database.LoadFiles twice. In Database.LoadFile there are checks to make sure that a duplicate doesn't exist before adding a new key to the _rules and _zone dictionaries but not for _links.
I've modified the links section in LoadFile as follows (duplicate checks marked with !!! RDE):
else if (s.StartsWith("Link"))
                // Clean-up the string
                string[] arr = StripTrailingComments(s).Trim().Replace(' ', '\t').Replace("\t\t", "\t").Split('\t');
                Debug.Assert(arr.Length >= 3 && arr.Length <= 4, "Link entry invalid with " + arr.Length.ToString() + " entries");
                if (arr.Length == 3)
                    // This is well formed
                    // !!! RDE - Added duplicate check
                    if (!_links.ContainsKey(arr[2]))
                        _links.Add(arr[2], arr[1]);
                else if (arr.Length == 4)
                    // This seems to be a standard variation on the 3 item link
                    Debug.Assert(arr[2].Length == 0);
                    // !!! RDE - Added duplicate check
                    if (!_links.ContainsKey(arr[3]))
                        _links.Add(arr[3], arr[1]);
This allows me to call Database.LoadFiles twice without the crash.
Closed Oct 19, 2013 at 11:07 PM by abfo