Browse Source

avoid possible dangling pointer to member of destructed local object. Add documentation. Avoid superfluous string copies.

synchronous
Roker 5 years ago
parent
commit
f4a0da11a2
  1. 18
      passphrase_cache.cc
  2. 8
      passphrase_cache.hh

18
passphrase_cache.cc

@ -1,8 +1,13 @@
#include <cassert> #include <cassert>
#include "passphrase_cache.hh" #include "passphrase_cache.hh"
namespace
{
const char* const empty_string = "";
}
namespace pEp { namespace pEp {
PassphraseCache::cache_entry::cache_entry(std::string p, time_point t) : PassphraseCache::cache_entry::cache_entry(const std::string& p, time_point t) :
passphrase{p, 0, PassphraseCache::cache_entry::max_len}, tp{t} passphrase{p, 0, PassphraseCache::cache_entry::max_len}, tp{t}
{ } { }
@ -27,18 +32,21 @@ namespace pEp {
return *this; return *this;
} }
const char *PassphraseCache::add(std::string passphrase) const char *PassphraseCache::add(const std::string& passphrase)
{ {
assert(_which == _cache.end()); // never modify while iterating assert(_which == _cache.end()); // never modify while iterating
std::lock_guard<std::mutex> lock(_mtx); std::lock_guard<std::mutex> lock(_mtx);
if (passphrase != "") { if (!passphrase.empty()) {
while (_cache.size() >= _max_size) while (_cache.size() >= _max_size)
_cache.pop_front(); _cache.pop_front();
_cache.emplace_back(cache_entry(passphrase, clock::now()));
_cache.emplace_back(passphrase, clock::now());
auto back = _cache.back(); // FIXME: In C++17 list::emplace_back() returns the just inserted element already.
return back.passphrase.c_str();
} }
return passphrase.c_str(); return empty_string;
} }
bool PassphraseCache::for_each_passphrase(const passphrase_callee& callee) bool PassphraseCache::for_each_passphrase(const passphrase_callee& callee)

8
passphrase_cache.hh

@ -16,7 +16,7 @@ namespace pEp {
struct cache_entry { struct cache_entry {
static const size_t max_len = 250 * 4; static const size_t max_len = 250 * 4;
cache_entry(std::string p, time_point t); cache_entry(const std::string& p, time_point t);
std::string passphrase; std::string passphrase;
time_point tp; time_point tp;
@ -43,9 +43,9 @@ namespace pEp {
PassphraseCache(const PassphraseCache& second); PassphraseCache(const PassphraseCache& second);
PassphraseCache& operator=(const PassphraseCache& second); PassphraseCache& operator=(const PassphraseCache& second);
// adding a passphrase to the cache, which will timeout // adds the passphrase to the cache, which will timeout
// returns a ptr to the passsword entry in the cache. Don't free() it!
const char *add(std::string passphrase); const char *add(const std::string& passphrase);
// get all passphrases in cache from latest to oldest one by each call // get all passphrases in cache from latest to oldest one by each call
// this function is throwing PassphraseCache::Empty when cache is empty // this function is throwing PassphraseCache::Empty when cache is empty

Loading…
Cancel
Save