Skip to content

Commit 4bf4ef5

Browse files
walaclenticularis39
authored andcommitted
rtla/trace: Fix write loop in trace_event_save_hist()
The write loop in trace_event_save_hist() does not correctly handle errors from the write() system call. If write() returns -1, this value is added to the loop index, leading to an incorrect memory access on the next iteration and potentially an infinite loop. The loop also fails to handle EINTR. Fix the write loop by introducing proper error handling. The return value of write() is now stored in a ssize_t variable and checked for errors. The loop retries the call if interrupted by a signal and breaks on any other error after logging it with strerror(). Additionally, change the index variable type from int to size_t to match the type used for buffer sizes and by strlen(), improving type safety. Fixes: 761916f ("rtla/trace: Save event histogram output to a file") Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-16-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 48fbcd4 commit 4bf4ef5

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

tools/tracing/rtla/src/trace.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,11 @@ 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 index, out_fd;
345+
size_t index, hist_len;
346346
mode_t mode = 0644;
347347
char path[MAX_PATH];
348348
char *hist;
349-
size_t hist_len;
349+
int out_fd;
350350

351351
if (!tevent)
352352
return;
@@ -378,7 +378,15 @@ static void trace_event_save_hist(struct trace_instance *instance,
378378
index = 0;
379379
hist_len = strlen(hist);
380380
do {
381-
index += write(out_fd, &hist[index], hist_len - index);
381+
const ssize_t written = write(out_fd, &hist[index], hist_len - index);
382+
383+
if (written < 0) {
384+
if (errno == EINTR)
385+
continue;
386+
err_msg(" Error writing hist file: %s\n", strerror(errno));
387+
break;
388+
}
389+
index += written;
382390
} while (index < hist_len);
383391

384392
free(hist);

0 commit comments

Comments
 (0)