Skip to content

Commit

Permalink
Objects: Update enum lookups to support duplicate values
Browse files Browse the repository at this point in the history
Fixes #1060
  • Loading branch information
ikelos committed Dec 18, 2023
1 parent a08b780 commit 29c9857
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions volatility3/framework/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,21 @@ def _generate_inverse_choices(cls, choices: Dict[str, int]) -> Dict[int, str]:
inverse_choices: Dict[int, str] = {}
for k, v in choices.items():
if v in inverse_choices:
# Technically this shouldn't be a problem, but since we inverse cache
# and can't map one value to two possibilities we throw an exception during build
# We can remove/work around this if it proves a common issue
raise ValueError(
f"Enumeration value {v} duplicated as {k} and {inverse_choices[v]}"
# Issue #1060
# There is now a case where the kernel contains identifiers that map to the same value
# so we've opted to select the first correct value for lookups
vollog.debug(
"Duplicate identifier when creating inverse enumeration choice for lookups - {k} has the same value {v} as {inverse_choices[v]}"
)
continue
inverse_choices[v] = k
return inverse_choices

def lookup(self, value: int = None) -> str:
"""Looks up an individual value and returns the associated name."""
"""Looks up an individual value and returns the associated name.
If multiple Identifiers map to the same value, the first matching identifier will be returned
"""
if value is None:
return self.lookup(self)
if value in self._inverse_choices:
Expand Down Expand Up @@ -640,7 +644,10 @@ class VolTemplateProxy(interfaces.objects.ObjectInterface.VolTemplateProxy):

@classmethod
def lookup(cls, template: interfaces.objects.Template, value: int) -> str:
"""Looks up an individual value and returns the associated name."""
"""Looks up an individual value and returns the associated name.
If multiple Identifiers map to the same value, the first matching identifier will be returned
"""
_inverse_choices = Enumeration._generate_inverse_choices(
template.vol["choices"]
)
Expand Down

0 comments on commit 29c9857

Please sign in to comment.