public inbox for linux-rt-users@vger.kernel.org
 help / color / mirror / Atom feed
From: John Kacur <jkacur@redhat.com>
To: linux-rt-users <linux-rt-users@vger.kernel.org>
Cc: Clark Williams <williams@redhat.com>,
	John Kacur <jkacur@redhat.com>,
	"Claude Sonnet 4.5" <noreply@anthropic.com>
Subject: [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability
Date: Thu, 19 Feb 2026 16:21:18 -0500	[thread overview]
Message-ID: <20260219212120.227106-1-jkacur@redhat.com> (raw)

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


             reply	other threads:[~2026-02-19 21:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-19 21:21 John Kacur [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260219212120.227106-1-jkacur@redhat.com \
    --to=jkacur@redhat.com \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=noreply@anthropic.com \
    --cc=williams@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox