From b6898b5f3787e2528225fd5e22927f2e5ba3cc2b Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Thu, 24 Jul 2025 11:11:24 +0200 Subject: [PATCH] dns: Get rid of the ugly special case for OpCode 3 As suggested by Miod. Signed-off-by: Remi Gacogne --- pdns/dns.cc | 8 ++++---- pdns/test-dns_cc.cc | 10 ++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) 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)); -- 2.47.2