From 47a8b335644affc175934d56a8edc6f689e15581 Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Fri, 16 Nov 2018 19:07:47 +0100 Subject: [PATCH] [#53,!125] Ensure that the MySQL buffer size is greater than 0. --- src/lib/mysql/mysql_binding.cc | 8 +++++++- src/lib/mysql/mysql_binding.h | 12 +++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/mysql/mysql_binding.cc b/src/lib/mysql/mysql_binding.cc index 974bb3fa82..b268195935 100644 --- a/src/lib/mysql/mysql_binding.cc +++ b/src/lib/mysql/mysql_binding.cc @@ -232,7 +232,13 @@ MySqlBinding::MySqlBinding(enum_field_types buffer_type, void MySqlBinding::setBufferLength(const unsigned long length) { length_ = length; - buffer_.resize(length_); + // It appears that the MySQL connectors sometimes require that the + // buffer is specified (set to a non-zero value), even if the buffer + // length is 0. We have found that setting the buffer to 0 value would + // cause the value inserted to the database be NULL. In order to avoid + // it, we simply make sure that the buffer length is at least 1 byte and + // provide the pointer to this byte within the binding. + buffer_.resize(length_ > 0 ? length_ : 1); bind_.buffer = &buffer_[0]; bind_.buffer_length = length_; } diff --git a/src/lib/mysql/mysql_binding.h b/src/lib/mysql/mysql_binding.h index 048d238d28..62847f074f 100644 --- a/src/lib/mysql/mysql_binding.h +++ b/src/lib/mysql/mysql_binding.h @@ -495,9 +495,19 @@ private: /// @param end Iterator pointing to the end of the assigned range. template void setBufferValue(Iterator begin, Iterator end) { + length_ = std::distance(begin, end); buffer_.assign(begin, end); + // It appears that the MySQL connectors sometimes require that the + // buffer is specified (set to a non-zero value), even if the buffer + // length is 0. We have found that setting the buffer to 0 value would + // cause the value inserted to the database be NULL. In order to avoid + // it, we simply make sure that the buffer length is at least 1 byte and + // provide the pointer to this byte within the binding. + if (buffer_.empty()) { + buffer_.resize(1); + } bind_.buffer = &buffer_[0]; - bind_.buffer_length = std::distance(begin, end); + bind_.buffer_length = length_; } /// @brief Resizes the buffer and assigns new length to the binding. -- 2.47.2