]> www.infradead.org Git - users/hch/misc.git/commitdiff
perf build-id: Ensure snprintf string is empty when size is 0
authorIan Rogers <irogers@google.com>
Thu, 18 Sep 2025 17:24:16 +0000 (10:24 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 19 Sep 2025 19:53:09 +0000 (16:53 -0300)
The string result of build_id__snprintf() is unconditionally used in
places like dsos__fprintf_buildid_cb(). If the build id has size 0 then
this creates a use of uninitialized memory. Add null termination for the
size 0 case.

A similar fix was written by Jiri Olsa in commit 6311951d4f8f28c4 ("perf
tools: Initialize output buffer in build_id__sprintf") but lost in the
transition to snprintf.

Fixes: fccaaf6fbbc59910 ("perf build-id: Change sprintf functions to snprintf")
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/build-id.c

index bf7f3268b9a2f32be1e0e8098da3158d33ce4de0..35505a1ffd11173a3877b44d6f7a876349313cf3 100644 (file)
@@ -86,6 +86,13 @@ int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_size
 {
        size_t offs = 0;
 
+       if (build_id->size == 0) {
+               /* Ensure bf is always \0 terminated. */
+               if (bf_size > 0)
+                       bf[0] = '\0';
+               return 0;
+       }
+
        for (size_t i = 0; i < build_id->size && offs < bf_size; ++i)
                offs += snprintf(bf + offs, bf_size - offs, "%02x", build_id->data[i]);