Skip to content

Commit 52a0fc1

Browse files
committed
Fix Advisory.__hash__()
The hash is computed from a string, which is built by iterating over attributes of type Iterable/Sequence, such as impacted_package_urls. This change sorts those attributes before concatenating to the string, to avoid different hashes when the order of impacted_package_urls, etc differs. NB A better solution would be to avoid implementing __hash__() and use immutable collections like tuple and frozenset for the above mentioned attributes instead. See https://eng.lyft.com/hashing-and-equality-in-python-2ea8c738fb9d Signed-off-by: Haiko Schol <hs@haikoschol.com>
1 parent 4f5caea commit 52a0fc1

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

vulnerabilities/data_source.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class Advisory:
6161
def __hash__(self):
6262
s = '{}{}{}{}{}'.format(
6363
self.summary,
64-
''.join({str(p) for p in self.impacted_package_urls}),
65-
''.join({str(p) for p in self.resolved_package_urls}),
66-
''.join(self.reference_urls),
64+
''.join(sorted([str(p) for p in self.impacted_package_urls])),
65+
''.join(sorted([str(p) for p in self.resolved_package_urls])),
66+
''.join(sorted(self.reference_urls)),
6767
self.cve_id,
6868
)
6969
return hash(s)

0 commit comments

Comments
 (0)