Skip to content

Commit

Permalink
perf ftrace: Fix undefined behavior in cmp_profile_data()
Browse files Browse the repository at this point in the history
commit 246dfe3 upstream.

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: stable@vger.kernel.org
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: jserv@ccns.ncku.edu.tw
Cc: chuang@cs.nycu.edu.tw
Link: https://lore.kernel.org/r/20241209134226.1939163-1-visitorckw@gmail.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
visitorckw authored and gregkh committed Dec 19, 2024
1 parent 8aa9d1f commit 9891675
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tools/perf/builtin-ftrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,8 +1151,9 @@ static int cmp_profile_data(const void *a, const void *b)

if (v1 > v2)
return -1;
else
if (v1 < v2)
return 1;
return 0;
}

static void print_profile_result(struct perf_ftrace *ftrace)
Expand Down

0 comments on commit 9891675

Please sign in to comment.