From 59acef2d2b59a3f1a55337a933b97d901d46133a Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 6 Jan 2020 12:35:06 +0100 Subject: [PATCH] c++: Fix possible string truncation --- libtest/has.cc | 2 +- libtest/socket.cc | 2 +- tests/libmemcached-1.0/mem_functions.cc | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libtest/has.cc b/libtest/has.cc index 2d9abf6b..0983f070 100644 --- a/libtest/has.cc +++ b/libtest/has.cc @@ -165,7 +165,7 @@ static void initialize_memcached_binary_path() if (access(arg_buffer.str().c_str(), X_OK) == 0) { - strncpy(memcached_binary_path, arg_buffer.str().c_str(), FILENAME_MAX); + strncpy(memcached_binary_path, arg_buffer.str().c_str(), FILENAME_MAX-1); } } #endif diff --git a/libtest/socket.cc b/libtest/socket.cc index 76ac5bf1..9e06841a 100644 --- a/libtest/socket.cc +++ b/libtest/socket.cc @@ -55,7 +55,7 @@ void set_default_socket(const char *socket) { if (socket) { - strncpy(global_socket, socket, strlen(socket)); + strncpy(global_socket, socket, sizeof(global_socket)-1); } } diff --git a/tests/libmemcached-1.0/mem_functions.cc b/tests/libmemcached-1.0/mem_functions.cc index 0e6bcb7f..8b2dc606 100644 --- a/tests/libmemcached-1.0/mem_functions.cc +++ b/tests/libmemcached-1.0/mem_functions.cc @@ -4166,7 +4166,7 @@ test_return_t regression_bug_442914(memcached_st *original_memc) for (uint32_t x= 0; x < 250; ++x) { - char key[250]; + char key[251]; size_t len= (size_t)snprintf(key, sizeof(key), "%0250u", x); memcached_return_t rc= memcached_delete(memc, key, len, 0); char error_buffer[2048]= { 0 }; @@ -4176,7 +4176,7 @@ test_return_t regression_bug_442914(memcached_st *original_memc) // Delete, and then delete again to look for not found { - char key[250]; + char key[251]; size_t len= snprintf(key, sizeof(key), "%037u", 251U); memcached_return_t rc= memcached_delete(memc, key, len, 0); test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED); -- 2.30.2