Skip to contentSkip to conversation

pahole: Make sure the features string is NUL terminated

PATCH In review Arnaldo Carvalho de Melo 3 participants 3 messages Feb 5, 2024 – Feb 6, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pahole: Make sure the features string is NUL terminated

To address this compiler warning:

/home/acme/git/pahole/pahole.c: In function ‘parse_btf_features’:
/home/acme/git/pahole/pahole.c:1353:9: warning: ‘strncpy’ specified bound 1024 equals destination size [-Wstringop-truncation]

   1353 |         strncpy(f, features, sizeof(f));
        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  $ gcc -v |& tail -1
  gcc version 13.2.1 20231205 (Red Hat 13.2.1-6) (GCC)
  $

When building in Release mode, for instance by using the buildcmd.sh script.

Cc Alan Maguire <[email protected]>
Cc Eduard Zingerman <[email protected]>
Fixes 7bc9b9975545ab53 ("pahole: Add --btf_features support")
---
 pahole.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
pahole.c +2 -2
index 768a2feb338a2f3c..0e079ae19f231a5a 100644
@@ -1338,7 +1338,7 @@ static void show_supported_btf_features(FILE *output)
 static void parse_btf_features(const char *features, bool strict)
 {
 	char *saveptr = NULL, *s, *feature_name;
-	char f[BTF_MAX_FEATURE_STR];
+	char f[BTF_MAX_FEATURE_STR + 1];
 
 	init_btf_features();
 
@@ -1350,7 +1350,7 @@ static void parse_btf_features(const char *features, bool strict)
 		return;
 	}
 
-	strncpy(f, features, sizeof(f));
+	strncpy(f, features, BTF_MAX_FEATURE_STR)[BTF_MAX_FEATURE_STR] = '\0';
 	s = f;
 	while ((feature_name = strtok_r(s, ",", &saveptr)) != NULL) {
 		struct btf_feature *feature = find_btf_feature(feature_name);
Signature · 1 line
2.43.0
Tested-by on v1 1/1
#1 lore raw
Quoted 11 lines · Arnaldo Carvalho de Melo

To address this compiler warning:

/home/acme/git/pahole/pahole.c: In function ‘parse_btf_features’:
/home/acme/git/pahole/pahole.c:1353:9: warning: ‘strncpy’ specified bound 1024 equals destination size [-Wstringop-truncation]

   1353 |         strncpy(f, features, sizeof(f));
        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  $ gcc -v |& tail -1
  gcc version 13.2.1 20231205 (Red Hat 13.2.1-6) (GCC)
  $
Quoted 8 lines

When building in Release mode, for instance by using the buildcmd.sh script.

Cc Alan Maguire <[email protected]>
Cc Eduard Zingerman <[email protected]>
Fixes 7bc9b9975545ab53 ("pahole: Add --btf_features support")

---

Can confirm, warning is fixed by this patch.
strncpy() API is not great :(

Reviewed-by on v1 1/1
#2 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 11 lines · Arnaldo Carvalho de Melo

To address this compiler warning:

/home/acme/git/pahole/pahole.c: In function ‘parse_btf_features’:
/home/acme/git/pahole/pahole.c:1353:9: warning: ‘strncpy’ specified bound 1024 equals destination size [-Wstringop-truncation]

   1353 |         strncpy(f, features, sizeof(f));
        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  $ gcc -v |& tail -1
  gcc version 13.2.1 20231205 (Red Hat 13.2.1-6) (GCC)
  $
Quoted 8 lines

When building in Release mode, for instance by using the buildcmd.sh script.

Cc Alan Maguire <[email protected]>
Cc Eduard Zingerman <[email protected]>
Fixes 7bc9b9975545ab53 ("pahole: Add --btf_features support")

Thanks for fixing this Arnaldo! I'll make sure to build in release mode in future.

---

Quoted 25 lines
 pahole.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
pahole.c +2 -2
index 768a2feb338a2f3c..0e079ae19f231a5a 100644
@@ -1338,7 +1338,7 @@ static void show_supported_btf_features(FILE *output)
 static void parse_btf_features(const char *features, bool strict)
 {
 	char *saveptr = NULL, *s, *feature_name;
-	char f[BTF_MAX_FEATURE_STR];
+	char f[BTF_MAX_FEATURE_STR + 1];
 
 	init_btf_features();
 
@@ -1350,7 +1350,7 @@ static void parse_btf_features(const char *features, bool strict)
 		return;
 	}
 
-	strncpy(f, features, sizeof(f));
+	strncpy(f, features, BTF_MAX_FEATURE_STR)[BTF_MAX_FEATURE_STR] = '\0';
 	s = f;
 	while ((feature_name = strtok_r(s, ",", &saveptr)) != NULL) {
 		struct btf_feature *feature = find_btf_feature(feature_name);