-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add bounds checks in SDL_qsort #10066
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
base: main
Are you sure you want to change the base?
Conversation
|
These checks are insufficient. Feel free to use the commit in this pr to verify the fixes. |
|
@madebr You're accessing a zero-sized allocated block at line 61 when |
It's not valid, but testing it to make sure it at least doesn't crash makes sense (that's what these changes are about, after all). (No idea what exactly goes wrong in the qsort test, unless I missed something the log only mentions an incorrect exit code?) |
Whoops! Good catch. if (arraylen > 0) {
prev = nums[0];
} |
That's what I meant to say, we shouldn't test the order, just the function.
Because of a bug in the test itself, there's a segfault.
Test runs just fine with your fix (there was a note about a non-existent bug I found, apologies). |
3422807 to
83dee6e
Compare
|
I refactored the test, but honestly I think it does not look reasonable. The qsort used is in fact 3 qsorts for different cases. To test them all requires a lot of hackery (ignore failing build I can't figure out right pointer type for const array pointer). |
83dee6e to
b67d9c5
Compare
|
What's the status of this PR? Is it something we still need? |
|
@slouken |
I would tend to agree, but before we do that, we should test performance in release mode on a modern processor to see if we get significant speedup from those modes. |
|
Ok, I will set up a benchmark for this |
|
I benchmarked it and here are the results: CPU: 12th Gen Intel(R) Core(TM) i7-12700H Well, it makes things harder, I guess. I only compared these 2 pairs because Benchmark code bench.c.tar.gz |
|
So it sounds like it's worthwhile keeping all 3 cases. Thanks for the investigation! |
|
Hey @aikawayataro ! In #14344, I've refactored the tests a bit such that it tests
I've also split up the test in various unit tests using macros. So far so good, In this branch, I've implemented unit tests for "bad" compare callbacks that are non-transitive and random, which fail (=abort) with the current SDL_qsort implementation. Would you be willing to have another go at this? Note: cc ../test/testqsort.c -I../include libSDL3_test.a libSDL3.so -lunwind -Wl,-rpath,$PWD -otestqsort_stdlib -DTEST_STDLIB_QSORT
cc ../test/testqsort.c -I../include libSDL3_test.a libSDL3.so -lunwind -Wl,-rpath,$PWD -otestqsort_sdl
|
|
Hello! Yes, I'm still interested in this. I skimmed through 14344 and it does look good, all three qsort branches would be tested. |
b67d9c5 to
211432e
Compare
|
@madebr I checked |
|
When your compare function is not total I expect at least the following:
|
I would not necessarily expect these things. We should compare them to glibc and msvc qsort functions to determine expected behavior. |
So random sort and non-total ordering are unspecified. I tested with the libc of glibc and wine (with |
Description
Updating qsort implementation fixed only part of the non-transitive compare issue.
Using such a compare function should be considered a user code bug, but I believe it's better not to crash the whole program.
I set up a fuzzer and found a few unchecked memory reads and writes. With the proposed changes, qsort will not crash with invalid compare functions.
Fuzzer source code: fuzzer.zip