From f5ff7d7c1162b1d8b603675566168d8b2f132141 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 1 Mar 2015 20:04:24 +0000 Subject: Refactor to avoid possible destructed string copy --- netfs/daemon/daemon.cpp | 4 ++-- netfs/daemon/daemonFile.cpp | 2 +- netfs/daemon/daemonVolume.cpp | 2 +- netfs/fuse/fuseApp.cpp | 5 ++++- netfs/fuse/fuseDirs.cpp | 2 +- netfs/fuse/fuseFiles.cpp | 2 +- netfs/fuse/fuseMisc.cpp | 2 +- netfs/ice/typeConvert.cpp | 8 ++++---- netfs/ice/typeConvert.h | 8 ++++---- netfs/lib/entCache.cpp | 16 +++++++++------- netfs/lib/entCache.h | 4 ++-- 11 files changed, 30 insertions(+), 25 deletions(-) diff --git a/netfs/daemon/daemon.cpp b/netfs/daemon/daemon.cpp index b5a2ddd..3ad6294 100644 --- a/netfs/daemon/daemon.cpp +++ b/netfs/daemon/daemon.cpp @@ -76,8 +76,8 @@ extern "C" { } TempPrivs::TempPrivs(const NetFS::ReqEnv & re, const boost::filesystem::path & r) : - myu(UserEntCache::instance.getID(re.user)), - myg(GroupEntCache::instance.getID(re.grp)), + myu(UserEntCache::instance.getEntry(re.user)->id), + myg(GroupEntCache::instance.getEntry(re.grp)->id), root(r) { } diff --git a/netfs/daemon/daemonFile.cpp b/netfs/daemon/daemonFile.cpp index 3268be8..3acd1ca 100644 --- a/netfs/daemon/daemonFile.cpp +++ b/netfs/daemon/daemonFile.cpp @@ -36,7 +36,7 @@ FileServer::fgetattr(const NetFS::ReqEnv & re, const Ice::Current &) throw NetFS::SystemError(errno); } NetFS::Attr a; - a << StatSource { s, boost::bind(&UserEntCache::getName, &UserEntCache::instance, _1), boost::bind(&GroupEntCache::getName, &GroupEntCache::instance, _1) }; + a << StatSource { s, boost::bind(&UserEntCache::getName, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getName, &GroupEntCache::instance, _1, _2) }; return a; } diff --git a/netfs/daemon/daemonVolume.cpp b/netfs/daemon/daemonVolume.cpp index 0cad47e..0556d9a 100644 --- a/netfs/daemon/daemonVolume.cpp +++ b/netfs/daemon/daemonVolume.cpp @@ -70,7 +70,7 @@ VolumeServer::getattr(const NetFS::ReqEnv & re, const std::string & path, const throw NetFS::SystemError(errno); } NetFS::Attr a; - a << StatSource { s, boost::bind(&UserEntCache::getName, &UserEntCache::instance, _1), boost::bind(&GroupEntCache::getName, &GroupEntCache::instance, _1) }; + a << StatSource { s, boost::bind(&UserEntCache::getName, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getName, &GroupEntCache::instance, _1, _2) }; return a; } diff --git a/netfs/fuse/fuseApp.cpp b/netfs/fuse/fuseApp.cpp index c90cc15..fdcce57 100644 --- a/netfs/fuse/fuseApp.cpp +++ b/netfs/fuse/fuseApp.cpp @@ -171,6 +171,9 @@ NetFS::FuseApp::reqEnv() connectToService(); connectToVolume(); struct fuse_context * c = fuse_get_context(); - return { UserEntCache::instance.getName(c->uid), GroupEntCache::instance.getName(c->gid) }; + NetFS::ReqEnv re; + UserEntCache::instance.getName(c->uid, &re.user); + GroupEntCache::instance.getName(c->gid, &re.grp); + return re; } diff --git a/netfs/fuse/fuseDirs.cpp b/netfs/fuse/fuseDirs.cpp index 156dae6..1da9484 100644 --- a/netfs/fuse/fuseDirs.cpp +++ b/netfs/fuse/fuseDirs.cpp @@ -79,7 +79,7 @@ NetFS::FuseApp::readdir(const char * p, void * buf, fuse_fill_dir_t filler, off_ statCache.Add(new OptimisticCallCacheable( [asga,this]() { struct stat s; - s << AttrSource { volume->end_getattr(asga), boost::bind(&UserEntCache::getID, &UserEntCache::instance, _1), boost::bind(&GroupEntCache::getID, &GroupEntCache::instance, _1) }; + s << AttrSource { volume->end_getattr(asga), boost::bind(&UserEntCache::getID, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getID, &GroupEntCache::instance, _1, _2) }; return s; }, epath, time_t(NULL) + 2)); } diff --git a/netfs/fuse/fuseFiles.cpp b/netfs/fuse/fuseFiles.cpp index b8399f0..c6d4283 100644 --- a/netfs/fuse/fuseFiles.cpp +++ b/netfs/fuse/fuseFiles.cpp @@ -135,7 +135,7 @@ NetFS::FuseApp::fgetattr(const char *, struct stat * s, fuse_file_info * fi) { try { auto remote = getFileProxy(fi->fh)->remote; - *s << AttrSource { remote->fgetattr(reqEnv()), boost::bind(&UserEntCache::getID, &UserEntCache::instance, _1), boost::bind(&GroupEntCache::getID, &GroupEntCache::instance, _1) }; + *s << AttrSource { remote->fgetattr(reqEnv()), boost::bind(&UserEntCache::getID, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getID, &GroupEntCache::instance, _1, _2) }; return 0; } catch (NetFS::SystemError & e) { diff --git a/netfs/fuse/fuseMisc.cpp b/netfs/fuse/fuseMisc.cpp index 670c9ae..1f999bd 100644 --- a/netfs/fuse/fuseMisc.cpp +++ b/netfs/fuse/fuseMisc.cpp @@ -19,7 +19,7 @@ NetFS::FuseApp::getattr(const char * p, struct stat * s) *s = *cacehedStat; } else { - *s << AttrSource { volume->getattr(reqEnv(), p), boost::bind(&UserEntCache::getID, &UserEntCache::instance, _1), boost::bind(&GroupEntCache::getID, &GroupEntCache::instance, _1) }; + *s << AttrSource { volume->getattr(reqEnv(), p), boost::bind(&UserEntCache::getID, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getID, &GroupEntCache::instance, _1, _2) }; } return 0; } diff --git a/netfs/ice/typeConvert.cpp b/netfs/ice/typeConvert.cpp index 538b894..cc3ee93 100644 --- a/netfs/ice/typeConvert.cpp +++ b/netfs/ice/typeConvert.cpp @@ -7,8 +7,8 @@ operator<<(struct stat & s, const AttrSource & a) s.st_ino = a.attr.inode; s.st_mode = a.attr.mode; s.st_nlink = a.attr.links; - s.st_uid = a.user(a.attr.uid); - s.st_gid = a.group(a.attr.gid); + a.user(a.attr.uid, &s.st_uid); + a.group(a.attr.gid, &s.st_gid); s.st_rdev = a.attr.rdev; s.st_size = a.attr.size; s.st_blksize = a.attr.blockSize; @@ -41,8 +41,8 @@ operator<<(NetFS::Attr & a, const struct StatSource & s) a.inode = s.stat.st_ino; a.mode = s.stat.st_mode; a.links = s.stat.st_nlink; - a.uid = s.user(s.stat.st_uid); - a.gid = s.group(s.stat.st_gid); + s.user(s.stat.st_uid, &a.uid); + s.group(s.stat.st_gid, &a.gid); a.rdev = s.stat.st_rdev; a.size = s.stat.st_size; a.blockSize = s.stat.st_blksize; diff --git a/netfs/ice/typeConvert.h b/netfs/ice/typeConvert.h index dd56a3f..13e2b44 100644 --- a/netfs/ice/typeConvert.h +++ b/netfs/ice/typeConvert.h @@ -3,11 +3,11 @@ #include #include -typedef boost::function UserIdLookup; -typedef boost::function GroupIdLookup; +typedef boost::function UserIdLookup; +typedef boost::function GroupIdLookup; -typedef boost::function UserNameLookup; -typedef boost::function GroupNameLookup; +typedef boost::function UserNameLookup; +typedef boost::function GroupNameLookup; struct AttrSource { const NetFS::Attr & attr; diff --git a/netfs/lib/entCache.cpp b/netfs/lib/entCache.cpp index b07dda7..286c017 100644 --- a/netfs/lib/entCache.cpp +++ b/netfs/lib/entCache.cpp @@ -17,17 +17,19 @@ EntCache::~EntCache() } template -const typename EntCache::id_t & -EntCache::getID(const EntCache::name_t & u) const +void +EntCache::getID(const EntCache::name_t & u, EntCache::id_t * target) const { - return getEntry(u)->id; + auto e = getEntry(u); + *target = e->id; } template -const typename EntCache::name_t & -EntCache::getName(const EntCache::id_t & u) const +void +EntCache::getName(const EntCache::id_t & u, EntCache::name_t * target) const { - return getEntry(u)->name; + auto e = getEntry(u); + *target = e->name; } template @@ -107,7 +109,7 @@ EntCache::fillCache() const auto g = new Group(grp->gr_gid, grp->gr_name); for (auto member = grp->gr_mem; *member; member++) { try { - g->members.insert(EntCache::instance.getID(*member)); + g->members.insert(EntCache::instance.getEntry((const name_t &)*member)->id); } catch (const NetFS::SystemError &) { } diff --git a/netfs/lib/entCache.h b/netfs/lib/entCache.h index ea941e8..443474f 100644 --- a/netfs/lib/entCache.h +++ b/netfs/lib/entCache.h @@ -38,8 +38,8 @@ class EntCache { virtual ~EntCache(); - const id_t & getID(const name_t & ) const; - const name_t & getName(const id_t &) const; + void getID(const name_t &, id_t *) const; + void getName(const id_t &, name_t *) const; template entry_ptr getEntry(const key_t &) const; -- cgit v1.2.3