BIND 10 trac2086, updated. 8b731b4250b47f8e4ef6367609dce21a221aee82 [2086] Add basic checking of offsets data
BIND 10 source code commits
bind10-changes at lists.isc.org
Thu Jul 12 11:14:57 UTC 2012
The branch, trac2086 has been updated
via 8b731b4250b47f8e4ef6367609dce21a221aee82 (commit)
via 3198b650ccc2be44c93a380c3db4d5fe6429e940 (commit)
from 6498dfa5915fbca86d839b73490320360111e24e (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 8b731b4250b47f8e4ef6367609dce21a221aee82
Author: Jelte Jansen <jelte at isc.org>
Date: Thu Jul 12 13:14:40 2012 +0200
[2086] Add basic checking of offsets data
commit 3198b650ccc2be44c93a380c3db4d5fe6429e940
Author: Jelte Jansen <jelte at isc.org>
Date: Thu Jul 12 12:35:54 2012 +0200
[2086] Address review comments
- style fix in return value
- updated header documentation
- added common label count checks to tests
-----------------------------------------------------------------------
Summary of changes:
src/lib/dns/labelsequence.cc | 31 ++++++++++++++++++++-
src/lib/dns/labelsequence.h | 39 +++++++++++++--------------
src/lib/dns/tests/labelsequence_unittest.cc | 28 +++++++++++++++++++
3 files changed, 76 insertions(+), 22 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/lib/dns/labelsequence.cc b/src/lib/dns/labelsequence.cc
index 6e778f4..f9a0bd9 100644
--- a/src/lib/dns/labelsequence.cc
+++ b/src/lib/dns/labelsequence.cc
@@ -23,10 +23,39 @@
namespace isc {
namespace dns {
+LabelSequence::LabelSequence(const uint8_t* data,
+ const uint8_t* offsets,
+ size_t offsets_size) : data_(data),
+ offsets_(offsets),
+ offsets_size_(offsets_size),
+ first_label_(0),
+ last_label_(offsets_size_)
+{
+ // Check offsets
+ if (data == NULL || offsets == NULL) {
+ isc_throw(BadValue, "Null pointer passed to LabelSequence constructor");
+ }
+ if (offsets_size == 0) {
+ isc_throw(BadValue, "Zero offsets to LabelSequence constructor");
+ }
+ if (offsets_size > Name::MAX_LABELS) {
+ isc_throw(BadValue, "MAX_LABELS exceeded");
+ }
+ for (size_t cur_offset = 0; cur_offset < offsets_size; ++cur_offset) {
+ if (offsets[cur_offset] > Name::MAX_LABELLEN) {
+ isc_throw(BadValue, "MAX_LABEL_LEN exceeded");
+ }
+ if (cur_offset > 0 && offsets[cur_offset] <= offsets[cur_offset - 1]) {
+ isc_throw(BadValue, "Offset smaller than previous offset");
+ }
+ }
+}
+
+
const uint8_t*
LabelSequence::getData(size_t *len) const {
*len = getDataLength();
- return &(data_[offsets_[first_label_]]);
+ return (&data_[offsets_[first_label_]]);
}
size_t
diff --git a/src/lib/dns/labelsequence.h b/src/lib/dns/labelsequence.h
index c2149f0..cf1aeea 100644
--- a/src/lib/dns/labelsequence.h
+++ b/src/lib/dns/labelsequence.h
@@ -21,24 +21,25 @@
namespace isc {
namespace dns {
-/// \brief Light-weight Accessor to Name object
+/// \brief Light-weight Accessor to data of Name object
///
/// The purpose of this class is to easily match Names and parts of Names,
/// without needing to copy the underlying data on each label strip.
///
-/// It can only work on existing Name objects, and the Name object MUST
+/// It can only work on existing Name objects, or data as provided by the
+/// Name object or another LabelSequence, and the data or Name MUST
/// remain in scope during the entire lifetime of its associated
/// LabelSequence(s).
///
/// Upon creation of a LabelSequence, it records the offsets of the
/// labels in the wireformat data of the Name. When stripLeft() or
/// stripRight() is called on the LabelSequence, no changes in the
-/// Name's data occur, but the internal pointers of the
+/// original data occur, but the internal pointers of the
/// LabelSequence are modified.
///
/// LabelSequences can be compared to other LabelSequences, and their
/// data can be requested (which then points to part of the original
-/// data of the associated Name object).
+/// data of the original Name object).
///
class LabelSequence {
// Name calls the private toText(bool) method of LabelSequence.
@@ -69,27 +70,23 @@ public:
/// \note No validation is done on the given data upon construction;
/// use with care.
///
- /// \param data The Name data, in wire format
- /// \param offsets The offsets of the labels in the Name, as given
- /// by the Name object or another LabelSequence
+ /// \param data The raw data for the domain name, in wire format
+ /// \param offsets The offsets of the labels in the domain name data,
+ /// as given by a Name object or another LabelSequence
/// \param offsets_size The size of the offsets data
LabelSequence(const uint8_t* data,
const uint8_t* offsets,
- size_t offsets_size) : data_(data),
- offsets_(offsets),
- offsets_size_(offsets_size),
- first_label_(0),
- last_label_(offsets_size_)
- {}
+ size_t offsets_size);
/// \brief Return the wire-format data for this LabelSequence
///
- /// The data, is returned as a pointer to the original wireformat
- /// data of the original Name object, and the given len value is
+ /// The data is returned as a pointer to (the part of) the original
+ /// wireformat data, from either the original Name object, or the
+ /// raw data given in the constructor, and the given len value is
/// set to the number of octets that match this labelsequence.
///
/// \note The data pointed to is only valid if the original Name
- /// object is still in scope
+ /// object or data is still in scope
///
/// \param len Pointer to a size_t where the length of the data
/// will be stored (in number of octets)
@@ -107,7 +104,7 @@ public:
/// versa.
///
/// \note The data pointed to is only valid if the original Name
- /// object is still in scope
+ /// object or data is still in scope
///
/// \return The length of the data of the label sequence in octets.
size_t getDataLength() const;
@@ -149,7 +146,7 @@ public:
/// \brief Remove labels from the end of this LabelSequence
///
/// \note No actual memory is changed, this operation merely updates the
- /// internal pointers based on the offsets in the Name object.
+ /// internal pointers based on the offsets originally provided.
///
/// \exception OutOfRange if i is greater than or equal to the number
/// of labels currently pointed to by this LabelSequence
@@ -168,13 +165,13 @@ public:
/// LabelSequence as a string. The returned string ends with a dot
/// '.' if the label sequence is absolute.
///
- /// This function assumes the underlying name is in proper
+ /// This function assumes the underlying data is in proper
/// uncompressed wire format. If it finds an unexpected label
/// character including compression pointer, an exception of class
/// \c BadLabelType will be thrown. In addition, if resource
/// allocation for the result string fails, a corresponding standard
/// exception will be thrown.
- //
+ ///
/// \return a string representation of the <code>LabelSequence</code>.
std::string toText() const;
@@ -244,7 +241,7 @@ private:
///
/// \param os A \c std::ostream object on which the insertion operation is
/// performed.
-/// \param name The \c LabelSequence object output by the operation.
+/// \param label_sequence The \c LabelSequence object output by the operation.
/// \return A reference to the same \c std::ostream object referenced by
/// parameter \c os after the insertion operation.
std::ostream&
diff --git a/src/lib/dns/tests/labelsequence_unittest.cc b/src/lib/dns/tests/labelsequence_unittest.cc
index 6aca1b9..bbd52c9 100644
--- a/src/lib/dns/tests/labelsequence_unittest.cc
+++ b/src/lib/dns/tests/labelsequence_unittest.cc
@@ -707,6 +707,7 @@ TEST(LabelSequence, rawConstruction) {
result = s1.compare(s3);
EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
result.getRelation());
+ EXPECT_EQ(2, result.getCommonLabels());
s1.stripRight(1);
s3.stripRight(1);
@@ -714,11 +715,38 @@ TEST(LabelSequence, rawConstruction) {
result = s1.compare(s3);
EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
result.getRelation());
+ EXPECT_EQ(1, result.getCommonLabels());
data[9] = 'f';
result = s1.compare(s3);
EXPECT_EQ(isc::dns::NameComparisonResult::NONE,
result.getRelation());
+ EXPECT_EQ(0, result.getCommonLabels());
+}
+
+// Test with some data that exceeds limits (MAX_LABELS and MAX_LABEL_LEN)
+TEST(LabelSequence, badRawConstruction) {
+ uint8_t data[1] = { 0 };
+ uint8_t offsets[1] = { 0 };
+
+ EXPECT_THROW(LabelSequence(NULL, offsets, 1), isc::BadValue);
+ EXPECT_THROW(LabelSequence(data, NULL, 1), isc::BadValue);
+ EXPECT_THROW(LabelSequence(data, offsets, 0), isc::BadValue);
+
+ // exceed MAX_LABELS
+ EXPECT_THROW(LabelSequence(data, offsets, 127), isc::BadValue);
+
+ // exceed MAX_LABEL_LEN
+ uint8_t offsets_toolonglabel[1] = { 64 };
+ EXPECT_THROW(LabelSequence(data, offsets_toolonglabel, 1), isc::BadValue);
+
+ // Add an offset that is lower than the previous offset
+ uint8_t offsets_lower[3] = { 0, 8, 4 };
+ EXPECT_THROW(LabelSequence(data, offsets_lower, 3), isc::BadValue);
+
+ // Add an offset that is equal to the previous offset
+ uint8_t offsets_noincrease[3] = { 0, 8, 8 };
+ EXPECT_THROW(LabelSequence(data, offsets_noincrease, 3), isc::BadValue);
}
}
More information about the bind10-changes
mailing list