]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rtla/trace: Fix write loop in trace_event_save_hist()
authorWander Lairson Costa <wander@redhat.com>
Mon, 9 Mar 2026 19:46:28 +0000 (16:46 -0300)
committerTomas Glozar <tglozar@redhat.com>
Wed, 11 Mar 2026 14:29:50 +0000 (15:29 +0100)
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: 761916fd02c2 ("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>
tools/tracing/rtla/src/trace.c

index ed7db5f4115ceb4bdbd4e2b569eb27d6340266c4..fed3362527b08d6f70b5220edb3381b2c29d5abc 100644 (file)
@@ -342,11 +342,11 @@ static void trace_event_disable_filter(struct trace_instance *instance,
 static void trace_event_save_hist(struct trace_instance *instance,
                                  struct trace_events *tevent)
 {
-       int index, out_fd;
+       size_t index, hist_len;
        mode_t mode = 0644;
        char path[MAX_PATH];
        char *hist;
-       size_t hist_len;
+       int out_fd;
 
        if (!tevent)
                return;
@@ -378,7 +378,15 @@ static void trace_event_save_hist(struct trace_instance *instance,
        index = 0;
        hist_len = strlen(hist);
        do {
-               index += write(out_fd, &hist[index], hist_len - index);
+               const ssize_t written = write(out_fd, &hist[index], hist_len - index);
+
+               if (written < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       err_msg("  Error writing hist file: %s\n", strerror(errno));
+                       break;
+               }
+               index += written;
        } while (index < hist_len);
 
        free(hist);