Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behavior in qsort comparison functions for rv_histogram #526

Merged
merged 1 commit into from
Dec 24, 2024

Conversation

visitorckw
Copy link
Collaborator

The freq variable is of type size_t, but the qsort comparison functions were directly returning a->freq - b->freq, which implicitly converts the result to an int. This conversion can cause overflow, leading to implementation-defined behavior.

When freq values are sufficiently large, this issue may violate the antisymmetric and transitive properties required for comparison functions:

Antisymmetry: If a < b, then b > a.
Transitivity: If a < b and b < c, then a < c.

Violating these properties results in undefined behavior in qsort, which could trigger memory corruption in some glibc implementations, posing a potential security risk. [1]

Rewrite the comparison functions (cmp_dec and cmp_asc) to compare size_t values explicitly, ensuring correctness and avoiding overflow.

Link: https://www.qualys.com/2024/01/30/qsort.txt [1]

The freq variable is of type size_t, but the qsort comparison functions
were directly returning a->freq - b->freq, which implicitly converts
the result to an int. This conversion can cause overflow, leading to
implementation-defined behavior.

When freq values are sufficiently large, this issue may violate the
antisymmetric and transitive properties required for comparison
functions:

Antisymmetry: If a < b, then b > a.
Transitivity: If a < b and b < c, then a < c.

Violating these properties results in undefined behavior in qsort,
which could trigger memory corruption in some glibc implementations,
posing a potential security risk. [1]

Rewrite the comparison functions (cmp_dec and cmp_asc) to compare
size_t values explicitly, ensuring correctness and avoiding overflow.

Link: https://www.qualys.com/2024/01/30/qsort.txt [1]
@jserv jserv merged commit 337f4ef into sysprog21:master Dec 24, 2024
8 checks passed
@visitorckw visitorckw deleted the fix-qsort-cmp branch December 24, 2024 16:34
@jserv jserv added this to the release-2025.1 milestone Jan 19, 2025
vestata pushed a commit to vestata/rv32emu that referenced this pull request Jan 24, 2025
Fix undefined behavior in qsort comparison functions for rv_histogram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants