Skip to contentSkip to conversation

tracing/eprobe: no need to check for negative ret value for snprintf

PATCH In review Quanfa Fu 3 participants 4 messages Jan 9, 2023 – Jan 10, 2023 Tracing

lore
Share & tools
lore

Quanfa Fu

author
#0 lore raw

PATCH v3 1/1 tracing/eprobe: no need to check for negative ret value for snprintf

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

-----
V2 -> V3: continue to use snprintf
V1 -> V2: memory allc uses kzalloc and replace snprintf with memcpy

---
 kernel/trace/trace_eprobe.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
kernel/trace/trace_eprobe.c +4 -8
index 352b65e2b910..594ac1d086aa 100644
@@ -923,17 +923,13 @@ 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;
-		}
+		if (i)
+			ret = snprintf(p, len, " %s", argv[i]);
+		else
+			ret = snprintf(p, len, "%s", argv[i]);
 		p += ret;
 		len -= ret;
 	}
-	p[-1] = '\0';
 
 	/*
 	 * Ensure the filter string can be parsed correctly. Note, this
Signature · 1 line
2.31.1
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 3 lines · On Mon, 9 Jan 2023 12:06:25 +0800 Quanfa Fu <[email protected]> wrote:

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

Thanks for simplifying, this looks good to me.

Quoted 38 lines

-----
V2 -> V3: continue to use snprintf
V1 -> V2: memory allc uses kzalloc and replace snprintf with memcpy

---
 kernel/trace/trace_eprobe.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
kernel/trace/trace_eprobe.c +4 -8
index 352b65e2b910..594ac1d086aa 100644
@@ -923,17 +923,13 @@ 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;
-		}
+		if (i)
+			ret = snprintf(p, len, " %s", argv[i]);
+		else
+			ret = snprintf(p, len, "%s", argv[i]);
 		p += ret;
 		len -= ret;
 	}
-	p[-1] = '\0';
 
 	/*
 	 * Ensure the filter string can be parsed correctly. Note, this
Signature · 1 line
2.31.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer Reviewed-by
#2 lore raw
Quoted 10 lines · On Mon, 9 Jan 2023 23:59:13 +0900 Masami Hiramatsu (Google)…
Quoted 3 lines · On Mon, 9 Jan 2023 12:06:25 +0800 Quanfa Fu <[email protected]> wrote:

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

Thanks for simplifying, this looks good to me.

Masami, do you want to take it into the probes branch, or do you want me to take it into the tracing branch?

-- Steve

Quoted 3 lines
Quoted 3 lines
#3 lore raw
Quoted 10 lines · On Mon, 9 Jan 2023 10:12:26 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 7 lines · On Mon, 9 Jan 2023 23:59:13 +0900 Masami Hiramatsu (Google)…
Quoted 3 lines · On Mon, 9 Jan 2023 12:06:25 +0800 Quanfa Fu <[email protected]> wrote:

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

Quoted 8 lines
Quoted 3 lines

Thanks for simplifying, this looks good to me.

Masami, do you want to take it into the probes branch, or do you want me to take it into the tracing branch?

Yes, I'll take it to probe/for-next.

Thank you!

Quoted 7 lines

-- Steve

Quoted 3 lines
Quoted 3 lines
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>