Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf string: Avoid undefined NULL+1

While the value NULL+1 is never used it triggers a ubsan warning.
Restructure and comment the loop to avoid this.

Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20241120065224.286813-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
61e0a944 72698466

+12 -3
+12 -3
tools/perf/util/string.c
··· 254 254 255 255 do { 256 256 ptr = strpbrk(str, stopset); 257 - if (ptr == str || 258 - (ptr == str + 1 && *(ptr - 1) != '\\')) 257 + if (!ptr) { 258 + /* stopset not in str. */ 259 259 break; 260 + } 261 + if (ptr == str) { 262 + /* stopset character is first in str. */ 263 + break; 264 + } 265 + if (ptr == str + 1 && str[0] != '\\') { 266 + /* stopset chacter is second and wasn't preceded by a '\'. */ 267 + break; 268 + } 260 269 str = ptr + 1; 261 - } while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\'); 270 + } while (ptr[-1] == '\\' && ptr[-2] != '\\'); 262 271 263 272 return ptr; 264 273 }