From a83eb6704af40530cf38935cb30dd576af563e61 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 6 Jan 2020 12:31:51 +0100 Subject: [PATCH] libmemcached-1.0: add memcached_return_t::MEMCACHED_UNIX_SOCKET_PATH_TOO_BIG detect a possible string truncation and return an appropriate error --- docs/source/libmemcached/memcached_return_t.rst | 3 ++- libmemcached-1.0/types/return.h | 1 + libmemcached/connect.cc | 5 ++++- libmemcached/strerror.cc | 3 +++ tests/libmemcached-1.0/mem_functions.cc | 4 ++-- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/source/libmemcached/memcached_return_t.rst b/docs/source/libmemcached/memcached_return_t.rst index 9eba3fab..0cf7d762 100644 --- a/docs/source/libmemcached/memcached_return_t.rst +++ b/docs/source/libmemcached/memcached_return_t.rst @@ -123,10 +123,11 @@ Libmemcached return types: :c:type:`MEMCACHED_PARSE_USER_ERROR` :c:type:`MEMCACHED_DEPRECATED` + +:c:type:`MEMCACHED_UNIX_SOCKET_PATH_TOO_BIG` -------- SEE ALSO -------- :manpage:`memcached` :manpage:`libmemcached` :manpage:`memcached_client_error` or :manpage:`memcached_worker_error` - diff --git a/libmemcached-1.0/types/return.h b/libmemcached-1.0/types/return.h index f4ec9b08..80c90515 100644 --- a/libmemcached-1.0/types/return.h +++ b/libmemcached-1.0/types/return.h @@ -86,6 +86,7 @@ enum memcached_return_t { MEMCACHED_IN_PROGRESS, MEMCACHED_SERVER_TEMPORARILY_DISABLED, MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE, + MEMCACHED_UNIX_SOCKET_PATH_TOO_BIG, MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */ MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE= MEMCACHED_ERROR }; diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 006810bf..6d900377 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -466,7 +466,10 @@ static memcached_return_t unix_socket_connect(memcached_instance_st* server) memset(&servAddr, 0, sizeof (struct sockaddr_un)); servAddr.sun_family= AF_UNIX; - strncpy(servAddr.sun_path, server->hostname(), sizeof(servAddr.sun_path)); /* Copy filename */ + if (strlen(server->hostname()) >= sizeof(servAddr.sun_path)) { + return memcached_set_error(*server, MEMCACHED_UNIX_SOCKET_PATH_TOO_BIG, MEMCACHED_AT); + } + strncpy(servAddr.sun_path, server->hostname(), sizeof(servAddr.sun_path)-1); /* Copy filename */ if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1) { diff --git a/libmemcached/strerror.cc b/libmemcached/strerror.cc index 7a4e0a82..c5615ca9 100644 --- a/libmemcached/strerror.cc +++ b/libmemcached/strerror.cc @@ -188,6 +188,9 @@ const char *memcached_strerror(const memcached_st *, memcached_return_t rc) case MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE: return "SERVER FAILED TO ALLOCATE OBJECT"; + case MEMCACHED_UNIX_SOCKET_PATH_TOO_BIG: + return "UNIX SOCKET PATH TOO LARGE"; + default: case MEMCACHED_MAXIMUM_RETURN: return "INVALID memcached_return_t"; diff --git a/tests/libmemcached-1.0/mem_functions.cc b/tests/libmemcached-1.0/mem_functions.cc index 321a35db..0e6bcb7f 100644 --- a/tests/libmemcached-1.0/mem_functions.cc +++ b/tests/libmemcached-1.0/mem_functions.cc @@ -496,7 +496,7 @@ test_return_t memcached_return_t_TEST(memcached_st *memc) 4159057246U, 3425930182U, 2593724503U, 1868899624U, 1769812374U, 2302537950U, 1110330676U, 3365377466U, 1336171666U, 3021258493U, 2334992265U, 3861994737U, - 3582734124U, 3365377466U }; + 3582734124U, 3889811103, 3365377466U }; // You have updated the memcache_error messages but not updated docs/tests. for (int rc= int(MEMCACHED_SUCCESS); rc < int(MEMCACHED_MAXIMUM_RETURN); ++rc) @@ -512,7 +512,7 @@ test_return_t memcached_return_t_TEST(memcached_st *memc) } test_compare(values[rc], hash_val); } - test_compare(49, int(MEMCACHED_MAXIMUM_RETURN)); + test_compare(50, int(MEMCACHED_MAXIMUM_RETURN)); return TEST_SUCCESS; } -- 2.30.2