From c4ac923b4cbcf704fdf3af883cca006a88ed2e86 Mon Sep 17 00:00:00 2001 From: Carsten Grohmann Date: Mon, 21 Jun 2021 01:08:56 +0200 Subject: [PATCH] Ensure NUL-terminated result after strncpy (#1694) Long symlinks may cause that the result buffer is filled and not proper terminated with a null byte. --- src/s3fs.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 1f123e1..1d29a6a 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -804,7 +804,7 @@ static int s3fs_readlink(const char* _path, char* buf, size_t size) WTF8_ENCODE(path) std::string strValue; - // check symblic link cache + // check symbolic link cache if(!StatCache::getStatCacheData()->GetSymlink(std::string(path), strValue)){ // not found in cache, then open the path { // scope for AutoFdEntity @@ -841,13 +841,14 @@ static int s3fs_readlink(const char* _path, char* buf, size_t size) strValue = s3fs_wtf8_decode(strValue); } - // add symblic link cache + // add symbolic link cache if(!StatCache::getStatCacheData()->AddSymlink(std::string(path), strValue)){ S3FS_PRN_ERR("failed to add symbolic link cache for %s", path); } } // copy result - strncpy(buf, strValue.c_str(), size); + strncpy(buf, strValue.c_str(), size - 1); + buf[size - 1] = '\0'; S3FS_MALLOCTRIM(0);