Skip to content

Commit

Permalink
robustly handle 'missing' enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
blattm committed Oct 13, 2023
1 parent 41d1b28 commit 6962e1e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 3 additions & 2 deletions decompiler/structures/pseudo/complextypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ class Enum(ComplexType):
def add_member(self, member: ComplexTypeMember):
self.members[member.value] = member

def get_name_by_value(self, value: int) -> str:
return self.members.get(value).name
def get_name_by_value(self, value: int) -> Optional[str]:
member = self.members.get(value)
return member.name if member is not None else None

def declaration(self) -> str:
members = ",\n\t".join(f"{x.name} = {x.value}" for x in self.members.values())
Expand Down
5 changes: 4 additions & 1 deletion decompiler/structures/pseudo/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ def __str__(self) -> str:
Constants of type Enum are represented as strings (corresponding enumerator identifiers).
"""
if isinstance(self._type, Enum):
return self._type.get_name_by_value(self.value)
name = self._type.get_name_by_value(self.value)
if name is not None:
return name
# otherwise, i.e. if value is not found in Enum class, fall through
if self._type.is_boolean:
return "true" if self.value else "false"
if isinstance(self.value, str):
Expand Down

0 comments on commit 6962e1e

Please sign in to comment.