]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Use the mdb_strerror() wrapper everywhere consistently.
authorMiod Vallat <miod.vallat@powerdns.com>
Fri, 13 Jun 2025 12:22:17 +0000 (14:22 +0200)
committerMiod Vallat <miod.vallat@powerdns.com>
Mon, 23 Jun 2025 14:12:15 +0000 (16:12 +0200)
Signed-off-by: Miod Vallat <miod.vallat@powerdns.com>
ext/lmdb-safe/lmdb-safe.cc
ext/lmdb-safe/lmdb-safe.hh
ext/lmdb-safe/lmdb-typed.hh

index 128e0c398b8dbcf2c5bc914cb839d9b5e61d0017..84866cfa225fcc358ad2d1e2f60dc77cb1b607b2 100644 (file)
@@ -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);
 }
index fada3749457fc7bd593c3c11d7c0c91ff8b6f8a3..0a737bb7f39b2ac98fcd18b28eaa07facb0bf05a 100644 (file)
@@ -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<MDB_val*>(&key.d_mdbval),
                      const_cast<MDB_val*>(&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<MDB_val*>(&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<MDB_val*>(&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<MDB_val*>(&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<MDB_val*>(&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<MDB_val*>(&key.d_mdbval),
                            const_cast<MDB_val*>(&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<MDB_val*>(&key.d_mdbval),
-                      const_cast<MDB_val*>(&val.d_mdbval), flags)))
-      throw std::runtime_error("putting data: " + std::string(mdb_strerror(rc)));
+                      const_cast<MDB_val*>(&val.d_mdbval), flags))) {
+      throw std::runtime_error("putting data: " + MDBError(rc));
+    }
   }
 #endif
 
@@ -846,7 +857,7 @@ public:
         const_cast<MDB_val*>(&key.d_mdbval),
         const_cast<MDB_val*>(&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<MDB_val*>(&key.d_mdbval),
                            const_cast<MDB_val*>(&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<MDB_val*>(&key.d_mdbval),
                             const_cast<MDB_val*>(&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<MDB_val*>(&key.d_mdbval),
                             const_cast<MDB_val*>(&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<MDB_val*>(&pkey.d_mdbval),
                      const_cast<MDB_val*>(&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));
       }
     }
   }
index f3421e1733c1845ed9eb00950f88d0208ebc87ba..dc04b19e8a7e8a03e224ae7eb46fb533e2ab2038 100644 (file)
@@ -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<std::string>().rfind(d_prefix, 0)!=0 &&