From 797839508366c32d2ae7c84280c5ad106f28b41b Mon Sep 17 00:00:00 2001 From: Eryu Guan Date: Wed, 31 May 2023 12:36:23 +0800 Subject: [PATCH] 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: 6ca5a24a7f29 ("Fix two inconsistency issues between stat cache and cache file (#2152)") Signed-off-by: Eryu Guan --- src/fdcache_entity.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/fdcache_entity.cpp b/src/fdcache_entity.cpp index d64bf4d..0e0e894 100644 --- a/src/fdcache_entity.cpp +++ b/src/fdcache_entity.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #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 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){