Skip to contentSkip to conversation

perf build-id: Fix off-by-one bug when printing kernel/module build-id

PATCH Applied Michael Petlan 4 participants 4 messages May 19 – May 20 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf build-id: Fix off-by-one bug when printing kernel/module build-id

When changing sprintf functions to snprintf, one byte got lost. Since snprintf ones do not handle the '\0' terminating character, the number of printed characters is 40, while sizeof(sbuild_id) is 41, including the terminating '\0' character. This makes the later check fail so that nothing is printed.

Fix that.

Before:
[Michael@Carbon ~]$ perf buildid-list -k
[Michael@Carbon ~]$

After:

    [Michael@Carbon ~]$ perf buildid-list -k
    a527806324d543c4bc3ff2f9c9519d494fed5f68
    [Michael@Carbon ~]$
Fixes fccaaf6fbbc5 ("perf build-id: Change sprintf functions to snprintf")
---
 tools/perf/builtin-buildid-list.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-buildid-list.c +2 -2
index a91bbb34ac94..e0881b0ac38f 100644
@@ -61,7 +61,7 @@ static int sysfs__fprintf_build_id(FILE *fp)
 	int ret;
 
 	ret = sysfs__snprintf_build_id("/", sbuild_id, sizeof(sbuild_id));
-	if (ret != sizeof(sbuild_id))
+	if (ret + 1 != sizeof(sbuild_id))
 		return ret < 0 ? ret : -EINVAL;
 
 	return fprintf(fp, "%s\n", sbuild_id);
@@ -73,7 +73,7 @@ static int filename__fprintf_build_id(const char *name, FILE *fp)
 	int ret;
 
 	ret = filename__snprintf_build_id(name, sbuild_id, sizeof(sbuild_id));
-	if (ret != sizeof(sbuild_id))
+	if (ret + 1 != sizeof(sbuild_id))
 		return ret < 0 ? ret : -EINVAL;
 
 	return fprintf(fp, "%s\n", sbuild_id);
Signature · 1 line
2.47.3
reviewer Tested-by on v1 1/1
#1 lore raw
Quoted 13 lines · Michael Petlan

When changing sprintf functions to snprintf, one byte got lost. Since snprintf ones do not handle the '\0' terminating character, the number of printed characters is 40, while sizeof(sbuild_id) is 41, including the terminating '\0' character. This makes the later check fail so that nothing is printed.

Fix that.

Before:
[Michael@Carbon ~]$ perf buildid-list -k
[Michael@Carbon ~]$

Quoted 8 lines

After:

    [Michael@Carbon ~]$ perf buildid-list -k
    a527806324d543c4bc3ff2f9c9519d494fed5f68
    [Michael@Carbon ~]$
Fixes fccaaf6fbbc5 ("perf build-id: Change sprintf functions to snprintf")

also Sashiko is green for this patch:
https://sashiko.dev/#/patchset/20260519223855.191637-1-mpetlan%40redhat.com

Thanks!
Ian

Quoted 29 lines
---
 tools/perf/builtin-buildid-list.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-buildid-list.c +2 -3
index a91bbb34ac94..e0881b0ac38f 100644
@@ -61,7 +61,7 @@ static int sysfs__fprintf_build_id(FILE *fp)
        int ret;
 
        ret = sysfs__snprintf_build_id("/", sbuild_id, sizeof(sbuild_id));
-       if (ret != sizeof(sbuild_id))
+       if (ret + 1 != sizeof(sbuild_id))
                return ret < 0 ? ret : -EINVAL;
 
        return fprintf(fp, "%s\n", sbuild_id);
@@ -73,7 +73,7 @@ static int filename__fprintf_build_id(const char *name, FILE *fp)
        int ret;
 
        ret = filename__snprintf_build_id(name, sbuild_id, sizeof(sbuild_id));
-       if (ret != sizeof(sbuild_id))
+       if (ret + 1 != sizeof(sbuild_id))
                return ret < 0 ? ret : -EINVAL;
 
        return fprintf(fp, "%s\n", sbuild_id);
--

2.47.3

maintainer Applied
#2 lore raw
Quoted 19 lines · Ian Rogers
Quoted 18 lines · Michael Petlan

When changing sprintf functions to snprintf, one byte got lost. Since snprintf ones do not handle the '\0' terminating character, the number of printed characters is 40, while sizeof(sbuild_id) is 41, including the terminating '\0' character. This makes the later check fail so that nothing is printed.

Fix that.

Before:
[Michael@Carbon ~]$ perf buildid-list -k
[Michael@Carbon ~]$

After:

    [Michael@Carbon ~]$ perf buildid-list -k
    a527806324d543c4bc3ff2f9c9519d494fed5f68
    [Michael@Carbon ~]$
Quoted 7 lines
Quoted 3 lines
Fixes fccaaf6fbbc5 ("perf build-id: Change sprintf functions to snprintf")

also Sashiko is green for this patch:
https://sashiko.dev/#/patchset/20260519223855.191637-1-mpetlan%40redhat.com

Thanks, applied to perf-tools-next, for v7.2.

- Arnaldo

maintainer
#3 lore raw
Quoted 22 lines · Arnaldo Carvalho de Melo
Quoted 21 lines · Ian Rogers
Quoted 20 lines · Michael Petlan

When changing sprintf functions to snprintf, one byte got lost. Since snprintf ones do not handle the '\0' terminating character, the number of printed characters is 40, while sizeof(sbuild_id) is 41, including the terminating '\0' character. This makes the later check fail so that nothing is printed.

Fix that.

Before:
[Michael@Carbon ~]$ perf buildid-list -k
[Michael@Carbon ~]$

After:

    [Michael@Carbon ~]$ perf buildid-list -k
    a527806324d543c4bc3ff2f9c9519d494fed5f68
    [Michael@Carbon ~]$
Fixes fccaaf6fbbc5 ("perf build-id: Change sprintf functions to snprintf")
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

also Sashiko is green for this patch:
https://sashiko.dev/#/patchset/20260519223855.191637-1-mpetlan%40redhat.com

Thanks, applied to perf-tools-next, for v7.2.

I sent out this a month ago, but forgot to ping for inclusion.

https://lore.kernel.org/linux-perf-users/[email protected]/

Anyway, I'm ok with fixing it either way.

Thanks,
Namhyung