Skip to content

Commit

Permalink
Explicitly return None for value-returning functions
Browse files Browse the repository at this point in the history
get_clevis_config_from_protected_header() and get_distfile() return
values in some exit paths. In the default case, explicitly return `None`
to point out that the caller expects a return value.

Invert the conditions to move the error/default cases into the "then"
blocks, and the "main case" on the top level.

Spotted by CodeQL "Explicit returns mixed with implicit (fall through)
returns" notes.
  • Loading branch information
martinpitt committed Nov 17, 2022
1 parent 0c24164 commit ab4f438
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
36 changes: 19 additions & 17 deletions pkg/storaged/luksmeta-monitor-hack.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ def get_clevis_config_from_protected_header(protected_header):
header = b64_decode(protected_header).decode("utf-8")
header_object = json.loads(header)
clevis = header_object.get("clevis", None)
if clevis:
pin = clevis.get("pin", None)
if pin == "tang":
return clevis
elif pin == "sss":
subpins = {}
jwes = clevis["sss"]["jwe"]
for jwe in jwes:
subconf = get_clevis_config_from_jwe(jwe)
subpin = subconf["pin"]
if subpin not in subpins:
subpins[subpin] = [subconf[subpin]]
else:
subpins[subpin].append(subconf[subpin])
return {"pin": "sss", "sss": {"t": clevis["sss"]["t"], "pins": subpins}}
else:
return {"pin": pin, pin: {}}
if clevis is None:
return None

pin = clevis.get("pin", None)
if pin == "tang":
return clevis
elif pin == "sss":
subpins = {}
jwes = clevis["sss"]["jwe"]
for jwe in jwes:
subconf = get_clevis_config_from_jwe(jwe)
subpin = subconf["pin"]
if subpin not in subpins:
subpins[subpin] = [subconf[subpin]]
else:
subpins[subpin].append(subconf[subpin])
return {"pin": "sss", "sss": {"t": clevis["sss"]["t"], "pins": subpins}}
else:
return {"pin": pin, pin: {}}


def get_clevis_config_from_jwe(jwe):
Expand Down
29 changes: 16 additions & 13 deletions test/common/lcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,22 @@ def get_dist_map(package):

def get_distfile(url, dist_map, webpack_name):
parts = url.split("/")
if len(parts) > 2 and "cockpit" in parts:
base = parts[-2]
file = parts[-1]
if file == "manifests.js":
return None
if base in dist_map:
path = dist_map[base] + "/" + file
else:
path = f"{BASE_DIR}/dist/" + base + "/" + file
if os.path.exists(path) and os.path.exists(path + ".map"):
return DistFile(path, webpack_name)
else:
sys.stderr.write(f"SKIP {url} -> {path}\n")
if len(parts) < 3 or "cockpit" not in parts:
return None

base = parts[-2]
file = parts[-1]
if file == "manifests.js":
return None
if base in dist_map:
path = dist_map[base] + "/" + file
else:
path = f"{BASE_DIR}/dist/" + base + "/" + file
if os.path.exists(path) and os.path.exists(path + ".map"):
return DistFile(path, webpack_name)
else:
sys.stderr.write(f"SKIP {url} -> {path}\n")
return None


def grow_array(arr, size, val):
Expand Down

0 comments on commit ab4f438

Please sign in to comment.