From: Fred Morcos Date: Thu, 17 Oct 2024 14:02:17 +0000 (+0200) Subject: Rework MDBOutVal X-Git-Tag: rec-5.2.0-alpha1~23^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ddecc97aee88cc9498f3d70788c58cdacb937b28;p=thirdparty%2Fpdns.git Rework MDBOutVal --- diff --git a/ext/lmdb-safe/lmdb-safe.hh b/ext/lmdb-safe/lmdb-safe.hh index 40f203f21b..fc4c0ae7a3 100644 --- a/ext/lmdb-safe/lmdb-safe.hh +++ b/ext/lmdb-safe/lmdb-safe.hh @@ -166,6 +166,14 @@ namespace LMDBLS { #endif /* ifndef DNSDIST */ +template +auto networkToHostByteOrder(T value) -> T; + +template <> +inline auto networkToHostByteOrder(uint32_t value) -> uint32_t +{ + return ntohl(value); +} struct MDBOutVal { @@ -174,19 +182,11 @@ struct MDBOutVal return d_mdbval; } -#ifndef DNSDIST - template ::value, T>::type* = nullptr> - T get() const; - - template ::value, T>::type* = nullptr> - T getNoStripHeader() const; -#endif /* ifndef DNSDIST */ - - template ::value, T>::type* = nullptr> + template T get() const; #ifndef DNSDIST - template ::value, T>::type* = nullptr> + template T getNoStripHeader() const; #endif @@ -194,59 +194,77 @@ struct MDBOutVal }; #ifndef DNSDIST -template <> -inline uint32_t MDBOutVal::get() const +template +inline T MDBOutVal::get() const { - uint32_t ret = 0; - size_t offset = LMDBLS::LScheckHeaderAndGetSize(this, sizeof(uint32_t)); - // NOLINTNEXTLINE - memcpy(&ret, reinterpret_cast(d_mdbval.mv_data) + offset, sizeof(uint32_t)); - ret = ntohl(ret); + T ret{}; + size_t offset = LMDBLS::LScheckHeaderAndGetSize(this, sizeof(ret)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + memcpy(&ret, static_cast(d_mdbval.mv_data) + offset, sizeof(ret)); + ret = networkToHostByteOrder(ret); return ret; } -template <> -inline uint32_t MDBOutVal::getNoStripHeader() const +template +inline T MDBOutVal::getNoStripHeader() const { - uint32_t ret = 0; - if (d_mdbval.mv_size != sizeof(uint32_t)) { + T ret{}; + if (d_mdbval.mv_size != sizeof(ret)) { throw std::runtime_error("MDB data has wrong length for type"); } - memcpy(&ret, d_mdbval.mv_data, sizeof(uint32_t)); - ret = ntohl(ret); + memcpy(&ret, d_mdbval.mv_data, sizeof(ret)); + ret = networkToHostByteOrder(ret); return ret; } #endif /* ifndef DNSDIST */ -template<> inline std::string MDBOutVal::get() const +#ifdef DNSDIST + +template <> +inline std::string MDBOutVal::get() const { -#ifndef DNSDIST - size_t offset = LMDBLS::LScheckHeaderAndGetSize(this); + return {static_cast(d_mdbval.mv_data), d_mdbval.mv_size}; +} - return std::string((char*)d_mdbval.mv_data+offset, d_mdbval.mv_size-offset); +template <> +inline string_view MDBOutVal::get() const +{ + return {static_cast(d_mdbval.mv_data), d_mdbval.mv_size}; } -template<> inline std::string MDBOutVal::getNoStripHeader() const +#else + +template <> +inline std::string MDBOutVal::get() const { -#endif - return std::string((char*)d_mdbval.mv_data, d_mdbval.mv_size); + size_t offset = LMDBLS::LScheckHeaderAndGetSize(this); + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + return {static_cast(d_mdbval.mv_data) + offset, d_mdbval.mv_size - offset}; } -template<> inline string_view MDBOutVal::get() const +template <> +inline string_view MDBOutVal::get() const { -#ifndef DNSDIST size_t offset = LMDBLS::LScheckHeaderAndGetSize(this); + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + return {static_cast(d_mdbval.mv_data) + offset, d_mdbval.mv_size - offset}; +} - return string_view((char*)d_mdbval.mv_data+offset, d_mdbval.mv_size-offset); +template <> +inline std::string MDBOutVal::getNoStripHeader() const +{ + return {static_cast(d_mdbval.mv_data), d_mdbval.mv_size}; } -template<> inline string_view MDBOutVal::getNoStripHeader() const +template <> +inline string_view MDBOutVal::getNoStripHeader() const { -#endif - return string_view((char*)d_mdbval.mv_data, d_mdbval.mv_size); + return {static_cast(d_mdbval.mv_data), d_mdbval.mv_size}; } +#endif // ifdef DNSDIST + class MDBInVal { public: