From 01e1c616ba0b4de1533a193639f36b3e83d76efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 27 Aug 2018 12:11:42 +0300 Subject: [PATCH] Fix CDC error detection The error detection assumed the buffer was null-terminated which was never guaranteed. --- connectors/cdc-connector/cdc_connector.cpp | 19 ++++++++++--------- connectors/cdc-connector/cdc_connector.h | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/connectors/cdc-connector/cdc_connector.cpp b/connectors/cdc-connector/cdc_connector.cpp index 69ca1de4d..ca2ac80a8 100644 --- a/connectors/cdc-connector/cdc_connector.cpp +++ b/connectors/cdc-connector/cdc_connector.cpp @@ -523,14 +523,14 @@ bool Connection::do_registration() return rval; } -bool Connection::is_error(const char* str) +bool Connection::is_error() { bool rval = false; - if (str[0] == 'E' && str[1] == 'R' && str[2] == 'R') + if (m_buffer.size() >= 3 && m_buffer[0] == 'E' && m_buffer[1] == 'R' && m_buffer[2] == 'R') { m_error = "MaxScale responded with an error: "; - m_error += str; + m_error.append(m_buffer.begin(), m_buffer.end()); rval = true; } @@ -545,6 +545,12 @@ bool Connection::read_row(std::string& dest) { if (!m_buffer.empty()) { + if (is_error()) + { + rval = false; + break; + } + std::deque::iterator it = std::find(m_buffer.begin(), m_buffer.end(), '\n'); if (it != m_buffer.end()) @@ -577,18 +583,13 @@ bool Connection::read_row(std::string& dest) assert(std::find(m_buffer.begin(), m_buffer.end(), '\n') == m_buffer.end()); std::copy(buf, buf + rc, std::back_inserter(m_buffer)); - if (is_error(&m_buffer[0])) + if (is_error()) { rval = false; break; } } - if (is_error(dest.c_str())) - { - rval = false; - } - return rval; } diff --git a/connectors/cdc-connector/cdc_connector.h b/connectors/cdc-connector/cdc_connector.h index e91c371f1..c5787d165 100644 --- a/connectors/cdc-connector/cdc_connector.h +++ b/connectors/cdc-connector/cdc_connector.h @@ -147,7 +147,7 @@ private: bool read_row(std::string& dest); void process_schema(json_t* json); SRow process_row(json_t*); - bool is_error(const char* str); + bool is_error(); // Lower-level functions int wait_for_event(short events);