Skip to content

Commit 62892e7

Browse files
visitorckwnamhyung
authored andcommitted
perf bench: Fix undefined behavior in cmpworker()
The comparison function cmpworker() 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. In its current implementation, cmpworker() incorrectly returns 0 when w1->tid < w2->tid, which breaks both symmetry and transitivity. This violation causes undefined behavior, potentially leading to issues such as memory corruption in glibc [1]. Fix the issue by returning -1 when w1->tid < w2->tid, ensuring compliance with the C standard and preventing undefined behavior. Link: https://www.qualys.com/2024/01/30/qsort.txt [1] Fixes: 121dd9e ("perf bench: Add epoll parallel epoll_wait benchmark") Cc: stable@vger.kernel.org Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Reviewed-by: James Clark <james.clark@linaro.org> Link: https://lore.kernel.org/r/20250116110842.4087530-1-visitorckw@gmail.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 035f0c2 commit 62892e7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

tools/perf/bench/epoll-wait.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,12 @@ static int cmpworker(const void *p1, const void *p2)
420420

421421
struct worker *w1 = (struct worker *) p1;
422422
struct worker *w2 = (struct worker *) p2;
423-
return w1->tid > w2->tid;
423+
424+
if (w1->tid > w2->tid)
425+
return 1;
426+
if (w1->tid < w2->tid)
427+
return -1;
428+
return 0;
424429
}
425430

426431
int bench_epoll_wait(int argc, const char **argv)

0 commit comments

Comments
 (0)