From e5edb5c78f7c1c3ae30bb984b30f33903d7ae703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 13 Mar 2019 19:35:32 +0200 Subject: [PATCH] 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. --- .../routing/readwritesplit/rwsplit_select_backends.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/server/modules/routing/readwritesplit/rwsplit_select_backends.cc b/server/modules/routing/readwritesplit/rwsplit_select_backends.cc index dd9591550..80d26e096 100644 --- a/server/modules/routing/readwritesplit/rwsplit_select_backends.cc +++ b/server/modules/routing/readwritesplit/rwsplit_select_backends.cc @@ -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) {