Skip to content

Commit 246dfe3

Browse files
visitorckwnamhyung
authored andcommitted
perf ftrace: Fix undefined behavior in cmp_profile_data()
The comparison function cmp_profile_data() violates the C standard's requirements for qsort() comparison functions, which mandate symmetry and transitivity: * Symmetry: If x < y, then y > x. * Transitivity: If x < y and y < z, then x < z. When v1 and v2 are equal, the function incorrectly returns 1, breaking symmetry and transitivity. This causes undefined behavior, which can lead to memory corruption in certain versions of glibc [1]. Fix the issue by returning 0 when v1 and v2 are equal, ensuring compliance with the C standard and preventing undefined behavior. Link: https://www.qualys.com/2024/01/30/qsort.txt [1] Fixes: 0f22381 ("perf ftrace: Add 'profile' command") Fixes: 74ae366 ("perf ftrace profile: Add -s/--sort option") Cc: [email protected] Signed-off-by: Kuan-Wei Chiu <[email protected]> Reviewed-by: Namhyung Kim <[email protected]> Reviewed-by: Arnaldo Carvalho de Melo <[email protected]> Cc: [email protected] Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent c33aea4 commit 246dfe3

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

tools/perf/builtin-ftrace.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,9 @@ static int cmp_profile_data(const void *a, const void *b)
11511151

11521152
if (v1 > v2)
11531153
return -1;
1154-
else
1154+
if (v1 < v2)
11551155
return 1;
1156+
return 0;
11561157
}
11571158

11581159
static void print_profile_result(struct perf_ftrace *ftrace)

0 commit comments

Comments
 (0)