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.
This commit is contained in:
Carsten Grohmann 2021-06-21 01:08:56 +02:00 committed by GitHub
parent cc022a68f4
commit c4ac923b4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);