diff mbox series

[v2,2/6] trace-cmd: Fix warnings reported by gcc 8.2

Message ID 20190108150015.21327-3-kaslevs@vmware.com (mailing list archive)
State Accepted
Headers show
Series Add VM guest kernel tracing using vsock sockets for transport | expand

Commit Message

Slavomir Kaslev Jan. 8, 2019, 3 p.m. UTC
Compiling trace-cmd with gcc 8.2 reports:

    trace-read.c: In function ‘append_pid_filter’:
    trace-read.c:414:3: warning: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Wrestrict]
       sprintf(filter, "%s||" FILTER_FMT, filter, pid, pid, pid);
       ^~~~~~~

since `snprintf` declares its first argument as __restrict but `buf` is also
passed as argument #5.

Signed-off-by: Slavomir Kaslev <kaslevs@vmware.com>
---
 tracecmd/trace-read.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Steven Rostedt Jan. 8, 2019, 10:32 p.m. UTC | #1
On Tue,  8 Jan 2019 17:00:11 +0200
Slavomir Kaslev <kaslevs@vmware.com> wrote:

> Compiling trace-cmd with gcc 8.2 reports:
> 
>     trace-read.c: In function ‘append_pid_filter’:
>     trace-read.c:414:3: warning: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Wrestrict]
>        sprintf(filter, "%s||" FILTER_FMT, filter, pid, pid, pid);
>        ^~~~~~~
> 
> since `snprintf` declares its first argument as __restrict but `buf` is also
> passed as argument #5.
> 
> Signed-off-by: Slavomir Kaslev <kaslevs@vmware.com>

FYI, I took this patch but changed the subject and description to this:

From: Slavomir Kaslev <kaslevs@vmware.com>
Subject: [PATCH] trace-cmd: Do not use target string pointer as parameter to
 sprintf()

Compiling trace-cmd with gcc 8.2 reports:

    trace-read.c: In function ‘append_pid_filter’:
    trace-read.c:414:3: warning: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Wrestrict]
       sprintf(filter, "%s||" FILTER_FMT, filter, pid, pid, pid);
       ^~~~~~~

since `snprintf` declares its first argument as __restrict but `buf` is also
passed as argument #5.

The code just concatenates to the filter string. Use the original string
length as the position to write to filter.

Signed-off-by: Slavomir Kaslev <kaslevs@vmware.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

-- Steve
diff mbox series

Patch

diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c
index c406d66..1f91fff 100644
--- a/tracecmd/trace-read.c
+++ b/tracecmd/trace-read.c
@@ -386,7 +386,7 @@  static void add_pid_filter(const char *arg)
 static char *append_pid_filter(char *curr_filter, char *pid)
 {
 	char *filter;
-	int len;
+	int len, curr_len;
 
 #define FILTER_FMT "(common_pid==" __STR ")||(pid==" __STR ")||(next_pid==" __STR ")"
 
@@ -405,13 +405,13 @@  static char *append_pid_filter(char *curr_filter, char *pid)
 			die("Failed to allocate for filter %s", curr_filter);
 		sprintf(filter, ".*:" FILTER_FMT, pid, pid, pid);
 	} else {
-
-		len += strlen(curr_filter);
+		curr_len = strlen(curr_filter);
+		len += curr_len;
 
 		filter = realloc(curr_filter, len);
 		if (!filter)
 			die("realloc");
-		sprintf(filter, "%s||" FILTER_FMT, filter, pid, pid, pid);
+		sprintf(filter + curr_len, "||" FILTER_FMT, pid, pid, pid);
 	}
 
 	return filter;