[PATCH] perf script: Fix unsigned comparison against zero in print_bstack_flags()

From: liujing

Date: Thu Sep 03 2026 - 04:55:34 EST


From: Liu Jing <liujing@xxxxxxxxxxxxxxxxxxxx>

In print_bstack_flags(), the variable `pos` is declared as `size_t`
(unsigned), making the check `pos < 0` always false. This means the
error return from snprintf() is never detected.

Fix it by changing the return type of bstack_event_str() to int and
the type of pos to int, so that negative return values from snprintf()
are properly checked.

Signed-off-by: Liu Jing <liujing@xxxxxxxxxxxxxxxxxxxx>
---
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -952,7 +952,7 @@
return printed;
}

-static inline size_t
+static inline int
bstack_event_str(struct branch_entry *br, char *buf, size_t sz)
{
if (!(br->flags.mispred || br->flags.predicted || br->flags.not_taken))
@@ -966,7 +966,7 @@
static int print_bstack_flags(FILE *fp, struct branch_entry *br)
{
char events[16] = { 0 };
- size_t pos;
+ int pos;

pos = bstack_event_str(br, events, sizeof(events));
return fprintf(fp, "/%s/%c/%c/%d/%s/%s ",