Skip to contentSkip to conversation

perf intel-pt: replace strlcpy() with strscpy()

PATCH In review [email protected] 2 participants 2 messages Dec 12, 2023 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf intel-pt: replace strlcpy() with strscpy()

From: Yang Guang <[email protected]>

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated. No return values were used, so direct replacement is safe.

---
 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/intel-pt-decoder/intel-pt-decoder.c +1 -1
index b450178e3420..5b14c6701ecb 100644
@@ -507,7 +507,7 @@ int intel_pt__strerror(int code, char *buf, size_t buflen)
 {
 	if (code < 1 || code >= INTEL_PT_ERR_MAX)
 		code = INTEL_PT_ERR_UNK;
-	strlcpy(buf, intel_pt_err_msgs[code], buflen);
+	strscpy(buf, intel_pt_err_msgs[code], buflen);
 	return 0;
 }
 
Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 17 lines · On 12/12/23 05:22, [email protected] wrote:

From: Yang Guang <[email protected]>

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated. No return values were used, so direct replacement is safe.

---
 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/intel-pt-decoder/intel-pt-decoder.c +0 -0
index b450178e3420..5b14c6701ecb 100644

tools/perf/util/intel-pt-decoder/intel-pt-decoder.c int intel_pt__strerror(int code, char *buf, size_t buflen) · line 513

Quoted 8 lines
tools/perf/util/intel-pt-decoder/intel-pt-decoder.c +1 -1
@@ -507,7 +507,7 @@ int intel_pt__strerror(int code, char *buf, size_t buflen)
 {
 	if (code < 1 || code >= INTEL_PT_ERR_MAX)
 		code = INTEL_PT_ERR_UNK;
-	strlcpy(buf, intel_pt_err_msgs[code], buflen);
+	strscpy(buf, intel_pt_err_msgs[code], buflen);
 	return 0;
 }
 

perf doesn't have strscpy():

util/intel-pt-decoder/intel-pt-decoder.c: In function ‘intel_pt__strerror’:
util/intel-pt-decoder/intel-pt-decoder.c:510:9: error: implicit declaration of function ‘strscpy’; did you mean ‘strlcpy’? [-Werror=implicit-function-declaration]

  510 |         strscpy(buf, intel_pt_err_msgs[code], buflen);
      |         ^~~~~~~
      |         strlcpy

cc1: all warnings being treated as errors