1
0
Fork 0

rather use null-pointers than try/catch for control flow (#7271)

This commit is contained in:
Wilfried Goesgens 2018-11-08 16:27:44 +01:00 committed by Max Neunhöffer
parent ffd40088ed
commit 0ce4c715b2
2 changed files with 26 additions and 29 deletions

View File

@ -173,32 +173,28 @@ auth::TokenCache::Entry auth::TokenCache::checkAuthenticationBasic(
auth::TokenCache::Entry auth::TokenCache::checkAuthenticationJWT( auth::TokenCache::Entry auth::TokenCache::checkAuthenticationJWT(
std::string const& jwt) { std::string const& jwt) {
try {
// note that we need the write lock here because it is an LRU // note that we need the write lock here because it is an LRU
// cache. reading from it will move the read entry to the start of // cache. reading from it will move the read entry to the start of
// the cache's linked list. so acquiring just a read-lock is // the cache's linked list. so acquiring just a read-lock is
// insufficient!! // insufficient!!
{
WRITE_LOCKER(writeLocker, _jwtLock); WRITE_LOCKER(writeLocker, _jwtLock);
// intentionally copy the entry from the cache // intentionally copy the entry from the cache
auth::TokenCache::Entry const& entry = _jwtCache.get(jwt); auth::TokenCache::Entry const* entry = _jwtCache.get(jwt);
if (entry != nullptr) {
// would have thrown if not found // would have thrown if not found
if (entry.expired()) { if (entry->expired()) {
try {
_jwtCache.remove(jwt); _jwtCache.remove(jwt);
} catch (std::range_error const&) {
}
LOG_TOPIC(TRACE, Logger::AUTHENTICATION) << "JWT Token expired"; LOG_TOPIC(TRACE, Logger::AUTHENTICATION) << "JWT Token expired";
return auth::TokenCache::Entry::Unauthenticated(); return auth::TokenCache::Entry::Unauthenticated();
} }
if (_userManager != nullptr) { if (_userManager != nullptr) {
// LDAP rights might need to be refreshed // LDAP rights might need to be refreshed
_userManager->refreshUser(entry.username()); _userManager->refreshUser(entry->username());
}
return *entry;
} }
return entry;
} catch (std::range_error const&) {
// mop: not found
} }
std::vector<std::string> const parts = StringUtils::split(jwt, '.'); std::vector<std::string> const parts = StringUtils::split(jwt, '.');
if (parts.size() != 3) { if (parts.size() != 3) {
LOG_TOPIC(TRACE, arangodb::Logger::AUTHENTICATION) LOG_TOPIC(TRACE, arangodb::Logger::AUTHENTICATION)
@ -224,16 +220,16 @@ auth::TokenCache::Entry auth::TokenCache::checkAuthenticationJWT(
return auth::TokenCache::Entry::Unauthenticated(); return auth::TokenCache::Entry::Unauthenticated();
} }
auth::TokenCache::Entry entry = validateJwtBody(body); auth::TokenCache::Entry newEntry = validateJwtBody(body);
if (!entry._authenticated) { if (!newEntry._authenticated) {
LOG_TOPIC(TRACE, arangodb::Logger::AUTHENTICATION) LOG_TOPIC(TRACE, arangodb::Logger::AUTHENTICATION)
<< "Couldn't validate jwt body " << body; << "Couldn't validate jwt body " << body;
return auth::TokenCache::Entry::Unauthenticated(); return auth::TokenCache::Entry::Unauthenticated();
} }
WRITE_LOCKER(writeLocker, _jwtLock); WRITE_LOCKER(writeLocker, _jwtLock);
_jwtCache.put(jwt, entry); _jwtCache.put(jwt, newEntry);
return entry; return newEntry;
} }
std::shared_ptr<VPackBuilder> auth::TokenCache::parseJson( std::shared_ptr<VPackBuilder> auth::TokenCache::parseJson(

View File

@ -88,23 +88,24 @@ class LruCache {
} }
} }
const value_t& get(const key_t& key) { value_t const* get(const key_t& key) {
auto it = _cache_items_map.find(key); auto it = _cache_items_map.find(key);
if (it == _cache_items_map.end()) { if (it == _cache_items_map.end()) {
throw std::range_error("There is no such key in cache"); return nullptr;
} else { } else {
_cache_items_list.splice(_cache_items_list.begin(), _cache_items_list, it->second); _cache_items_list.splice(_cache_items_list.begin(), _cache_items_list, it->second);
return it->second->second; return &it->second->second;
} }
} }
void remove(key_t const& key) { bool remove(key_t const& key) {
auto it = _cache_items_map.find(key); auto it = _cache_items_map.find(key);
if (it == _cache_items_map.end()) { if (it == _cache_items_map.end()) {
throw std::range_error("There is no such key in cache"); return false;
} else { } else {
_cache_items_list.erase(it->second); _cache_items_list.erase(it->second);
_cache_items_map.erase(it); _cache_items_map.erase(it);
return true;
} }
} }