From 5ec382b918f41481c089a45fb3d8472c017589fc Mon Sep 17 00:00:00 2001 From: Dmitri Smirnov Date: Mon, 9 Apr 2018 19:21:46 -0700 Subject: [PATCH] Fix up backupable_db stack corruption. Summary: Fix up OACR(Lint) warnings. Closes https://github.com/facebook/rocksdb/pull/3674 Differential Revision: D7563869 Pulled By: ajkr fbshipit-source-id: 8c1e5045c8a6a2d85b2933fdbc60fde93bf0c9de --- third-party/gtest-1.7.0/fused-src/gtest/gtest.h | 4 ---- util/status.cc | 12 ++++++++++-- utilities/backupable/backupable_db.cc | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/third-party/gtest-1.7.0/fused-src/gtest/gtest.h b/third-party/gtest-1.7.0/fused-src/gtest/gtest.h index e3f0cfb95..3cec41a9e 100644 --- a/third-party/gtest-1.7.0/fused-src/gtest/gtest.h +++ b/third-party/gtest-1.7.0/fused-src/gtest/gtest.h @@ -3410,10 +3410,6 @@ inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 /* deprecated function */) -inline const char* StrNCpy(char* dest, const char* src, size_t n) { - return strncpy(dest, src, n); -} - // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and // StrError() aren't needed on Windows CE at this time and thus not // defined there. diff --git a/util/status.cc b/util/status.cc index e0c1af99e..1881c58de 100644 --- a/util/status.cc +++ b/util/status.cc @@ -15,9 +15,17 @@ 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[std::strlen(state) + 1]; // +1 for the null terminator - std::strcpy(result, state); + new char[cch]; +#ifdef OS_WIN + errno_t ret; + ret = strncpy_s(result, cch, state, cch - 1); + assert(ret == 0); +#else + std::strncpy(result, state, cch - 1); +#endif return result; } diff --git a/utilities/backupable/backupable_db.cc b/utilities/backupable/backupable_db.cc index d0602c1cb..98e8a4781 100644 --- a/utilities/backupable/backupable_db.cc +++ b/utilities/backupable/backupable_db.cc @@ -1770,7 +1770,8 @@ Status BackupEngineImpl::BackupMeta::StoreToFile(bool sync) { } char writelen_temp[19]; - if (len + sprintf(writelen_temp, "%" ROCKSDB_PRIszt "\n", files_.size()) >= buf_size) { + if (len + snprintf(writelen_temp, sizeof(writelen_temp), + "%" ROCKSDB_PRIszt "\n", files_.size()) >= buf_size) { backup_meta_file->Append(Slice(buf.get(), len)); buf.reset(); unique_ptr new_reset_buf(new char[max_backup_meta_file_size_]); @@ -1785,7 +1786,8 @@ Status BackupEngineImpl::BackupMeta::StoreToFile(bool sync) { for (const auto& file : files_) { // use crc32 for now, switch to something else if needed - size_t newlen = len + file->filename.length() + sprintf(writelen_temp, " crc32 %u\n", file->checksum_value); + size_t newlen = len + file->filename.length() + snprintf(writelen_temp, + sizeof(writelen_temp), " crc32 %u\n", file->checksum_value); const char *const_write = writelen_temp; if (newlen >= buf_size) { backup_meta_file->Append(Slice(buf.get(), len));