Skip to content

Commit

Permalink
xrEngine: Fix integer overflow of Pure classes
Browse files Browse the repository at this point in the history
Also use std::sort, which is faster than qsort.
  • Loading branch information
Zegeri authored and eagleivg committed Nov 6, 2018
1 parent 31bb00f commit cd62f9c
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/xrEngine/pure.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ DECLARE_MESSAGE(ScreenResolutionChanged);
struct MessageObject
{
IPure* Object;
int Prio;
unsigned long Prio;
};

template<class T>
Expand All @@ -44,19 +44,12 @@ class MessageRegistry
bool changed, inProcess;
xr_vector<MessageObject> messages;

static int __cdecl compare(const void* e1, const void* e2)
{
MessageObject* p1 = (MessageObject*)e1;
MessageObject* p2 = (MessageObject*)e2;
return p2->Prio - p1->Prio;
}

public:
MessageRegistry() : changed(false), inProcess(false) {}

void Clear() { messages.clear(); }

constexpr void Add(T* object, const int priority = REG_PRIORITY_NORMAL)
constexpr void Add(T* object, unsigned long priority = REG_PRIORITY_NORMAL)
{
Add({ object, priority });
}
Expand Down Expand Up @@ -117,8 +110,10 @@ class MessageRegistry

void Resort()
{
if (!messages.empty())
qsort(&messages.front(), messages.size(), sizeof(MessageObject), compare);
if (!messages.empty()) {
std::sort(std::begin(messages), std::end(messages),
[](const auto& a, const auto& b) { return a.Prio < b.Prio; });
}

while (!messages.empty() && messages.back().Prio == REG_PRIORITY_INVALID)
messages.pop_back();
Expand Down

0 comments on commit cd62f9c

Please sign in to comment.