perf ftrace: Fix undefined behavior in cmp_profile_data()
Subsystems (2)
PATCH 1/1 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.
--- tools/perf/builtin-ftrace.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +2 -1
@@ -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)
Signature · 1 line
2.34.1
Hello,
Quoted 12 lines · Kuan-Wei Chiu
The comparison function
cmp_profile_data()violates the C standard's requirements forqsort()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].
Quoted 8 lines
Fix the issue by returning 0 when v1 and v2 are equal, ensuring compliance with the C standard and preventing undefined behavior.
Fixes 0f223813edd0 ("perf ftrace: Add 'profile' command")Fixes 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")Signed-off-by Kuan-Wei Chiu <[email protected]>
Thanks for the fix.
Namhyung
Quoted 22 lines
--- tools/perf/builtin-ftrace.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tools/perf/builtin-ftrace.c +2
-2@@ -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) --2.34.1
Quoted 18 lines · Namhyung Kim
Hello,
Quoted 15 lines · Kuan-Wei Chiu
The comparison function
cmp_profile_data()violates the C standard's requirements forqsort()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.
Quoted 7 lines
Quoted 5 lines
Fixes 0f223813edd0 ("perf ftrace: Add 'profile' command")Fixes 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")Signed-off-by Kuan-Wei Chiu <[email protected]>Reviewed-by Namhyung Kim <[email protected]>
I'm assuming you'll pick this for perf-tools, ok?
- Arnaldo
Namhyung Kim
maintainerQuoted 26 lines · Arnaldo Carvalho de Melo
Quoted 24 lines · Namhyung Kim
Hello,
Quoted 20 lines · Kuan-Wei Chiu
The comparison function
cmp_profile_data()violates the C standard's requirements forqsort()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.
Fixes 0f223813edd0 ("perf ftrace: Add 'profile' command")Fixes 74ae366c37b7 ("perf ftrace profile: Add -s/--sort option")Signed-off-by Kuan-Wei Chiu <[email protected]>
Quoted 5 lines
Quoted 1 line
Reviewed-by Namhyung Kim <[email protected]>I'm assuming you'll pick this for perf-tools, ok?
Reviewed-by Arnaldo Carvalho de Melo <[email protected]>
Yep, sure.
Thanks,
Namhyung
Quoted 8 lines · Kuan-Wei Chiu
The comparison function
cmp_profile_data()violates the C standard's requirements forqsort()comparison functions, which mandate symmetry and transitivity:* Symmetry: If x < y, then y > x.
* Transitivity: If x < y and y < z, then x < z.[...]
Applied to perf-tools, thanks!
Best regards,
Namhyung