Skip to content

Commit

Permalink
Windows Envars: Fix unbound locals
Browse files Browse the repository at this point in the history
Technically, these were protected against `UnboundLocalError` exceptions
through the use of the `sys` and `ntuser` boolean variables, but this
really isn't the best way to prevent that from happening, and
type-checkers still warn about the potentially unbound locals. This fix
instead uses the `sys` and `ntuser` variables to hold the registry keys,
preinitializing them to `None` and checking their value before
attempting to access instance methods.
  • Loading branch information
dgmcdona committed Feb 12, 2025
1 parent 8dceeb2 commit 443cfd2
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions volatility3/framework/plugins/windows/envars.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,20 @@ def _get_silent_vars(self) -> List[str]:
symbol_table=kernel.symbol_table_name,
hive_offsets=None,
):
sys = False
ntuser = False

## The global variables
sys = None
try:
key = hive.get_key(
sys = hive.get_key(
"CurrentControlSet\\Control\\Session Manager\\Environment"
)
sys = True
except (KeyError, registry.RegistryFormatException):
with contextlib.suppress(KeyError, registry.RegistryFormatException):
key = hive.get_key(
sys = hive.get_key(
"ControlSet001\\Control\\Session Manager\\Environment"
)
sys = True
if sys:
with contextlib.suppress(KeyError, registry.RegistryFormatException):
for node in key.get_values():
for node in sys.get_values():
try:
value_node_name = node.get_name()
if value_node_name:
Expand All @@ -99,13 +95,13 @@ def _get_silent_vars(self) -> List[str]:
)
continue

ntuser = None
## The user-specific variables
with contextlib.suppress(KeyError, registry.RegistryFormatException):
key = hive.get_key("Environment")
ntuser = True
ntuser = hive.get_key("Environment")
if ntuser:
with contextlib.suppress(KeyError, registry.RegistryFormatException):
for node in key.get_values():
for node in ntuser.get_values():
try:
value_node_name = node.get_name()
if value_node_name:
Expand Down

0 comments on commit 443cfd2

Please sign in to comment.