-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add bounds checks in SDL_qsort #10066
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! |
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