Re: [PATCH v3] perf bench: Fix undefined behavior in cmpworker()

From: James Clark
Date: Thu Jan 16 2025 - 06:37:16 EST




On 16/01/2025 11:08 am, Kuan-Wei Chiu wrote:
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: 121dd9ea0116 ("perf bench: Add epoll parallel epoll_wait benchmark")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kuan-Wei Chiu <visitorckw@xxxxxxxxx>
---
Changes in v3:
- Perform a full comparison for clarity, as suggested by James.


Reviewed-by: James Clark <james.clark@xxxxxxxxxx>