encoding: don't call memcmp() with null pointer
It's undefined behavior.
Change-Id: Id2fd5cbc0483af2bf9857e81fb6cac8163da3474
Refs: #4368
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index 13b10bc..691bbdf 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -28,6 +28,7 @@
#include <boost/asio/buffer.hpp>
#include <boost/range/adaptor/reversed.hpp>
+#include <cstring>
namespace ndn {
@@ -504,7 +505,8 @@
{
return lhs.type() == rhs.type() &&
lhs.value_size() == rhs.value_size() &&
- ::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0;
+ (lhs.value_size() == 0 ||
+ std::memcmp(lhs.value(), rhs.value(), lhs.value_size()) == 0);
}
} // namespace ndn
diff --git a/src/name-component.cpp b/src/name-component.cpp
index 5cf31aa..36a01f9 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -31,6 +31,7 @@
#include "util/string-helper.hpp"
#include <boost/algorithm/string/trim.hpp>
+#include <cstring>
#include <sstream>
namespace ndn {
@@ -387,7 +388,7 @@
{
return type() == other.type() &&
value_size() == other.value_size() &&
- (empty() || // needed on OSX 10.9 due to STL bug
+ (empty() || // needed with Apple clang < 9.0.0 due to libc++ bug
std::equal(value_begin(), value_end(), other.value_begin()));
}
@@ -410,7 +411,7 @@
if (cmpSize != 0)
return cmpSize;
- if (empty()) // needed on OSX 10.9 due to STL bug
+ if (empty())
return 0;
return std::memcmp(value(), other.value(), value_size());
diff --git a/tests/unit-tests/name-component.t.cpp b/tests/unit-tests/name-component.t.cpp
index 651b160..d91ce24 100644
--- a/tests/unit-tests/name-component.t.cpp
+++ b/tests/unit-tests/name-component.t.cpp
@@ -91,11 +91,19 @@
BOOST_AUTO_TEST_CASE(Generic)
{
+ name::Component empty;
name::Component compD("D");
name::Component compD2("D");
name::Component compF("F");
name::Component compAA("AA");
+ BOOST_CHECK_EQUAL(empty == empty, true);
+ BOOST_CHECK_EQUAL(empty != empty, false);
+ BOOST_CHECK_EQUAL(empty < empty, false);
+ BOOST_CHECK_EQUAL(empty <= empty, true);
+ BOOST_CHECK_EQUAL(empty == compD, false);
+ BOOST_CHECK_EQUAL(empty < compD, true);
+
BOOST_CHECK_EQUAL(compD == compD2, true);
BOOST_CHECK_EQUAL(compD != compD2, false);
BOOST_CHECK_EQUAL(compD < compD2, false);