Remove bogus gcc-8.1 warning (#3870)

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
main
Daniel Black 7 years ago committed by Facebook Github Bot
parent a16e00b7b9
commit e5ae1bb465
  1. 6
      include/rocksdb/status.h
  2. 15
      util/status.cc

@ -26,7 +26,7 @@ class Status {
public: public:
// Create a success status. // Create a success status.
Status() : code_(kOk), subcode_(kNone), state_(nullptr) {} Status() : code_(kOk), subcode_(kNone), state_(nullptr) {}
~Status() { delete[] state_; } ~Status() { free((void *) state_); }
// Copy the specified status. // Copy the specified status.
Status(const Status& s); Status(const Status& s);
@ -284,7 +284,7 @@ inline Status& Status::operator=(const Status& s) {
if (this != &s) { if (this != &s) {
code_ = s.code_; code_ = s.code_;
subcode_ = s.subcode_; subcode_ = s.subcode_;
delete[] state_; free((void *) state_);
state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_); state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_);
} }
return *this; return *this;
@ -308,7 +308,7 @@ inline Status& Status::operator=(Status&& s)
s.code_ = kOk; s.code_ = kOk;
subcode_ = std::move(s.subcode_); subcode_ = std::move(s.subcode_);
s.subcode_ = kNone; s.subcode_ = kNone;
delete[] state_; free((void *)state_);
state_ = nullptr; state_ = nullptr;
std::swap(state_, s.state_); std::swap(state_, s.state_);
} }

@ -10,24 +10,17 @@
#include "rocksdb/status.h" #include "rocksdb/status.h"
#include <stdio.h> #include <stdio.h>
#include <cstring> #include <cstring>
#include <string.h>
#include "port/port.h" #include "port/port.h"
namespace rocksdb { namespace rocksdb {
const char* Status::CopyState(const char* state) { 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 #ifdef OS_WIN
errno_t ret; return _strdup(state);
ret = strncpy_s(result, cch, state, cch - 1);
assert(ret == 0);
#else #else
std::strncpy(result, state, cch - 1); return strdup(state);
#endif #endif
return result;
} }
Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2) 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 len1 = msg.size();
const size_t len2 = msg2.size(); const size_t len2 = msg2.size();
const size_t size = len1 + (len2 ? (2 + len2) : 0); 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); memcpy(result, msg.data(), len1);
if (len2) { if (len2) {
result[len1] = ':'; result[len1] = ':';

Loading…
Cancel
Save