From 1d098c1f50ed51470984cef87df46a64a4475634 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 1 Jun 2019 12:33:24 +0200 Subject: [PATCH] Fix TagValue::operator== if 0 byte contained --- tagvalue.cpp | 22 +++++++++++++++++++--- tests/tagvalue.cpp | 8 ++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tagvalue.cpp b/tagvalue.cpp index b12b718..6cfecee 100644 --- a/tagvalue.cpp +++ b/tagvalue.cpp @@ -117,11 +117,19 @@ bool TagValue::operator==(const TagValue &other) const if (m_type == other.m_type) { switch (m_type) { case TagDataType::Text: - if (m_size != other.m_size && m_encoding != other.m_encoding) { + if (m_size != other.m_size || m_encoding != other.m_encoding) { // don't consider differently encoded text values equal return false; } - return strncmp(m_ptr.get(), other.m_ptr.get(), m_size) == 0; + if (!m_size) { + return true; + } + for (auto i1 = m_ptr.get(), i2 = other.m_ptr.get(), end = m_ptr.get() + m_size; i1 != end; ++i1, ++i2) { + if (*i1 != *i2) { + return false; + } + } + return true; case TagDataType::PositionInSet: return toPositionInSet() == other.toPositionInSet(); case TagDataType::Integer: @@ -138,7 +146,15 @@ bool TagValue::operator==(const TagValue &other) const if (m_size != other.m_size) { return false; } - return strncmp(m_ptr.get(), other.m_ptr.get(), m_size) == 0; + if (!m_size) { + return true; + } + for (auto i1 = m_ptr.get(), i2 = other.m_ptr.get(), end = m_ptr.get() + m_size; i1 != end; ++i1, ++i2) { + if (*i1 != *i2) { + return false; + } + } + return true; } return false; } diff --git a/tests/tagvalue.cpp b/tests/tagvalue.cpp index 2f23bc1..1b670db 100644 --- a/tests/tagvalue.cpp +++ b/tests/tagvalue.cpp @@ -195,6 +195,14 @@ void TagValueTests::testEqualityOperator() CPPUNIT_ASSERT_MESSAGE("equality requires identical types or identical string representation"s, TagValue(0) != TagValue::empty()); CPPUNIT_ASSERT_EQUAL_MESSAGE("types might differ"s, TagValue(15), TagValue(15)); CPPUNIT_ASSERT_EQUAL_MESSAGE("types might differ"s, TagValue("15", 2, TagTextEncoding::Latin1), TagValue(15)); + CPPUNIT_ASSERT_EQUAL_MESSAGE("comparision of equal UTF-16 strings"s, TagValue("\x31\0\x32\0", 4, TagTextEncoding::Utf16LittleEndian), + TagValue("\x31\0\x32\0", 4, TagTextEncoding::Utf16LittleEndian)); + CPPUNIT_ASSERT_MESSAGE("comparision of different UTF-16 strings"s, + TagValue("\x31\0\x33\0", 4, TagTextEncoding::Utf16LittleEndian) != TagValue("\x31\0\x32\0", 4, TagTextEncoding::Utf16LittleEndian)); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "comparision of equal binary data"s, TagValue("\x31\0\x32\0", 4, TagDataType::Binary), TagValue("\x31\0\x32\0", 4, TagDataType::Binary)); + CPPUNIT_ASSERT_MESSAGE("comparision of different binary data"s, + TagValue("\x31\0\x33\0", 4, TagDataType::Binary) != TagValue("\x31\0\x32\0", 4, TagDataType::Binary)); CPPUNIT_ASSERT_MESSAGE("encoding must be equal if relevant for types"s, TagValue("\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian) != TagValue("15", 2, TagTextEncoding::Latin1)); CPPUNIT_ASSERT_EQUAL_MESSAGE(