Parcourir la source

[2443] Skip checks in LabelSequence when building in non-debug mode

Mukund Sivaraman il y a 11 ans
Parent
commit
60a4f25645
2 fichiers modifiés avec 20 ajouts et 1 suppressions
  1. 14 1
      src/lib/dns/labelsequence.cc
  2. 6 0
      src/lib/dns/tests/labelsequence_unittest.cc

+ 14 - 1
src/lib/dns/labelsequence.cc

@@ -12,6 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
+#include "config.h"
+
 #include <dns/labelsequence.h>
 #include <dns/name_internal.h>
 #include <exceptions/exceptions.h>
@@ -24,24 +26,34 @@ namespace isc {
 namespace dns {
 
 LabelSequence::LabelSequence(const void* buf) {
+#ifdef ENABLE_DEBUG
+    // In non-debug mode, derefencing the NULL pointer further below
+    // will lead to a crash, so disabling this check is not
+    // unsafe. Except for a programming mistake, this case should not
+    // happen.
     if (buf == NULL) {
         isc_throw(BadValue,
                   "Null pointer passed to LabelSequence constructor");
     }
+#endif
 
     const uint8_t* bp = reinterpret_cast<const uint8_t*>(buf);
-
     first_label_ = 0;
     const uint8_t offsets_len = *bp++;
+
+#ifdef ENABLE_DEBUG
     if (offsets_len == 0 || offsets_len > Name::MAX_LABELS) {
         isc_throw(BadValue,
                   "Bad offsets len in serialized LabelSequence data: "
                   << static_cast<unsigned int>(offsets_len));
     }
+#endif
+
     last_label_ = offsets_len - 1;
     offsets_ = bp;
     data_ = bp + offsets_len;
 
+#ifdef ENABLE_DEBUG
     // Check the integrity on the offsets and the name data
     const uint8_t* dp = data_;
     for (size_t cur_offset = 0; cur_offset < offsets_len; ++cur_offset) {
@@ -52,6 +64,7 @@ LabelSequence::LabelSequence(const void* buf) {
         }
         dp += (1 + *dp);
     }
+#endif
 }
 
 LabelSequence::LabelSequence(const LabelSequence& src,

+ 6 - 0
src/lib/dns/tests/labelsequence_unittest.cc

@@ -853,6 +853,10 @@ TEST_F(LabelSequenceTest, serialize) {
                  isc::BadValue);
 }
 
+#ifdef ENABLE_DEBUG
+
+// These checks are enabled only in debug mode in the LabelSequence
+// class.
 TEST_F(LabelSequenceTest, badDeserialize) {
     EXPECT_THROW(LabelSequence(NULL), isc::BadValue);
     const uint8_t zero_offsets[] = { 0 };
@@ -879,6 +883,8 @@ TEST_F(LabelSequenceTest, badDeserialize) {
     EXPECT_THROW(LabelSequence ls(offsets_noincrease), isc::BadValue);
 }
 
+#endif
+
 namespace {
 
 // Helper function; repeatedly calls