Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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:
http://schneide.wordpress.com/2010/11/01/bug-hunting-fun-with-stdsort/
  • Loading branch information...
commit 238c86786f993acabfaae8681254b907560f1122 1 parent 9d8dfd5
@popcornmix popcornmix authored
Showing with 3 additions and 1 deletion.
  1. +3 −1 xbmc/windowing/WinSystem.cpp
View
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)
Please sign in to comment.
Something went wrong with that request. Please try again.