Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: 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>

authored by

Wander Lairson Costa and committed by
Tomas Glozar
4bf4ef52 48fbcd4d

+11 -3
+11 -3
tools/tracing/rtla/src/trace.c
··· 342 342 static void trace_event_save_hist(struct trace_instance *instance, 343 343 struct trace_events *tevent) 344 344 { 345 - int index, out_fd; 345 + size_t index, hist_len; 346 346 mode_t mode = 0644; 347 347 char path[MAX_PATH]; 348 348 char *hist; 349 - size_t hist_len; 349 + int out_fd; 350 350 351 351 if (!tevent) 352 352 return; ··· 378 378 index = 0; 379 379 hist_len = strlen(hist); 380 380 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; 382 390 } while (index < hist_len); 383 391 384 392 free(hist);