From: Miod Vallat Date: Fri, 13 Jun 2025 12:22:17 +0000 (+0200) Subject: Use the mdb_strerror() wrapper everywhere consistently. X-Git-Tag: dnsdist-2.1.0-alpha0~11^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=223f28750cd1dd4ae828a23ba0d8ef365913a963;p=thirdparty%2Fpdns.git Use the mdb_strerror() wrapper everywhere consistently. Signed-off-by: Miod Vallat --- diff --git a/ext/lmdb-safe/lmdb-safe.cc b/ext/lmdb-safe/lmdb-safe.cc index 128e0c398b..84866cfa22 100644 --- a/ext/lmdb-safe/lmdb-safe.cc +++ b/ext/lmdb-safe/lmdb-safe.cc @@ -17,11 +17,6 @@ using std::runtime_error; using std::tuple; using std::weak_ptr; -static string MDBError(int rc) -{ - return mdb_strerror(rc); -} - #ifndef DNSDIST namespace LMDBLS { @@ -261,8 +256,9 @@ MDB_txn *MDBRWTransactionImpl::openRWTransaction(MDBEnv *env, MDB_txn *parent, i throw std::runtime_error("Duplicate RW transaction"); } - if(int rc=mdb_txn_begin(env->d_env, parent, flags, &result)) - throw std::runtime_error("Unable to start RW transaction: "+std::string(mdb_strerror(rc))); + if(int retCode=mdb_txn_begin(env->d_env, parent, flags, &result); retCode != 0) { + throw std::runtime_error("Unable to start RW transaction: "+MDBError(retCode)); + } env->incRWTX(); return result; @@ -292,8 +288,8 @@ void MDBRWTransactionImpl::commit() return; } - if(int rc = mdb_txn_commit(d_txn)) { - throw std::runtime_error("committing: " + std::string(mdb_strerror(rc))); + if(int retCode = mdb_txn_commit(d_txn); retCode != 0) { + throw std::runtime_error("committing: " + MDBError(retCode)); } environment().decRWTX(); d_txn = nullptr; @@ -329,8 +325,9 @@ MDB_txn *MDBROTransactionImpl::openROTransaction(MDBEnv *env, MDB_txn *parent, i A transaction and its cursors must only be used by a single thread, and a thread may only have a single transaction at a time. If MDB_NOTLS is in use, this does not apply to read-only transactions. */ MDB_txn *result = nullptr; - if(int rc=mdb_txn_begin(env->d_env, parent, MDB_RDONLY | flags, &result)) - throw std::runtime_error("Unable to start RO transaction: "+string(mdb_strerror(rc))); + if(int retCode=mdb_txn_begin(env->d_env, parent, MDB_RDONLY | flags, &result); retCode != 0) { + throw std::runtime_error("Unable to start RO transaction: "+MDBError(retCode)); + } env->incROTX(); @@ -385,8 +382,8 @@ void MDBROTransactionImpl::commit() void MDBRWTransactionImpl::clear(MDB_dbi dbi) { - if(int rc = mdb_drop(d_txn, dbi, 0)) { - throw runtime_error("Error clearing database: " + MDBError(rc)); + if(int retCode = mdb_drop(d_txn, dbi, 0); retCode != 0) { + throw runtime_error("Error clearing database: " + MDBError(retCode)); } } @@ -394,8 +391,8 @@ MDBRWCursor MDBRWTransactionImpl::getRWCursor(const MDBDbi& dbi) { MDB_cursor *cursor; int rc= mdb_cursor_open(d_txn, dbi, &cursor); - if(rc) { - throw std::runtime_error("Error creating RW cursor: "+std::string(mdb_strerror(rc))); + if(rc != 0) { + throw std::runtime_error("Error creating RW cursor: "+MDBError(rc)); } return MDBRWCursor(d_rw_cursors, cursor, d_txn, d_txtime); @@ -409,8 +406,8 @@ MDBRWCursor MDBRWTransactionImpl::getCursor(const MDBDbi &dbi) MDBRWTransaction MDBRWTransactionImpl::getRWTransaction() { MDB_txn *txn; - if (int rc = mdb_txn_begin(environment(), *this, 0, &txn)) { - throw std::runtime_error(std::string("failed to start child transaction: ")+mdb_strerror(rc)); + if (int retCode = mdb_txn_begin(environment(), *this, 0, &txn); retCode != 0) { + throw std::runtime_error(std::string("failed to start child transaction: ")+MDBError(retCode)); } // we need to increase the counter here because commit/abort on the child transaction will decrease it environment().incRWTX(); @@ -450,8 +447,8 @@ MDBROCursor MDBROTransactionImpl::getROCursor(const MDBDbi &dbi) { MDB_cursor *cursor; int rc= mdb_cursor_open(d_txn, dbi, &cursor); - if(rc) { - throw std::runtime_error("Error creating RO cursor: "+std::string(mdb_strerror(rc))); + if(rc != 0) { + throw std::runtime_error("Error creating RO cursor: "+MDBError(rc)); } return MDBROCursor(d_cursors, cursor); } diff --git a/ext/lmdb-safe/lmdb-safe.hh b/ext/lmdb-safe/lmdb-safe.hh index fada374945..0a737bb7f3 100644 --- a/ext/lmdb-safe/lmdb-safe.hh +++ b/ext/lmdb-safe/lmdb-safe.hh @@ -33,6 +33,11 @@ using std::string; #define StringView string #endif +static inline string MDBError(int ret) +{ + return mdb_strerror(ret); +} + /* open issues: * * - Missing convenience functions (string_view, string). @@ -405,8 +410,8 @@ public: int rc = mdb_get(d_txn, dbi, const_cast(&key.d_mdbval), const_cast(&val.d_mdbval)); - if(rc && rc != MDB_NOTFOUND) { - throw std::runtime_error("getting data: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("getting data: " + MDBError(rc)); } #ifndef DNSDIST @@ -607,8 +612,8 @@ private: } rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op); - if(rc && rc != MDB_NOTFOUND) { - throw std::runtime_error("Unable to get from cursor: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("Unable to get from cursor: " + MDBError(rc)); } if (rc == MDB_NOTFOUND) { @@ -634,8 +639,9 @@ public: { d_prefix.clear(); int rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op); - if(rc && rc != MDB_NOTFOUND) - throw std::runtime_error("Unable to get from cursor: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("Unable to get from cursor: " + MDBError(rc)); + } return skipDeleted(key, data, op, rc); } @@ -644,8 +650,9 @@ public: d_prefix.clear(); key.d_mdbval = in.d_mdbval; int rc=mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, MDB_SET); - if(rc && rc != MDB_NOTFOUND) - throw std::runtime_error("Unable to find from cursor: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("Unable to find from cursor: " + MDBError(rc)); + } return skipDeleted(key, data, MDB_SET, rc); } @@ -666,8 +673,9 @@ public: key.d_mdbval = in.d_mdbval; int rc = mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, MDB_SET_RANGE); - if(rc && rc != MDB_NOTFOUND) - throw std::runtime_error("Unable to lower_bound from cursor: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("Unable to lower_bound from cursor: " + MDBError(rc)); + } return skipDeleted(key, data, MDB_SET_RANGE, rc); } @@ -675,8 +683,9 @@ public: int nextprev(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op) { int rc = mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, op); - if(rc && rc != MDB_NOTFOUND) - throw std::runtime_error("Unable to prevnext from cursor: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("Unable to prevnext from cursor: " + MDBError(rc)); + } return skipDeleted(key, data, op, rc); } @@ -693,8 +702,9 @@ public: int currentlast(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op) { int rc = mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, op); - if(rc && rc != MDB_NOTFOUND) - throw std::runtime_error("Unable to next from cursor: " + std::string(mdb_strerror(rc))); + if(rc != 0 && rc != MDB_NOTFOUND) { + throw std::runtime_error("Unable to next from cursor: " + MDBError(rc)); + } return skipDeleted(key, data, op, rc); } @@ -810,7 +820,7 @@ public: const_cast(&key.d_mdbval), const_cast(&pval.d_mdbval), flags); if (mdbPutRc != 0) { - throw std::runtime_error("putting data: " + std::string(mdb_strerror(mdbPutRc))); + throw std::runtime_error("putting data: " + MDBError(mdbPutRc)); } } #else @@ -821,8 +831,9 @@ public: int rc; if ((rc = mdb_put(d_txn, dbi, const_cast(&key.d_mdbval), - const_cast(&val.d_mdbval), flags))) - throw std::runtime_error("putting data: " + std::string(mdb_strerror(rc))); + const_cast(&val.d_mdbval), flags))) { + throw std::runtime_error("putting data: " + MDBError(rc)); + } } #endif @@ -846,7 +857,7 @@ public: const_cast(&key.d_mdbval), const_cast(&pval.d_mdbval), 0); if (mdbPutRc != 0) { - throw std::runtime_error("marking data deleted: " + std::string(mdb_strerror(mdbPutRc))); + throw std::runtime_error("marking data deleted: " + MDBError(mdbPutRc)); } return; } @@ -854,7 +865,7 @@ public: // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) int mdbDelRc = mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, nullptr); if (mdbDelRc != 0 && mdbDelRc != MDB_NOTFOUND) { - throw std::runtime_error("deleting data: " + std::string(mdb_strerror(mdbDelRc))); + throw std::runtime_error("deleting data: " + MDBError(mdbDelRc)); } } @@ -868,8 +879,8 @@ public: // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) const_cast(&key.d_mdbval), const_cast(&val.d_mdbval)); - if ((mdbGetRc != 0) && mdbGetRc != MDB_NOTFOUND) { - throw std::runtime_error("getting data: " + std::string(mdb_strerror(mdbGetRc))); + if (mdbGetRc != 0 && mdbGetRc != MDB_NOTFOUND) { + throw std::runtime_error("getting data: " + MDBError(mdbGetRc)); } #ifndef DNSDIST @@ -927,8 +938,9 @@ public: int rc = mdb_cursor_put(*this, const_cast(&key.d_mdbval), const_cast(&pval.d_mdbval), MDB_CURRENT); - if(rc) - throw std::runtime_error("mdb_cursor_put: " + std::string(mdb_strerror(rc))); + if(rc != 0) { + throw std::runtime_error("mdb_cursor_put: " + MDBError(rc)); + } } #else void put(const MDBOutVal& key, const MDBInVal& data) @@ -936,8 +948,9 @@ public: int rc = mdb_cursor_put(*this, const_cast(&key.d_mdbval), const_cast(&data.d_mdbval), MDB_CURRENT); - if(rc) - throw std::runtime_error("mdb_cursor_put: " + std::string(mdb_strerror(rc))); + if(rc != 0) { + throw std::runtime_error("mdb_cursor_put: " + MDBError(rc)); + } } #endif @@ -960,13 +973,13 @@ public: const_cast(&pkey.d_mdbval), const_cast(&pval.d_mdbval), 0); if(rc_put) { - throw std::runtime_error("marking data deleted: " + std::string(mdb_strerror(rc_put))); + throw std::runtime_error("marking data deleted: " + MDBError(rc_put)); } } else { // do a normal delete if (int rc_del = mdb_cursor_del(*this, 0); rc_del != 0) { - throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc_del))); + throw std::runtime_error("deleting data: " + MDBError(rc_del)); } } } diff --git a/ext/lmdb-safe/lmdb-typed.hh b/ext/lmdb-safe/lmdb-typed.hh index f3421e1733..dc04b19e8a 100644 --- a/ext/lmdb-safe/lmdb-typed.hh +++ b/ext/lmdb-safe/lmdb-typed.hh @@ -453,7 +453,7 @@ public: d_end = true; } else if(rc != 0) { - throw std::runtime_error("in genoperator, " + std::string(mdb_strerror(rc))); + throw std::runtime_error("in genoperator, " + MDBError(rc)); } else if(!d_prefix.empty() && // d_key.getNoStripHeader().rfind(d_prefix, 0)!=0 &&