From: Fred Morcos Date: Tue, 24 Sep 2024 07:28:17 +0000 (+0200) Subject: Cleanup lmdb-typed.cc X-Git-Tag: rec-5.2.0-alpha1~61^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65d91e4f589242b546607d4ada98de59a6318e4a;p=thirdparty%2Fpdns.git Cleanup lmdb-typed.cc --- diff --git a/ext/lmdb-safe/lmdb-typed.cc b/ext/lmdb-safe/lmdb-typed.cc index 1edc8324ff..e9a2d1e2c0 100644 --- a/ext/lmdb-safe/lmdb-typed.cc +++ b/ext/lmdb-safe/lmdb-typed.cc @@ -4,9 +4,10 @@ unsigned int MDBGetMaxID(MDBRWTransaction& txn, MDBDbi& dbi) { auto cursor = txn->getRWCursor(dbi); - MDBOutVal maxidval, maxcontent; + MDBOutVal maxidval{}; + MDBOutVal maxcontent{}; unsigned int maxid{0}; - if (!cursor.get(maxidval, maxcontent, MDB_LAST)) { + if (cursor.get(maxidval, maxcontent, MDB_LAST) == 0) { maxid = maxidval.getNoStripHeader(); } return maxid; @@ -15,15 +16,16 @@ unsigned int MDBGetMaxID(MDBRWTransaction& txn, MDBDbi& dbi) unsigned int MDBGetRandomID(MDBRWTransaction& txn, MDBDbi& dbi) { auto cursor = txn->getRWCursor(dbi); - unsigned int id; + unsigned int newID = 0; for (int attempts = 0; attempts < 20; attempts++) { - MDBOutVal key, content; + MDBOutVal key{}; + MDBOutVal content{}; // dns_random generates a random number in [0..signed_int_max-1]. We add 1 to avoid 0 and allow type_max. // 0 is avoided because the put() interface uses it to mean "please allocate a number for me" - id = dns_random(std::numeric_limits::max()) + 1; - if (cursor.find(MDBInVal(id), key, content)) { - return id; + newID = dns_random(std::numeric_limits::max()) + 1; + if (cursor.find(MDBInVal(newID), key, content) != 0) { + return newID; } } throw std::runtime_error("MDBGetRandomID() could not assign an unused random ID");