runtime: fix variable null check (#89920)
authorПавел Харьков <48874697+PabloKharo@users.noreply.github.com>
Sat, 12 Aug 2023 15:58:33 +0000 (18:58 +0300)
committerGitHub <noreply@github.com>
Sat, 12 Aug 2023 15:58:33 +0000 (08:58 -0700)
Fixed error when _hashtableContentsToEnumerate is null.
It appears always when using 'Length' property when variable is null.
Also reduced nesting.

src/coreclr/tools/Common/TypeSystem/Common/Utilities/LockFreeReaderHashtable.cs

index 44be986803b66f52795e7bef615749f38f02cc42..613ec30fa01d2eeffe37acb25d13d89bdbe4f36c 100644 (file)
@@ -689,27 +689,24 @@ namespace Internal.TypeSystem
 
             public bool MoveNext()
             {
-                if (_sentinel != null)
+                if ((_sentinel != null) && (_hashtableContentsToEnumerate != null))
                 {
-                    if ((_hashtableContentsToEnumerate != null) && (_index < _hashtableContentsToEnumerate.Length))
+                    for (; _index < _hashtableContentsToEnumerate.Length; _index++)
                     {
-                        for (; _index < _hashtableContentsToEnumerate.Length; _index++)
+                        if ((_hashtableContentsToEnumerate[_index] != null) && (_hashtableContentsToEnumerate[_index] != _sentinel))
                         {
-                            if ((_hashtableContentsToEnumerate[_index] != null) && (_hashtableContentsToEnumerate[_index] != _sentinel))
-                            {
-                                _current = _hashtableContentsToEnumerate[_index];
-                                _index++;
-                                return true;
-                            }
+                            _current = _hashtableContentsToEnumerate[_index];
+                            _index++;
+                            return true;
                         }
                     }
-                }
 
-                if ((_index == _hashtableContentsToEnumerate.Length) && _sentinel != null)
-                {
-                    _current = _sentinel;
-                    _index++;
-                    return true;
+                    if (_index == _hashtableContentsToEnumerate.Length)
+                    {
+                        _current = _sentinel;
+                        _index++;
+                        return true;
+                    }
                 }
 
                 _current = default(TValue);