Closed VaslD closed 4 years ago
Thanks for supporting the development of C# Algorithms with your first issue! We look forward to handling it.
Created pull https://github.com/aalhour/C-Sharp-Algorithms/pull/134 to return primes list as a read-only IList
Describe the bug
In
PrimesList.GetAll()
the underlying list of primes is returned as a mutableList<int>
which makes it susceptible to modifications outside the library’s control.Which in turn may interfere with (change behaviors of) methods called later because they rely heavily on the predefined list.
Expected behavior
Although it’s a developer’s job to pay attention and never modify library resources, the library itself should use some protection to make exposed resources secure.
List<T>.AsReadOnly()
is the best option to return a read-only wrapper over the original list. It’s O(1) (no-copy) and syncs with the original list when modified. The return type will have to be changed to eitherIList<int>
orIReadOnlyCollection<int>
(from currentlyList<int>
).Notes
There may be other occurrences that a private/internal collection is returned without additional consideration. I just happen to bump into the one in
PrimesList
. If said collections are not meant to be modified outside the library, they should all be wrapped under a read only collection to prevent contamination.