From e5ae1bb46564689e56a38f3509daffa4aca3b29c Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Wed, 27 Jun 2018 12:09:50 -0700 Subject: [PATCH] Remove bogus gcc-8.1 warning (#3870) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Various rearrangements of the cch maths failed or replacing = '\0' with memset failed to convince the compiler it was nul terminated. So took the perverse option of changing strncpy to strcpy. Return null if memory couldn't be allocated. util/status.cc: In static member function ‘static const char* rocksdb::Status::CopyState(const char*)’: util/status.cc:28:15: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation] std::strncpy(result, state, cch - 1); ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ util/status.cc:19:18: note: length computed here std::strlen(state) + 1; // +1 for the null terminator ~~~~~~~~~~~^~~~~~~ cc1plus: all warnings being treated as errors make: *** [Makefile:645: shared-objects/util/status.o] Error 1 closes #2705 Closes https://github.com/facebook/rocksdb/pull/3870 Differential Revision: D8594114 Pulled By: anand1976 fbshipit-source-id: ab20f3a456a711e4d29144ebe630e4fe3c99ec25 --- include/rocksdb/status.h | 6 +++--- util/status.cc | 15 ++++----------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/include/rocksdb/status.h b/include/rocksdb/status.h index 3573d37e3..c803f89bb 100644 --- a/include/rocksdb/status.h +++ b/include/rocksdb/status.h @@ -26,7 +26,7 @@ class Status { public: // Create a success status. Status() : code_(kOk), subcode_(kNone), state_(nullptr) {} - ~Status() { delete[] state_; } + ~Status() { free((void *) state_); } // Copy the specified status. Status(const Status& s); @@ -284,7 +284,7 @@ inline Status& Status::operator=(const Status& s) { if (this != &s) { code_ = s.code_; subcode_ = s.subcode_; - delete[] state_; + free((void *) state_); state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_); } return *this; @@ -308,7 +308,7 @@ inline Status& Status::operator=(Status&& s) s.code_ = kOk; subcode_ = std::move(s.subcode_); s.subcode_ = kNone; - delete[] state_; + free((void *)state_); state_ = nullptr; std::swap(state_, s.state_); } diff --git a/util/status.cc b/util/status.cc index 319b0d9a4..f302d2ead 100644 --- a/util/status.cc +++ b/util/status.cc @@ -10,24 +10,17 @@ #include "rocksdb/status.h" #include #include +#include #include "port/port.h" namespace rocksdb { const char* Status::CopyState(const char* state) { - const size_t cch = - std::strlen(state) + 1; // +1 for the null terminator - char* const result = - new char[cch]; - result[cch - 1] = '\0'; #ifdef OS_WIN - errno_t ret; - ret = strncpy_s(result, cch, state, cch - 1); - assert(ret == 0); + return _strdup(state); #else - std::strncpy(result, state, cch - 1); + return strdup(state); #endif - return result; } Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2) @@ -37,7 +30,7 @@ Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2 const size_t len1 = msg.size(); const size_t len2 = msg2.size(); const size_t size = len1 + (len2 ? (2 + len2) : 0); - char* const result = new char[size + 1]; // +1 for null terminator + char* const result = (char*) malloc(size + 1); // +1 for null terminator memcpy(result, msg.data(), len1); if (len2) { result[len1] = ':';