Description
Storing a caller’s list or array directly leaves both the caller and the object with a reference to the same data.
Potential impact
Later edits to the original can change values or permissions that have already been validated.
Remediation
Validate element types and permitted values before storing them. Copy a Python list with value[:], list(value) or copy.copy(value). If mutable elements also need isolation, copy them or use immutable values. Basic NumPy slicing creates a view, so use copy() for independent array storage.
Examples
The example assumes a Python list whose elements need no separate copy.
Before
def set_values(self, values):
self.__values = values
After
def set_values(self, values):
self.__values = values[:]
With values[:], adding, removing or replacing an item in the caller’s list leaves the entries in self.__values unchanged.