]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#53,!125] Ensure that the MySQL buffer size is greater than 0.
authorMarcin Siodelski <marcin@isc.org>
Fri, 16 Nov 2018 18:07:47 +0000 (19:07 +0100)
committerMarcin Siodelski <marcin@isc.org>
Wed, 21 Nov 2018 19:18:48 +0000 (14:18 -0500)
src/lib/mysql/mysql_binding.cc
src/lib/mysql/mysql_binding.h

index 974bb3fa82fd2a754daefcb979584453d8f764e6..b268195935384bb92bf3486c191b05ea944e18c3 100644 (file)
@@ -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_;
 }
index 048d238d28a573ed28f887c175045358c2c4bdd3..62847f074f65eb23d347a98acd1b458d523dd5cf 100644 (file)
@@ -495,9 +495,19 @@ private:
     /// @param end Iterator pointing to the end of the assigned range.
     template<typename Iterator>
     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.