From 3d3bc94e392b09e21520a059cb1f45bf237bae65 Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 13 Feb 2019 20:19:46 +0100 Subject: [PATCH] Prevent warning "Cannot make an empty frame" --- exceptions.cpp | 31 ++++++++++++++++++++++++++++++- exceptions.h | 7 +++++++ id3/id3v2frame.cpp | 37 ++++++++++++++++++++----------------- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/exceptions.cpp b/exceptions.cpp index 5492daf..06bc98c 100644 --- a/exceptions.cpp +++ b/exceptions.cpp @@ -35,7 +35,8 @@ const char *Failure::what() const USE_NOTHROW /*! * \class TagParser::NoDataFoundException * \brief The exception that is thrown when the data to be parsed holds no - * parsable information. + * parsable information (e.g. relevant section in the file does not exist or + * has size of zero). */ /*! @@ -88,6 +89,34 @@ const char *InvalidDataException::what() const USE_NOTHROW return "data to be parsed or to be made seems to be invalid"; } +/*! + * \class TagParser::NoDataException + * \brief The exception that is thrown when the value to be written is empty but that + * is not allowed in that context (e.g. an empty ID3v2 frame is not allowed). + */ + +/*! + * \brief Constructs a new exception. + */ +NoDataProvidedException::NoDataProvidedException() USE_NOTHROW +{ +} + +/*! + * \brief Destroys the exception. + */ +NoDataProvidedException::~NoDataProvidedException() USE_NOTHROW +{ +} + +/*! + * \brief Returns a C-style character string describing the cause of the exception. + */ +const char *NoDataProvidedException::what() const USE_NOTHROW +{ + return "can not write empty value"; +} + /*! * \class TagParser::TruncatedDataException * \brief The exception that is thrown when the data to be parsed is truncated diff --git a/exceptions.h b/exceptions.h index f135a86..a23e003 100644 --- a/exceptions.h +++ b/exceptions.h @@ -29,6 +29,13 @@ public: virtual const char *what() const USE_NOTHROW; }; +class TAG_PARSER_EXPORT NoDataProvidedException : public Failure { +public: + NoDataProvidedException() USE_NOTHROW; + virtual ~NoDataProvidedException() USE_NOTHROW; + virtual const char *what() const USE_NOTHROW; +}; + class TAG_PARSER_EXPORT TruncatedDataException : public InvalidDataException { public: TruncatedDataException() USE_NOTHROW; diff --git a/id3/id3v2frame.cpp b/id3/id3v2frame.cpp index 44b1bc3..ad47ebf 100644 --- a/id3/id3v2frame.cpp +++ b/id3/id3v2frame.cpp @@ -430,23 +430,7 @@ Id3v2FrameMaker::Id3v2FrameMaker(Id3v2Frame &frame, byte version, Diagnostics &d { const string context("making " % m_frame.idToString() + " frame"); - // get non-empty, assigned values - vector values; - values.reserve(1 + frame.additionalValues().size()); - if (!frame.value().isEmpty()) { - values.emplace_back(&frame.value()); - } - for (const auto &value : frame.additionalValues()) { - if (!value.isEmpty()) { - values.emplace_back(&value); - } - } - - // validate assigned data - if (values.empty()) { - diag.emplace_back(DiagLevel::Critical, "Cannot make an empty frame.", context); - throw InvalidDataException(); - } + // validate frame's configuration if (m_frame.isEncrypted()) { diag.emplace_back(DiagLevel::Critical, "Cannot make an encrypted frame (isn't supported by this tagging library).", context); throw InvalidDataException(); @@ -462,6 +446,25 @@ Id3v2FrameMaker::Id3v2FrameMaker(Id3v2Frame &frame, byte version, Diagnostics &d diag.emplace_back(DiagLevel::Warning, "The existing flag and group information is not supported by the version of ID3v2 and will be ignored/discarted.", context); } + + // get non-empty, assigned values + vector values; + values.reserve(1 + frame.additionalValues().size()); + if (!frame.value().isEmpty()) { + values.emplace_back(&frame.value()); + } + for (const auto &value : frame.additionalValues()) { + if (!value.isEmpty()) { + values.emplace_back(&value); + } + } + + // validate assigned values + if (values.empty()) { + throw NoDataProvidedException(); + // note: This is not really an issue because in the case we're not provided with any value here just means that the field + // is supposed to be removed. So don't add any diagnostic messages here. + } const bool isTextFrame = Id3v2FrameIds::isTextFrame(m_frameId); if (values.size() != 1) { if (!isTextFrame) {