Ensure to handle all conversion errors in TagValue's equality operator

For instance, if a DateTime of wrong size is assigned this would lead to a
conversion error which would otherwise be unhandled.
This commit is contained in:
Martchus 2022-06-18 14:40:53 +02:00
parent 46014def51
commit aa4b8a8e47
1 changed files with 80 additions and 74 deletions

View File

@ -227,6 +227,7 @@ bool TagValue::compareTo(const TagValue &other, TagValueComparisionFlags options
}
}
try {
// check for equality if both types are identical
if (m_type == other.m_type) {
switch (m_type) {
@ -235,7 +236,8 @@ bool TagValue::compareTo(const TagValue &other, TagValueComparisionFlags options
if (m_size != other.m_size && m_encoding == other.m_encoding) {
return false;
}
if (m_encoding == other.m_encoding || m_encoding == TagTextEncoding::Unspecified || other.m_encoding == TagTextEncoding::Unspecified) {
if (m_encoding == other.m_encoding || m_encoding == TagTextEncoding::Unspecified
|| other.m_encoding == TagTextEncoding::Unspecified) {
return compareData(other, options & TagValueComparisionFlags::CaseInsensitive);
}
@ -287,24 +289,28 @@ bool TagValue::compareTo(const TagValue &other, TagValueComparisionFlags options
return false;
}
// check for equality if types are different by comparing the string representation (if that makes sense)
// handle certain types
if (m_type == TagDataType::Undefined || other.m_type == TagDataType::Undefined) {
return false;
} else if (m_type == TagDataType::Popularity || other.m_type == TagDataType::Popularity) {
return toPopularity() == other.toPopularity();
}
// do not attempt to convert certain types to string because it will always fail anyways
for (const auto dataType : { m_type, other.m_type }) {
switch (dataType) {
case TagDataType::TimeSpan:
case TagDataType::DateTime:
case TagDataType::Picture:
case TagDataType::Binary:
case TagDataType::Undefined:
// do not attempt to convert these types to string because it will always fail anyways
return false;
default:;
}
}
try {
if (m_type == TagDataType::Popularity || other.m_type == TagDataType::Popularity) {
return toPopularity() == other.toPopularity();
}
// check for equality if types are different by comparing the string representation (if that makes sense)
return compareData(toString(), other.toString(m_encoding), options & TagValueComparisionFlags::CaseInsensitive);
} catch (const ConversionException &) {
return false;
}