Use smart pointer to manage pcfstat object

Previously pcfstat points to a raw pointer, and it may be leaked if
function returned before deleting it.

So use smart pointer to automatically release the object.

Note that currently s3fs only uses c++03, so we use auto_ptr here, not
unique_ptr, which requires c++11.

Fixes: 6ca5a24a7f ("Fix two inconsistency issues between stat cache and cache file (#2152)")
Signed-off-by: Eryu Guan <eguan@linux.alibaba.com>
This commit is contained in:
Eryu Guan 2023-05-31 12:36:23 +08:00 committed by Andrew Gaul
parent d0a944fcaa
commit 7978395083
1 changed files with 4 additions and 6 deletions

View File

@ -24,6 +24,7 @@
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <memory>
#include "common.h"
#include "fdcache_entity.h"
@ -485,7 +486,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
bool need_save_csf = false; // need to save(reset) cache stat file
bool is_truncate = false; // need to truncate
CacheFileStat* pcfstat = NULL;
std::auto_ptr<CacheFileStat> pcfstat;
if(!cachepath.empty()){
// using cache
@ -500,7 +501,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
}
// open cache and cache stat file, load page info.
pcfstat = new CacheFileStat(path.c_str());
pcfstat.reset(new CacheFileStat(path.c_str()));
// try to open cache file
if( -1 != (physical_fd = open(cachepath.c_str(), O_RDWR)) &&
@ -638,14 +639,11 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
}
// reset cache stat file
if(need_save_csf && pcfstat){
if(need_save_csf && pcfstat.get()){
if(!pagelist.Serialize(*pcfstat, true, inode)){
S3FS_PRN_WARN("failed to save cache stat file(%s), but continue...", path.c_str());
}
}
if(pcfstat){
delete pcfstat;
}
// set original headers and size in it.
if(pmeta){