2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-09-01 06:45:38 +00:00

Use string startswith() and endswith() methods

... instead of slicing to check for prefixes and suffixes.

This change prevents a crash in aa-mergeprof - if `replacement` is empty,
trying to access `replacement[0]` causes an IndexError.
Using `.startswith()` works without crashing.

This backports parts of the severity.py changes in
commit 091c6ad59d
by Mark Grassi.
This commit is contained in:
Christian Boltz
2022-10-09 20:46:35 +02:00
parent e8c7f0f84f
commit e1714b9631

View File

@@ -168,9 +168,9 @@ class Severity(object):
leading = True
if resource.find(variable + "/") != -1 and resource.find(variable + "//") == -1:
trailing = True
if replacement[0] == '/' and replacement[:2] != '//' and leading: # finds if the replacement has leading / or not
if replacement.startswith('/') and not replacement.startswith('//') and leading: # finds if the replacement has leading / or not
replacement = replacement[1:]
if replacement[-1] == '/' and replacement[-2:] != '//' and trailing:
if replacement.endswith('/') and not replacement.endswith('//') and trailing:
replacement = replacement[:-1]
return resource.replace(variable, replacement)