Skip to content

Commit 3e3887c

Browse files
committed
lp+mgmt+security: tidy up TLV parsing code in a few classes
Change-Id: I458bd10e16a05b08ed444f983a18453648cd191b
1 parent b7f97e6 commit 3e3887c

File tree

9 files changed

+95
-105
lines changed

9 files changed

+95
-105
lines changed

ndn-cxx/lp/cache-policy.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -37,11 +37,6 @@ operator<<(std::ostream& os, CachePolicyType policy)
3737
}
3838
}
3939

40-
CachePolicy::CachePolicy()
41-
: m_policy(CachePolicyType::NONE)
42-
{
43-
}
44-
4540
CachePolicy::CachePolicy(const Block& block)
4641
{
4742
wireDecode(block);
@@ -82,7 +77,6 @@ CachePolicy::wireEncode() const
8277
wireEncode(buffer);
8378

8479
m_wire = buffer.block();
85-
8680
return m_wire;
8781
}
8882

@@ -92,23 +86,20 @@ CachePolicy::wireDecode(const Block& wire)
9286
if (wire.type() != tlv::CachePolicy) {
9387
NDN_THROW(Error("CachePolicy", wire.type()));
9488
}
95-
9689
m_wire = wire;
9790
m_wire.parse();
9891

99-
auto it = m_wire.elements_begin();
100-
if (it == m_wire.elements_end()) {
101-
NDN_THROW(Error("Empty CachePolicy"));
102-
}
92+
m_policy = CachePolicyType::NONE;
10393

104-
if (it->type() == tlv::CachePolicyType) {
94+
auto it = m_wire.elements_begin();
95+
if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
10596
m_policy = readNonNegativeIntegerAs<CachePolicyType>(*it);
10697
if (getPolicy() == CachePolicyType::NONE) {
107-
NDN_THROW(Error("Unknown CachePolicyType"));
98+
NDN_THROW(Error("Unknown CachePolicyType " + std::to_string(to_underlying(m_policy))));
10899
}
109100
}
110101
else {
111-
NDN_THROW(Error("CachePolicyType", it->type()));
102+
NDN_THROW(Error("CachePolicyType is missing or out of order"));
112103
}
113104
}
114105

ndn-cxx/lp/cache-policy.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -34,14 +34,14 @@ namespace ndn::lp {
3434
*/
3535
enum class CachePolicyType {
3636
NONE = 0,
37-
NO_CACHE = 1
37+
NO_CACHE = 1,
3838
};
3939

4040
std::ostream&
4141
operator<<(std::ostream& os, CachePolicyType policy);
4242

4343
/**
44-
* \brief Represents a CachePolicy header field.
44+
* \brief Represents a %CachePolicy header field.
4545
*/
4646
class CachePolicy
4747
{
@@ -52,7 +52,7 @@ class CachePolicy
5252
using ndn::tlv::Error::Error;
5353
};
5454

55-
CachePolicy();
55+
CachePolicy() = default;
5656

5757
explicit
5858
CachePolicy(const Block& block);
@@ -81,7 +81,7 @@ class CachePolicy
8181
public: // policy type
8282
/**
8383
* \brief Get policy type code.
84-
* \retval CachePolicyType::NONE if policy type is unset or has an unknown code
84+
* \retval CachePolicyType::NONE if policy type is unset or has an unknown code.
8585
*/
8686
CachePolicyType
8787
getPolicy() const;
@@ -94,7 +94,7 @@ class CachePolicy
9494
setPolicy(CachePolicyType policy);
9595

9696
private:
97-
CachePolicyType m_policy;
97+
CachePolicyType m_policy = CachePolicyType::NONE;
9898
mutable Block m_wire;
9999
};
100100

ndn-cxx/lp/nack-header.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -22,6 +22,7 @@
2222
*/
2323

2424
#include "ndn-cxx/lp/nack-header.hpp"
25+
#include "ndn-cxx/encoding/block-helpers.hpp"
2526

2627
namespace ndn::lp {
2728

@@ -53,11 +54,6 @@ isLessSevere(lp::NackReason x, lp::NackReason y)
5354
return to_underlying(x) < to_underlying(y);
5455
}
5556

56-
NackHeader::NackHeader()
57-
: m_reason(NackReason::NONE)
58-
{
59-
}
60-
6157
NackHeader::NackHeader(const Block& block)
6258
{
6359
wireDecode(block);
@@ -90,7 +86,6 @@ NackHeader::wireEncode() const
9086
wireEncode(buffer);
9187

9288
m_wire = buffer.block();
93-
9489
return m_wire;
9590
}
9691

@@ -100,13 +95,13 @@ NackHeader::wireDecode(const Block& wire)
10095
if (wire.type() != tlv::Nack) {
10196
NDN_THROW(ndn::tlv::Error("Nack", wire.type()));
10297
}
103-
10498
m_wire = wire;
10599
m_wire.parse();
100+
106101
m_reason = NackReason::NONE;
107102

108-
if (m_wire.elements_size() > 0) {
109-
auto it = m_wire.elements_begin();
103+
auto it = m_wire.elements_begin();
104+
if (it != m_wire.elements_end()) {
110105
if (it->type() == tlv::NackReason) {
111106
m_reason = readNonNegativeIntegerAs<NackReason>(*it);
112107
}

ndn-cxx/lp/nack-header.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -24,7 +24,6 @@
2424
#ifndef NDN_CXX_LP_NACK_HEADER_HPP
2525
#define NDN_CXX_LP_NACK_HEADER_HPP
2626

27-
#include "ndn-cxx/encoding/block-helpers.hpp"
2827
#include "ndn-cxx/encoding/encoding-buffer.hpp"
2928
#include "ndn-cxx/lp/tlv.hpp"
3029

@@ -37,7 +36,7 @@ enum class NackReason {
3736
NONE = 0,
3837
CONGESTION = 50,
3938
DUPLICATE = 100,
40-
NO_ROUTE = 150
39+
NO_ROUTE = 150,
4140
};
4241

4342
std::ostream&
@@ -57,7 +56,7 @@ isLessSevere(lp::NackReason x, lp::NackReason y);
5756
class NackHeader
5857
{
5958
public:
60-
NackHeader();
59+
NackHeader() = default;
6160

6261
explicit
6362
NackHeader(const Block& block);
@@ -75,7 +74,7 @@ class NackHeader
7574
public: // reason
7675
/**
7776
* \brief Get reason code.
78-
* \retval NackReason::NONE if NackReason element does not exist or has an unknown code
77+
* \retval NackReason::NONE if NackReason element does not exist or has an unknown code.
7978
*/
8079
NackReason
8180
getReason() const;
@@ -88,7 +87,7 @@ class NackHeader
8887
setReason(NackReason reason);
8988

9089
private:
91-
NackReason m_reason;
90+
NackReason m_reason = NackReason::NONE;
9291
mutable Block m_wire;
9392
};
9493

ndn-cxx/mgmt/control-response.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -87,26 +87,31 @@ ControlResponse::wireDecode(const Block& wire)
8787
if (wire.type() != tlv::nfd::ControlResponse) {
8888
NDN_THROW(Error("ControlResponse", wire.type()));
8989
}
90+
91+
*this = {};
9092
m_wire = wire;
9193
m_wire.parse();
9294

93-
auto val = m_wire.elements_begin();
94-
if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) {
95-
NDN_THROW(Error("missing StatusCode sub-element"));
95+
auto it = m_wire.elements_begin();
96+
if (it != m_wire.elements_end() && it->type() == tlv::nfd::StatusCode) {
97+
m_code = readNonNegativeIntegerAs<uint32_t>(*it);
98+
++it;
99+
}
100+
else {
101+
NDN_THROW(Error("StatusCode is missing or out of order"));
96102
}
97-
m_code = readNonNegativeIntegerAs<uint32_t>(*val);
98-
++val;
99103

100-
if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) {
101-
NDN_THROW(Error("missing StatusText sub-element"));
104+
if (it != m_wire.elements_end() && it->type() == tlv::nfd::StatusText) {
105+
m_text = readString(*it);
106+
++it;
107+
}
108+
else {
109+
NDN_THROW(Error("StatusText is missing or out of order"));
102110
}
103-
m_text = readString(*val);
104-
++val;
105111

106-
if (val != m_wire.elements_end())
107-
m_body = *val;
108-
else
109-
m_body = {};
112+
if (it != m_wire.elements_end()) {
113+
m_body = *it;
114+
}
110115
}
111116

112117
} // namespace ndn::mgmt

ndn-cxx/mgmt/nfd/strategy-choice.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -87,11 +87,9 @@ StrategyChoice::wireDecode(const Block& block)
8787
if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
8888
val->parse();
8989
if (val->elements().empty()) {
90-
NDN_THROW(Error("expecting Strategy/Name"));
91-
}
92-
else {
93-
m_strategy.wireDecode(*val->elements_begin());
90+
NDN_THROW(Error("expecting Strategy.Name"));
9491
}
92+
m_strategy.wireDecode(val->elements().front());
9593
++val;
9694
}
9795
else {

ndn-cxx/security/additional-description.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2013-2023 Regents of the University of California.
3+
* Copyright (c) 2013-2025 Regents of the University of California.
44
*
55
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
66
*
@@ -40,7 +40,7 @@ AdditionalDescription::get(const std::string& key) const
4040
return it->second;
4141
}
4242

43-
NDN_THROW(Error("Entry does not exist for key (" + key + ")"));
43+
NDN_THROW(Error("Entry does not exist for key '" + key + "'"));
4444
}
4545

4646
void
@@ -115,41 +115,42 @@ AdditionalDescription::wireEncode() const
115115
wireEncode(buffer);
116116

117117
m_wire = buffer.block();
118-
m_wire.parse();
119-
120118
return m_wire;
121119
}
122120

123121
void
124122
AdditionalDescription::wireDecode(const Block& wire)
125123
{
126-
if (!wire.hasWire()) {
127-
NDN_THROW(Error("The supplied block does not contain wire format"));
124+
if (wire.type() != tlv::AdditionalDescription) {
125+
NDN_THROW(Error("AdditionalDescription", wire.type()));
128126
}
129-
130127
m_wire = wire;
131128
m_wire.parse();
132129

133-
if (m_wire.type() != tlv::AdditionalDescription)
134-
NDN_THROW(Error("AdditionalDescription", m_wire.type()));
135-
136-
auto it = m_wire.elements_begin();
137-
while (it != m_wire.elements_end()) {
138-
const Block& entry = *it;
139-
entry.parse();
140-
141-
if (entry.type() != tlv::DescriptionEntry)
142-
NDN_THROW(Error("DescriptionEntry", entry.type()));
143-
144-
if (entry.elements_size() != 2)
145-
NDN_THROW(Error("DescriptionEntry does not have two sub-TLVs"));
146-
147-
if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
148-
entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
149-
NDN_THROW(Error("Invalid DescriptionKey or DescriptionValue field"));
150-
151-
m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
152-
it++;
130+
m_info.clear();
131+
132+
for (const auto& e : m_wire.elements()) {
133+
switch (e.type()) {
134+
case tlv::DescriptionEntry:
135+
e.parse();
136+
if (e.elements_size() != 2) {
137+
NDN_THROW(Error("DescriptionEntry does not have two sub-elements"));
138+
}
139+
if (e.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
140+
e.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue) {
141+
NDN_THROW(Error("Missing DescriptionKey or DescriptionValue field"));
142+
}
143+
m_info.insert_or_assign(readString(e.elements()[KEY_OFFSET]),
144+
readString(e.elements()[VALUE_OFFSET]));
145+
break;
146+
default: // unrecognized element
147+
// if the TLV-TYPE is critical, abort decoding
148+
if (tlv::isCriticalType(e.type())) {
149+
NDN_THROW(Error("Unrecognized element of critical type " + std::to_string(e.type())));
150+
}
151+
// otherwise, ignore it
152+
break;
153+
}
153154
}
154155
}
155156

0 commit comments

Comments
 (0)