Skip to content

Commit

Permalink
Fix issues found by CodeQL
Browse files Browse the repository at this point in the history
library/network_connections.py
- 'except' clause does nothing but pass and there is no
  explanatory comment.
- Local variable 'dev_state' may be used before it is initialized.
- Local variable 'dev_id' may be used before it is initialized.

module_utils/network_lsr/argument_validator.py
- 'except' clause does nothing but pass and there is no
  explanatory comment.

module_utils/network_lsr/utils.py
- Mixing implicit and explicit returns may indicate an error
  as implicit returns always return None.

tests/unit/test_network_connections.py
- Module 'network_lsr.argument_validator' is imported with both
  'import' and 'import from'.
- assertTrue(a == b) cannot provide an informative message.
  Using assertEqual(a, b) instead will give more informative
  messages.

Signed-off-by: Noriko Hosoi <nhosoi@redhat.com>
  • Loading branch information
nhosoi committed Feb 9, 2023
1 parent c40ede8 commit 76e3274
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 79 deletions.
18 changes: 11 additions & 7 deletions library/network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,14 @@ def connection_compare(
try:
con_a.normalize()
except Exception:
# It's already checked normalize is successful.
pass
if normalize_b:
con_b = NM.SimpleConnection.new_clone(con_b)
try:
con_b.normalize()
except Exception:
# It's already checked normalize is successful.
pass
if compare_flags is None:
compare_flags = NM.SettingCompareFlags.IGNORE_TIMESTAMP
Expand Down Expand Up @@ -1493,8 +1495,7 @@ def check_activated(ac, dev):
except AttributeError:
ac_reason = None

if dev:
dev_state = dev.get_state()
dev_state = dev.get_state() if dev else None

if ac_state == NM.ActiveConnectionState.ACTIVATING:
if (
Expand Down Expand Up @@ -1528,7 +1529,7 @@ def check_activated(ac, dev):
)
# the state of the active connection is not very helpful.
# see if the device-state is better.
if (
if dev_state is not None and (
dev_state <= NM.DeviceState.DISCONNECTED
or dev_state > NM.DeviceState.DEACTIVATING
):
Expand Down Expand Up @@ -1567,10 +1568,11 @@ def check_activated_cb():
)
except Exception:
ac_id = None
if dev:
dev_id = dev.connect(
"notify::state", lambda source, pspec: check_activated_cb()
)
dev_id = (
dev.connect("notify::state", lambda source, pspec: check_activated_cb())
if dev
else None
)

try:
if not Util.GMainLoop_run(wait_time):
Expand Down Expand Up @@ -2550,6 +2552,8 @@ def forget_nm_connection(self, path):
]
)
except Exception:
# Assume that NetworkManager is not present.
# There is no connection to forget.
pass

def run_action_absent(self, idx):
Expand Down
6 changes: 6 additions & 0 deletions module_utils/network_lsr/argument_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def _validate_impl(self, value, name):
except Exception:
table = value
except Exception:
# Exception handling is done next.
pass
if table is None:
raise ValidationError(
Expand Down Expand Up @@ -336,6 +337,7 @@ def _validate_impl(self, value, name):
if isinstance(value, Util.STRING_TYPE) or v2 == value:
v = v2
except Exception:
# Exception handling is done next.
pass
if v is None:
raise ValidationError(
Expand Down Expand Up @@ -374,11 +376,13 @@ def _validate_impl(self, value, name):
try:
range = (int(match_group.group(1)), int(match_group.group(2)))
except Exception:
# Exception handling is done below.
pass
else:
try:
range = (int(value), int(value))
except Exception:
# Exception handling is done below.
pass
elif isinstance(value, bool):
# bool can (probably) be converted to integer type,
Expand Down Expand Up @@ -425,6 +429,7 @@ def _validate_impl(self, value, name):
if isinstance(value, Util.STRING_TYPE) or isinstance(value, int):
return Util.boolean(value)
except Exception:
# Exception handling is done next.
pass
raise ValidationError(name, "must be an boolean but is '%s'" % (value))

Expand Down Expand Up @@ -2665,6 +2670,7 @@ def _parse_route_tables_mapping(cls, file_content, mapping):
try:
tableid = int(table[2:], 16)
except Exception:
# Exception handling is done next.
pass
if tableid is None or tableid < 0 or tableid > 0xFFFFFFFF:
continue
Expand Down
2 changes: 2 additions & 0 deletions module_utils/network_lsr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def addr_family_norm(addr_family):
if addr_family in ["6", "inet6", "ip6", "ipv6", "IPv6"]:
return socket.AF_INET6
Util.addr_family_check(addr_family)
return None

@staticmethod
def addr_family_prefix_length(family):
Expand All @@ -348,6 +349,7 @@ def addr_family_prefix_length(family):
if addr_family == socket.AF_INET6:
return 128
Util.addr_family_check(addr_family)
return None

@staticmethod
def addr_family_valid_prefix(family, prefix):
Expand Down
Loading

0 comments on commit 76e3274

Please sign in to comment.