mgmt: check enum range during decoding
This commit also makes use of prependStringBlock and readString
functions in encode/decode routines.
refs #3974
Change-Id: I86a4f16ea6f50fffeff72c8b416878740e65ef2a
diff --git a/src/encoding/block-helpers.hpp b/src/encoding/block-helpers.hpp
index f1e14ff..5634ed6 100644
--- a/src/encoding/block-helpers.hpp
+++ b/src/encoding/block-helpers.hpp
@@ -63,6 +63,39 @@
uint64_t
readNonNegativeInteger(const Block& block);
+/** @brief Read a non-negative integer from a TLV element and cast to the specified type
+ * @tparam R result type, must be an integral type
+ * @param block the TLV element
+ * @throw tlv::Error block does not contain a valid non-negative integer or the number cannot be
+ * represented in R
+ */
+template<typename R>
+typename std::enable_if<std::is_integral<R>::value, R>::type
+readNonNegativeIntegerAs(const Block& block)
+{
+ uint64_t value = readNonNegativeInteger(block);
+ if (value > std::numeric_limits<R>::max()) {
+ BOOST_THROW_EXCEPTION(tlv::Error("Value in TLV element of type " + to_string(block.type()) +
+ " is too large"));
+ }
+ return static_cast<R>(value);
+}
+
+/** @brief Read a non-negative integer from a TLV element and cast to the specified type
+ * @tparam R result type, must be an enumeration type
+ * @param block the TLV element
+ * @throw tlv::Error block does not contain a valid non-negative integer or the number cannot be
+ * represented in R
+ * @warning If R is an unscoped enum type, it must have a fixed underlying type. Otherwise, this
+ * function may trigger unspecified behavior.
+ */
+template<typename R>
+typename std::enable_if<std::is_enum<R>::value, R>::type
+readNonNegativeIntegerAs(const Block& block)
+{
+ return static_cast<R>(readNonNegativeIntegerAs<typename std::underlying_type<R>::type>(block));
+}
+
/** @brief Prepend an empty TLV element
* @param encoder an EncodingBuffer or EncodingEstimator
* @param type TLV-TYPE number
@@ -255,6 +288,7 @@
using encoding::makeNonNegativeIntegerBlock;
using encoding::readNonNegativeInteger;
+using encoding::readNonNegativeIntegerAs;
using encoding::makeEmptyBlock;
using encoding::makeStringBlock;
using encoding::readString;
diff --git a/src/mgmt/control-response.cpp b/src/mgmt/control-response.cpp
index a0c74c2..9d82d40 100644
--- a/src/mgmt/control-response.cpp
+++ b/src/mgmt/control-response.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -56,8 +56,7 @@
m_wire = Block(tlv::nfd::ControlResponse);
m_wire.push_back(makeNonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code));
-
- m_wire.push_back(makeBinaryBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size()));
+ m_wire.push_back(makeStringBlock(tlv::nfd::StatusText, m_text));
if (m_body.hasWire()) {
m_wire.push_back(m_body);
@@ -74,20 +73,19 @@
m_wire.parse();
if (m_wire.type() != tlv::nfd::ControlResponse)
- throw Error("Requested decoding of ControlResponse, but Block is of different type");
+ BOOST_THROW_EXCEPTION(Error("expected ControlResponse, got " + to_string(m_wire.type()) + " element"));
Block::element_const_iterator val = m_wire.elements_begin();
if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) {
- throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)");
+ BOOST_THROW_EXCEPTION(Error("missing StatusCode sub-element"));
}
-
m_code = readNonNegativeInteger(*val);
++val;
if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) {
- throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)");
+ BOOST_THROW_EXCEPTION(Error("missing StatusText sub-element"));
}
- m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_text = readString(*val);
++val;
if (val != m_wire.elements_end())
diff --git a/src/mgmt/nfd/channel-status.cpp b/src/mgmt/nfd/channel-status.cpp
index f3a7e2b..e6e53f4 100644
--- a/src/mgmt/nfd/channel-status.cpp
+++ b/src/mgmt/nfd/channel-status.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -20,7 +20,7 @@
*/
#include "channel-status.hpp"
-#include "encoding/encoding-buffer.hpp"
+#include "encoding/block-helpers.hpp"
#include "encoding/tlv-nfd.hpp"
#include "util/concepts.hpp"
@@ -41,10 +41,7 @@
ChannelStatus::wireEncode(EncodingImpl<TAG>& encoder) const
{
size_t totalLength = 0;
-
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
- reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
-
+ totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
totalLength += encoder.prependVarNumber(totalLength);
totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus);
return totalLength;
@@ -83,7 +80,7 @@
Block::element_const_iterator val = m_wire.elements_begin();
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
- m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_localUri = readString(*val);
++val;
}
else {
diff --git a/src/mgmt/nfd/control-parameters.cpp b/src/mgmt/nfd/control-parameters.cpp
index bc18f13..465482f 100644
--- a/src/mgmt/nfd/control-parameters.cpp
+++ b/src/mgmt/nfd/control-parameters.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -20,8 +20,8 @@
*/
#include "control-parameters.hpp"
-#include "encoding/tlv-nfd.hpp"
#include "encoding/block-helpers.hpp"
+#include "encoding/tlv-nfd.hpp"
#include "util/concepts.hpp"
#include "util/string-helper.hpp"
@@ -75,18 +75,10 @@
totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
}
if (this->hasLocalUri()) {
- size_t valLength = encoder.prependByteArray(
- reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
- totalLength += valLength;
- totalLength += encoder.prependVarNumber(valLength);
- totalLength += encoder.prependVarNumber(tlv::nfd::LocalUri);
+ totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
}
if (this->hasUri()) {
- size_t valLength = encoder.prependByteArray(
- reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
- totalLength += valLength;
- totalLength += encoder.prependVarNumber(valLength);
- totalLength += encoder.prependVarNumber(tlv::nfd::Uri);
+ totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
}
if (this->hasFaceId()) {
totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
@@ -141,43 +133,43 @@
val = m_wire.find(tlv::nfd::FaceId);
m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
if (this->hasFaceId()) {
- m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_faceId = readNonNegativeInteger(*val);
}
val = m_wire.find(tlv::nfd::Uri);
m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
if (this->hasUri()) {
- m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_uri = readString(*val);
}
val = m_wire.find(tlv::nfd::LocalUri);
m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
if (this->hasLocalUri()) {
- m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_localUri = readString(*val);
}
val = m_wire.find(tlv::nfd::Origin);
m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
if (this->hasOrigin()) {
- m_origin = static_cast<RouteOrigin>(readNonNegativeInteger(*val));
+ m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
}
val = m_wire.find(tlv::nfd::Cost);
m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
if (this->hasCost()) {
- m_cost = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_cost = readNonNegativeInteger(*val);
}
val = m_wire.find(tlv::nfd::Flags);
m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
if (this->hasFlags()) {
- m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_flags = readNonNegativeInteger(*val);
}
val = m_wire.find(tlv::nfd::Mask);
m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
if (this->hasMask()) {
- m_mask = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_mask = readNonNegativeInteger(*val);
}
val = m_wire.find(tlv::nfd::Strategy);
@@ -201,7 +193,7 @@
val = m_wire.find(tlv::nfd::FacePersistency);
m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
if (this->hasFacePersistency()) {
- m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
+ m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
}
}
diff --git a/src/mgmt/nfd/face-event-notification.cpp b/src/mgmt/nfd/face-event-notification.cpp
index 888fe61..79a94f2 100644
--- a/src/mgmt/nfd/face-event-notification.cpp
+++ b/src/mgmt/nfd/face-event-notification.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -47,22 +47,14 @@
{
size_t totalLength = 0;
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::Flags, m_flags);
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::LinkType, m_linkType);
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::FacePersistency, m_facePersistency);
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::FaceScope, m_faceScope);
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
- reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
- reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::FaceId, m_faceId);
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::FaceEventKind, m_kind);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::LinkType, m_linkType);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceScope, m_faceScope);
+ totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
+ totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceEventKind, m_kind);
totalLength += encoder.prependVarNumber(totalLength);
totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
@@ -102,7 +94,7 @@
Block::element_const_iterator val = m_wire.elements_begin();
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
- m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
+ m_kind = readNonNegativeIntegerAs<FaceEventKind>(*val);
++val;
}
else {
@@ -118,7 +110,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
- m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_remoteUri = readString(*val);
++val;
}
else {
@@ -126,7 +118,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
- m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_localUri = readString(*val);
++val;
}
else {
@@ -134,7 +126,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
- m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
+ m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
++val;
}
else {
@@ -142,7 +134,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
- m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
+ m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
++val;
}
else {
@@ -150,7 +142,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
- m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
+ m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
++val;
}
else {
diff --git a/src/mgmt/nfd/face-query-filter.cpp b/src/mgmt/nfd/face-query-filter.cpp
index 36a1206..6bde1d4 100644
--- a/src/mgmt/nfd/face-query-filter.cpp
+++ b/src/mgmt/nfd/face-query-filter.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -20,8 +20,8 @@
*/
#include "face-query-filter.hpp"
-#include "encoding/tlv-nfd.hpp"
#include "encoding/block-helpers.hpp"
+#include "encoding/tlv-nfd.hpp"
#include "util/concepts.hpp"
namespace ndn {
@@ -62,18 +62,15 @@
}
if (hasLocalUri()) {
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
- reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
+ totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
}
if (hasRemoteUri()) {
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
- reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
+ totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
}
if (hasUriScheme()) {
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::UriScheme,
- reinterpret_cast<const uint8_t*>(m_uriScheme.data()), m_uriScheme.size());
+ totalLength += prependStringBlock(encoder, tlv::nfd::UriScheme, m_uriScheme);
}
if (m_faceId) {
@@ -129,7 +126,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::UriScheme) {
- m_uriScheme.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_uriScheme = readString(*val);
++val;
}
else {
@@ -137,7 +134,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
- m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_remoteUri = readString(*val);
++val;
}
else {
@@ -145,7 +142,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
- m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_localUri = readString(*val);
++val;
}
else {
@@ -153,7 +150,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
- m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
+ m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
++val;
}
else {
@@ -161,7 +158,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
- m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
+ m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
++val;
}
else {
@@ -169,7 +166,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
- m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
+ m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
++val;
}
else {
diff --git a/src/mgmt/nfd/face-status.cpp b/src/mgmt/nfd/face-status.cpp
index 3361d99..1cfc741 100644
--- a/src/mgmt/nfd/face-status.cpp
+++ b/src/mgmt/nfd/face-status.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -67,15 +67,12 @@
totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceScope, m_faceScope);
if (m_expirationPeriod) {
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::ExpirationPeriod, static_cast<uint64_t>(m_expirationPeriod->count()));
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::ExpirationPeriod,
+ m_expirationPeriod->count());
}
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
- reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
- reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
- totalLength += prependNonNegativeIntegerBlock(encoder,
- tlv::nfd::FaceId, m_faceId);
+ totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
+ totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
totalLength += encoder.prependVarNumber(totalLength);
totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
@@ -123,7 +120,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
- m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_remoteUri = readString(*val);
++val;
}
else {
@@ -131,7 +128,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
- m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_localUri = readString(*val);
++val;
}
else {
@@ -147,7 +144,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
- m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
+ m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
++val;
}
else {
@@ -155,7 +152,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
- m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
+ m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
++val;
}
else {
@@ -163,7 +160,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
- m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
+ m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
++val;
}
else {
diff --git a/src/mgmt/nfd/forwarder-status.cpp b/src/mgmt/nfd/forwarder-status.cpp
index 06c8f1f..50eac63 100644
--- a/src/mgmt/nfd/forwarder-status.cpp
+++ b/src/mgmt/nfd/forwarder-status.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -71,9 +71,7 @@
time::toUnixTimestamp(m_currentTimestamp).count());
totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp,
time::toUnixTimestamp(m_startTimestamp).count());
- totalLength += encoder.prependByteArrayBlock(tlv::nfd::NfdVersion,
- reinterpret_cast<const uint8_t*>(m_nfdVersion.data()),
- m_nfdVersion.size());
+ totalLength += prependStringBlock(encoder, tlv::nfd::NfdVersion, m_nfdVersion);
totalLength += encoder.prependVarNumber(totalLength);
totalLength += encoder.prependVarNumber(tlv::Content);
@@ -113,7 +111,7 @@
Block::element_const_iterator val = m_wire.elements_begin();
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
- m_nfdVersion.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ m_nfdVersion = readString(*val);
++val;
}
else {
@@ -137,7 +135,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
- m_nNameTreeEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+ m_nNameTreeEntries = readNonNegativeIntegerAs<size_t>(*val);
++val;
}
else {
@@ -145,7 +143,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
- m_nFibEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+ m_nFibEntries = readNonNegativeIntegerAs<size_t>(*val);
++val;
}
else {
@@ -153,7 +151,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
- m_nPitEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+ m_nPitEntries = readNonNegativeIntegerAs<size_t>(*val);
++val;
}
else {
@@ -161,7 +159,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
- m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+ m_nMeasurementsEntries = readNonNegativeIntegerAs<size_t>(*val);
++val;
}
else {
@@ -169,7 +167,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
- m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
+ m_nCsEntries = readNonNegativeIntegerAs<size_t>(*val);
++val;
}
else {
@@ -177,7 +175,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
- m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_nInInterests = readNonNegativeInteger(*val);
++val;
}
else {
@@ -185,7 +183,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
- m_nInData = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_nInData = readNonNegativeInteger(*val);
++val;
}
else {
@@ -193,7 +191,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
- m_nInNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_nInNacks = readNonNegativeInteger(*val);
++val;
}
else {
@@ -201,7 +199,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
- m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_nOutInterests = readNonNegativeInteger(*val);
++val;
}
else {
@@ -209,7 +207,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
- m_nOutData = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_nOutData = readNonNegativeInteger(*val);
++val;
}
else {
@@ -217,7 +215,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
- m_nOutNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
+ m_nOutNacks = readNonNegativeInteger(*val);
++val;
}
else {
diff --git a/src/mgmt/nfd/rib-entry.cpp b/src/mgmt/nfd/rib-entry.cpp
index 619fd5e..475af15 100644
--- a/src/mgmt/nfd/rib-entry.cpp
+++ b/src/mgmt/nfd/rib-entry.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -159,7 +159,7 @@
}
if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) {
- m_origin = static_cast<RouteOrigin>(readNonNegativeInteger(*val));
+ m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
++val;
}
else {
diff --git a/tests/unit-tests/encoding/block-helpers.t.cpp b/tests/unit-tests/encoding/block-helpers.t.cpp
index c4afa40..47226b3 100644
--- a/tests/unit-tests/encoding/block-helpers.t.cpp
+++ b/tests/unit-tests/encoding/block-helpers.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -31,6 +31,26 @@
BOOST_AUTO_TEST_SUITE(Encoding)
BOOST_AUTO_TEST_SUITE(TestBlockHelpers)
+enum E8 : uint8_t
+{
+ E8_NONE
+};
+
+enum class EC8 : uint8_t
+{
+ NONE
+};
+
+enum E16 : uint16_t
+{
+ E16_NONE
+};
+
+enum class EC16 : uint16_t
+{
+ NONE
+};
+
BOOST_AUTO_TEST_CASE(NonNegativeInteger)
{
Block b = makeNonNegativeIntegerBlock(100, 1000);
@@ -39,6 +59,14 @@
BOOST_CHECK_EQUAL(readNonNegativeInteger(b), 1000);
BOOST_CHECK_THROW(readNonNegativeInteger(Block()), tlv::Error);
+
+ BOOST_CHECK_THROW(readNonNegativeIntegerAs<uint8_t>(b), tlv::Error);
+ BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<uint16_t>(b), 1000);
+ BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<size_t>(b), 1000);
+ BOOST_CHECK_THROW(readNonNegativeIntegerAs<E8>(b), tlv::Error);
+ BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<E16>(b)), 1000);
+ BOOST_CHECK_THROW(readNonNegativeIntegerAs<EC8>(b), tlv::Error);
+ BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<EC16>(b)), 1000);
}
BOOST_AUTO_TEST_CASE(Empty)