public inbox for linux-rt-users@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability
@ 2026-02-19 21:21 John Kacur
  2026-02-19 21:21 ` [PATCH 1/5] hwlatdetect: Make count field backward compatible John Kacur
  2026-02-19 21:21 ` [PATCH 5/5] rt-tests: cyclictest: Remove duplicate option in getopt string John Kacur
  0 siblings, 2 replies; 3+ messages in thread
From: John Kacur @ 2026-02-19 21:21 UTC (permalink / raw)
  To: linux-rt-users; +Cc: Clark Williams, John Kacur, Claude Sonnet 4.5

Add defensive checks to the Sample.__init__ method to prevent
IndexError on malformed trace input:

- Verify fields array has at least 8 elements
- Check cpu_field is non-empty before accessing first character
- Add comments explaining the two different trace formats
- Improve code readability by using a named variable for cpu_field

Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
 src/hwlatdetect/hwlatdetect.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py
index 865f62196c26..38671724f3e3 100755
--- a/src/hwlatdetect/hwlatdetect.py
+++ b/src/hwlatdetect/hwlatdetect.py
@@ -257,8 +257,22 @@ class Tracer(Detector):
 
         def __init__(self, line):
             fields = line.split()
+            if len(fields) < 8:
+                raise ValueError(f"Unexpected trace format: {line}")
+
             kv = key_values(fields)
-            self.cpu = int(fields[1][1:-1]) if fields[1][0] == '[' else int(fields[1][:-5])
+
+            # Parse CPU number from either [NNN] or NNd.... format
+            cpu_field = fields[1]
+            if not cpu_field:
+                raise ValueError(f"Empty CPU field in: {line}")
+
+            if cpu_field[0] == '[':
+                # nolatency-format: [007]
+                self.cpu = int(cpu_field[1:-1])
+            else:
+                # latency-format: 13d....
+                self.cpu = int(cpu_field[:-5])
             i, o = fields[6].split('/')
             ts = fields[7][3:]
             self.timestamp = str(ts)
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-02-19 21:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-19 21:21 [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability John Kacur
2026-02-19 21:21 ` [PATCH 1/5] hwlatdetect: Make count field backward compatible John Kacur
2026-02-19 21:21 ` [PATCH 5/5] rt-tests: cyclictest: Remove duplicate option in getopt string John Kacur

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox