Fix reads into bad memory in readwritesplit

The candidate selection code used default values that would cause reads
past buffers. The code could also dereference the end iterator which
causes undefined behavior.
This commit is contained in:
Markus Mäkelä 2019-03-13 19:35:32 +02:00
parent b537176248
commit e5edb5c78f
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19

View File

@ -222,7 +222,7 @@ SRWBackendVector::iterator find_best_backend(SRWBackendVector& backends,
bool masters_accepts_reads)
{
// Group backends by priority. The set of highest priority backends will then compete.
int best_priority {INT_MAX}; // low numbers are high priority
int best_priority {2}; // low numbers are high priority
for (auto& psBackend : backends)
{
@ -252,7 +252,12 @@ SRWBackendVector::iterator find_best_backend(SRWBackendVector& backends,
}
auto best = select(priority_map[best_priority]);
auto rval = std::find(backends.begin(), backends.end(), *best);
auto rval = backends.end();
if (best != priority_map[best_priority].end())
{
rval = std::find(backends.begin(), backends.end(), *best);
}
for (auto& a : priority_map)
{