Skip to content

Commit 4271ddb

Browse files
committed
Fix length tor strcpy_s
1 parent 4bad99b commit 4271ddb

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

source/SqliteUtils.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <unicode/unistr.h>
5151

5252
/* modern.cpp.core */
53+
#include <Logger.h>
5354
#include <StringUtils.h>
5455

5556
/* local header */
@@ -69,6 +70,9 @@ namespace vx::sqlite_utils {
6970

7071
constexpr double earthBlubKm = 6378.137;
7172

73+
/** @brief Buffer size for errno. */
74+
constexpr std::int32_t errnoBufferSize = 128;
75+
7276
void sqlite3_deleter::operator()( sqlite3 *_handle ) const noexcept {
7377

7478
[[maybe_unused]] const std::int32_t resultCode = sqlite3_close( _handle );
@@ -375,7 +379,25 @@ namespace vx::sqlite_utils {
375379

376380
auto *databuffer( static_cast<char *>( sqlite3_malloc64( sizeof( char ) * str.size() ) ) );
377381
#ifdef _WIN32
378-
std::strncpy_s( databuffer, str.size(), str.data(), str.size() );
382+
errno_t error = std::strncpy_s( databuffer, str.size() + 1, str.data(), str.size() );
383+
if ( error ) {
384+
385+
std::vector<char> errnoBuffer {};
386+
try {
387+
388+
errnoBuffer.resize( errnoBufferSize );
389+
}
390+
catch ( const std::bad_alloc &_exception ) {
391+
392+
logFatal() << "bad_alloc:" << _exception.what();
393+
}
394+
catch ( const std::exception &_exception ) {
395+
396+
logFatal() << _exception.what();
397+
}
398+
std::ignore = strerror_s( errnoBuffer.data(), errnoBuffer.size(), error );
399+
logError() << std::string( std::cbegin( errnoBuffer ), std::cend( errnoBuffer ) );
400+
}
379401
#else
380402
std::strncpy( databuffer, str.data(), str.size() );
381403
#endif

0 commit comments

Comments
 (0)