Skip to content

Commit 5677a3d

Browse files
committed
tracing: Fix last_cmd_set() string management in histogram code
Using strnlen(dest, str, n) is confusing, as the size of dest must be strlen(dest) + n + 1. Even more confusing, using sizeof(string constant) gives you strlen(string constant) + 1 and not just strlen(). These two together made using strncat() with a constant string a bit off in the calculations as we have: len = sizeof(HIST_PREFIX) + strlen(str) + 1; kfree(last_cmd); last_cmd = kzalloc(len, GFP_KERNEL); strcpy(last_cmd, HIST_PREFIX); len -= sizeof(HIST_PREFIX) + 1; strncat(last_cmd, str, len); The above works if we s/sizeof/strlen/ with HIST_PREFIX (which is defined as "hist:", but because sizeof(HIST_PREFIX) is equal to strlen(HIST_PREFIX) + 1, we can drop the +1 in the code. But at least comment that we are doing so. Link: https://lore.kernel.org/all/202203082112.Iu7tvFl4-lkp@intel.com/ Fixes: 9f8e5ae ("tracing: Fix allocation of last_cmd in last_cmd_set()") Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 173c204 commit 5677a3d

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

kernel/trace/trace_events_hist.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,14 +749,16 @@ static void last_cmd_set(struct trace_event_file *file, char *str)
749749
if (!str)
750750
return;
751751

752-
len = sizeof(HIST_PREFIX) + strlen(str) + 1;
752+
/* sizeof() contains the nul byte */
753+
len = sizeof(HIST_PREFIX) + strlen(str);
753754
kfree(last_cmd);
754755
last_cmd = kzalloc(len, GFP_KERNEL);
755756
if (!last_cmd)
756757
return;
757758

758759
strcpy(last_cmd, HIST_PREFIX);
759-
len -= sizeof(HIST_PREFIX) + 1;
760+
/* Again, sizeof() contains the nul byte */
761+
len -= sizeof(HIST_PREFIX);
760762
strncat(last_cmd, str, len);
761763

762764
if (file) {

0 commit comments

Comments
 (0)