pytest: fix out of bounds access to bad_pattern_ids
PATCH 1/1 pytest: fix out of bounds access to bad_pattern_ids
From: Denis Mukhin <[email protected]>
Current logic in ConsoleBase() mislabels every bad pattern and walks pattern past the end when the last one matches.
Ensure out of bound access to bad_pattern_ids is handled correctly while processing test console output.
---
I hit that while testing reset v5 series in the CI:
https://github.com/dmkhn/u-boot/commits/u/x86-reset-v5/
--- test/py/console_base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
test/py/console_base.py +6 -6
@@ -212,24 +212,24 @@ class ConsoleBase(object): while not self.lab_mode and loop_num > 0: loop_num -= 1 while config_spl_serial and not env_spl_skipped and env_spl_banner_times > 0: - m = self.p.expect([pattern_u_boot_spl_signon, - pattern_lab_mode] + self.bad_patterns) + extra_patterns = [pattern_u_boot_spl_signon, pattern_lab_mode] + m = self.p.expect(extra_patterns + self.bad_patterns) if m == 1: self.set_lab_mode() break elif m != 0: raise BootFail('Bad pattern found on SPL console: ' + - self.bad_pattern_ids[m - 1]) + self.bad_pattern_ids[m - len(extra_patterns)]) env_spl_banner_times -= 1 if not self.lab_mode: - m = self.p.expect([pattern_u_boot_main_signon, - pattern_lab_mode] + self.bad_patterns) + extra_patterns = [pattern_u_boot_main_signon, pattern_lab_mode] + m = self.p.expect(extra_patterns + self.bad_patterns) if m == 1: self.set_lab_mode() elif m != 0: raise BootFail('Bad pattern found on console: ' + - self.bad_pattern_ids[m - 1]) + self.bad_pattern_ids[m - len(extra_patterns)]) if not self.lab_mode: self.u_boot_version_string = self.p.after while True:
Signature · 1 line
2.54.0
Hi Denis,
Quoted 1 line · BOON KHAI via U-Boot
pytest: fix out of bounds access to
bad_pattern_ids
Subject is fine, but please sharpen the body: with pattern_lab_mode at index 1, every bad-pattern index is off by one, so the wrong id is reported and a match on the final bad pattern indexes past the end of bad_pattern_ids.
Quoted 16 lines
Current logic in
ConsoleBase()mislabels every bad pattern and walks pattern past the end when the last one matches.Ensure out of bound access to
bad_pattern_idsis handled correctly while processing test console output.Fixes 8308a5eed6e6 ("test: Introduce lab mode")Signed-off-by Denis Mukhin <[email protected]>
test/py/console_base.py| 12 ++++++------1 file changed, 6 insertions(+), 6 deletions(-)test/py/console_base.py +0
-1@@ -212,24 +212,24 @@ class ConsoleBase(object): - m = self.p.expect([pattern_u_boot_spl_signon,
test/py/console_base.py class ConsoleBase(object): · line 219
Quoted 10 lines
test/py/console_base.py +3
-2- pattern_lab_mode] + self.bad_patterns) + extra_patterns = [pattern_u_boot_spl_signon, pattern_lab_mode] + m = self.p.expect(extra_patterns + self.bad_patterns) if m == 1: self.set_lab_mode() break elif m != 0: raise BootFail('Bad pattern found on SPL console: ' + - self.bad_pattern_ids[m - 1]) + self.bad_pattern_ids[m - len(extra_patterns)])
The fix is correct. Please hoist the length into a local so the offset arithmetic reads more clearly, e.g.:
extra_patterns = [pattern_u_boot_spl_signon, pattern_lab_mode] base = len(extra_patterns)
m = self.p.expect(extra_patterns + self.bad_patterns)
...
raise BootFail('Bad pattern found on SPL console: ' +
self.bad_pattern_ids[m - base])
Same for the main-signon block below. What do you think?
BTW while you are here, please double-check the other bad_pattern_ids[m - 1] sites in this file - the pattern is only correct where exactly one non-bad pattern precedes self.bad_patterns.
Regards,
Simon