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/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 {