From: Remi Gacogne Date: Thu, 24 Jul 2025 09:11:24 +0000 (+0200) Subject: dns: Get rid of the ugly special case for OpCode 3 X-Git-Tag: auth-5.1.0-alpha0~7^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F15881%2Fhead;p=thirdparty%2Fpdns.git dns: Get rid of the ugly special case for OpCode 3 As suggested by Miod. Signed-off-by: Remi Gacogne --- diff --git a/pdns/dns.cc b/pdns/dns.cc index df35dfa77..8f11a378b 100644 --- a/pdns/dns.cc +++ b/pdns/dns.cc @@ -136,9 +136,9 @@ std::optional ERCode::from_short(const std::string_view& ercode_string } std::string Opcode::to_s(uint8_t opcode) { - static const std::array s_opcodes = { "Query", "IQuery", "Status", "3", "Notify", "Update" }; + static const std::array s_opcodes = { "Query", "IQuery", "Status", "", "Notify", "Update" }; - if (opcode >= s_opcodes.size()) { + if (opcode >= s_opcodes.size() || s_opcodes.at(opcode).empty()) { return std::to_string(opcode); } @@ -147,9 +147,9 @@ std::string Opcode::to_s(uint8_t opcode) { std::optional Opcode::from_lowercase_string(const std::string_view& opcode_string) { - static const std::array s_opcodes = { "query", "iquery", "status", "3", "notify", "update" }; + static const std::array s_opcodes = { "query", "iquery", "status", "", "notify", "update" }; const auto* position = std::find(s_opcodes.begin(), s_opcodes.end(), opcode_string); - if (position == s_opcodes.end()) { + if (position == s_opcodes.end() || position->empty()) { return std::nullopt; } return std::distance(s_opcodes.begin(), position); diff --git a/pdns/test-dns_cc.cc b/pdns/test-dns_cc.cc index 9eedc8c0d..6f03e6186 100644 --- a/pdns/test-dns_cc.cc +++ b/pdns/test-dns_cc.cc @@ -71,8 +71,14 @@ BOOST_AUTO_TEST_CASE(test_opcode) BOOST_CHECK(long_s.size() > 0); boost::to_lower(long_s); auto opcode = Opcode::from_lowercase_string(long_s); - BOOST_CHECK(opcode); - BOOST_CHECK_EQUAL(*opcode, idx); + if (idx != 3) { + BOOST_CHECK(opcode); + BOOST_CHECK_EQUAL(*opcode, idx); + } + else { + BOOST_CHECK_EQUAL(long_s, "3"); + BOOST_CHECK(!opcode); + } } BOOST_CHECK_EQUAL(Opcode::to_s(Opcode::Update + 1), std::to_string(Opcode::Update + 1));