Skip to content
Browse files

[settings] Fix memory corruption caused by resolution sorting

This is possibly the most subtle bug I've seen.
What I've seen over last couple of weeks is random memory corruption
failures when browsing the settings/video output window.

I tracked it down to the sort function in CWinSystemBase::ScreenResolutions.
But why? Everything looked fine.

Eventually spotted it. The compare function doesn't obey strict weak ordering.
std::sort considers a reasonable response to that is to corrupt memory.

Here's a description from someone else who hit a similar bug:
  • Loading branch information...
1 parent 9d8dfd5 commit 238c86786f993acabfaae8681254b907560f1122 @popcornmix popcornmix committed
Showing with 3 additions and 1 deletion.
  1. +3 −1 xbmc/windowing/WinSystem.cpp
4 xbmc/windowing/WinSystem.cpp
@@ -149,9 +149,11 @@ static void AddResolution(vector<RESOLUTION_WHR> &resolutions, unsigned int addi
static bool resSortPredicate(RESOLUTION_WHR i, RESOLUTION_WHR j)
+ // note: this comparison must obey "strict weak ordering"
+ // a "!=" on the flags comparison resulted in memory corruption
return ( i.width < j.width
|| (i.width == j.width && i.height < j.height)
- || (i.width == j.width && i.height == j.height && i.flags != j.flags) );
+ || (i.width == j.width && i.height == j.height && i.flags < j.flags) );
vector<RESOLUTION_WHR> CWinSystemBase::ScreenResolutions(int screen, float refreshrate)

0 comments on commit 238c867

Please sign in to comment.
Something went wrong with that request. Please try again.