All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tracing/eprobe: Replace snprintf with memcpy
@ 2023-01-09  3:32 Quanfa Fu
  2023-01-09  3:52 ` Steven Rostedt
  0 siblings, 1 reply; 3+ messages in thread
From: Quanfa Fu @ 2023-01-09  3:32 UTC (permalink / raw)
  To: rostedt, mhiramat; +Cc: linux-kernel, linux-trace-kernel, Quanfa Fu

No need to check for negative return value from snprintf() as the
code does not return negative values. Replace snprintf with memcpy.

Signed-off-by: Quanfa Fu <quanfafu@gmail.com>

-----
V1 -> V2: memory allc uses kzalloc and replace snprintf with memcpy
---
 kernel/trace/trace_eprobe.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/kernel/trace/trace_eprobe.c b/kernel/trace/trace_eprobe.c
index 352b65e2b910..56eb39f495f6 100644
--- a/kernel/trace/trace_eprobe.c
+++ b/kernel/trace/trace_eprobe.c
@@ -905,7 +905,7 @@ static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[
 static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[])
 {
 	struct event_filter *dummy = NULL;
-	int i, ret, len = 0;
+	int i, ret, arg_len, len = 0;
 	char *p;
 
 	if (argc == 0) {
@@ -923,17 +923,17 @@ static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch
 
 	p = ep->filter_str;
 	for (i = 0; i < argc; i++) {
-		ret = snprintf(p, len, "%s ", argv[i]);
-		if (ret < 0)
-			goto error;
-		if (ret > len) {
-			ret = -E2BIG;
-			goto error;
-		}
-		p += ret;
-		len -= ret;
+		arg_len = strlen(argv[i]);
+		memcpy((void *)p, argv[i], arg_len);
+
+		if (i == argc - 1)
+			p[arg_len] = '\0';
+		else
+			p[arg_len] = ' ';
+
+		p += arg_len + 1;
+		len -= arg_len + 1;
 	}
-	p[-1] = '\0';
 
 	/*
 	 * Ensure the filter string can be parsed correctly. Note, this
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] tracing/eprobe: Replace snprintf with memcpy
  2023-01-09  3:32 [PATCH v2] tracing/eprobe: Replace snprintf with memcpy Quanfa Fu
@ 2023-01-09  3:52 ` Steven Rostedt
  2023-01-09  4:07   ` Quanfa Fu
  0 siblings, 1 reply; 3+ messages in thread
From: Steven Rostedt @ 2023-01-09  3:52 UTC (permalink / raw)
  To: Quanfa Fu; +Cc: mhiramat, linux-kernel, linux-trace-kernel

On Mon,  9 Jan 2023 11:32:13 +0800
Quanfa Fu <quanfafu@gmail.com> wrote:

> @@ -923,17 +923,17 @@ static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch
>  
>  	p = ep->filter_str;
>  	for (i = 0; i < argc; i++) {
> -		ret = snprintf(p, len, "%s ", argv[i]);
> -		if (ret < 0)
> -			goto error;
> -		if (ret > len) {
> -			ret = -E2BIG;
> -			goto error;
> -		}
> -		p += ret;
> -		len -= ret;
> +		arg_len = strlen(argv[i]);
> +		memcpy((void *)p, argv[i], arg_len);
> +
> +		if (i == argc - 1)
> +			p[arg_len] = '\0';
> +		else
> +			p[arg_len] = ' ';
> +
> +		p += arg_len + 1;
> +		len -= arg_len + 1;
>  	}

The above is too complex. I mentioned strncat() but you could still
just keep snprintf() too, which adds the '\0';

	for (i = 0; i < argc; i++) {
		if (i)
			ret = snprintf(p, len, " %s", argv[i]);
		else
			ret = snprintf(p, len, "%s", argv[i]);
		p += ret;
		len -= ret;
	}

-- Steve

> -	p[-1] = '\0';
>  
>  	/*
>  	 * Ensure the filter string can be parsed correctly. Note, this

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] tracing/eprobe: Replace snprintf with memcpy
  2023-01-09  3:52 ` Steven Rostedt
@ 2023-01-09  4:07   ` Quanfa Fu
  0 siblings, 0 replies; 3+ messages in thread
From: Quanfa Fu @ 2023-01-09  4:07 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: mhiramat, linux-kernel, linux-trace-kernel

Thanks, I'll check it out.
Thanks !!

On Mon, Jan 9, 2023 at 11:52 AM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Mon,  9 Jan 2023 11:32:13 +0800
> Quanfa Fu <quanfafu@gmail.com> wrote:
>
> > @@ -923,17 +923,17 @@ static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch
> >
> >       p = ep->filter_str;
> >       for (i = 0; i < argc; i++) {
> > -             ret = snprintf(p, len, "%s ", argv[i]);
> > -             if (ret < 0)
> > -                     goto error;
> > -             if (ret > len) {
> > -                     ret = -E2BIG;
> > -                     goto error;
> > -             }
> > -             p += ret;
> > -             len -= ret;
> > +             arg_len = strlen(argv[i]);
> > +             memcpy((void *)p, argv[i], arg_len);
> > +
> > +             if (i == argc - 1)
> > +                     p[arg_len] = '\0';
> > +             else
> > +                     p[arg_len] = ' ';
> > +
> > +             p += arg_len + 1;
> > +             len -= arg_len + 1;
> >       }
>
> The above is too complex. I mentioned strncat() but you could still
> just keep snprintf() too, which adds the '\0';
>
>         for (i = 0; i < argc; i++) {
>                 if (i)
>                         ret = snprintf(p, len, " %s", argv[i]);
>                 else
>                         ret = snprintf(p, len, "%s", argv[i]);
>                 p += ret;
>                 len -= ret;
>         }
>
> -- Steve
>
> > -     p[-1] = '\0';
> >
> >       /*
> >        * Ensure the filter string can be parsed correctly. Note, this

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-01-09  4:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-09  3:32 [PATCH v2] tracing/eprobe: Replace snprintf with memcpy Quanfa Fu
2023-01-09  3:52 ` Steven Rostedt
2023-01-09  4:07   ` Quanfa Fu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.