Skip to content

Commit 265905d

Browse files
walaclenticularis39
authored andcommitted
rtla: Use str_has_prefix() for prefix checks
The code currently uses strncmp() combined with strlen() to check if a string starts with a specific prefix. This pattern is verbose and prone to errors if the length does not match the prefix string. Replace this pattern with the str_has_prefix() helper function in both trace.c and utils.c. This improves code readability and safety by handling the prefix length calculation automatically. In addition, remove the unused retval variable from trace_event_save_hist() in trace.c to clean up the function and silence potential compiler warnings. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-12-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 0f4bc9d commit 265905d

2 files changed

Lines changed: 3 additions & 5 deletions

File tree

tools/tracing/rtla/src/trace.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ static void trace_event_disable_filter(struct trace_instance *instance,
342342
static void trace_event_save_hist(struct trace_instance *instance,
343343
struct trace_events *tevent)
344344
{
345-
int retval, index, out_fd;
345+
int index, out_fd;
346346
mode_t mode = 0644;
347347
char path[MAX_PATH];
348348
char *hist;
@@ -356,8 +356,7 @@ static void trace_event_save_hist(struct trace_instance *instance,
356356
return;
357357

358358
/* is this a hist: trigger? */
359-
retval = strncmp(tevent->trigger, "hist:", strlen("hist:"));
360-
if (retval)
359+
if (!str_has_prefix(tevent->trigger, "hist:"))
361360
return;
362361

363362
snprintf(path, ARRAY_SIZE(path), "%s_%s_hist.txt", tevent->system, tevent->event);

tools/tracing/rtla/src/utils.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ static int procfs_is_workload_pid(const char *comm_prefix, struct dirent *proc_e
331331
return 0;
332332

333333
buffer[MAX_PATH-1] = '\0';
334-
retval = strncmp(comm_prefix, buffer, strlen(comm_prefix));
335-
if (retval)
334+
if (!str_has_prefix(buffer, comm_prefix))
336335
return 0;
337336

338337
/* comm already have \n */

0 commit comments

Comments
 (0)