From afb2d998fe62f7e829e93e62506bf9968117c9c5 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 26 Jan 2023 18:07:42 -0800 Subject: [PATCH 1/5] fix copy constructors clang-tidy couldn't transform these because of missing members Found with cppcheck. Signed-off-by: Rosen Penev --- src/tiffcomposite_int.cpp | 19 ------------------- src/tiffcomposite_int.hpp | 6 +++--- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/src/tiffcomposite_int.cpp b/src/tiffcomposite_int.cpp index 1fcc55bfb5..376e795059 100644 --- a/src/tiffcomposite_int.cpp +++ b/src/tiffcomposite_int.cpp @@ -141,25 +141,6 @@ TiffEntryBase::TiffEntryBase(const TiffEntryBase& rhs) : storage_(rhs.storage_) { } -TiffDirectory::TiffDirectory(const TiffDirectory& rhs) : TiffComponent(rhs), hasNext_(rhs.hasNext_) { -} - -TiffSubIfd::TiffSubIfd(const TiffSubIfd& rhs) : TiffEntryBase(rhs), newGroup_(rhs.newGroup_) { -} - -TiffBinaryArray::TiffBinaryArray(const TiffBinaryArray& rhs) : - TiffEntryBase(rhs), - cfgSelFct_(rhs.cfgSelFct_), - arraySet_(rhs.arraySet_), - arrayCfg_(rhs.arrayCfg_), - arrayDef_(rhs.arrayDef_), - defSize_(rhs.defSize_), - setSize_(rhs.setSize_), - origData_(rhs.origData_), - origSize_(rhs.origSize_), - pRoot_(rhs.pRoot_) { -} - TiffComponent::UniquePtr TiffComponent::clone() const { return UniquePtr(doClone()); } diff --git a/src/tiffcomposite_int.hpp b/src/tiffcomposite_int.hpp index e198ea593a..c32c41c515 100644 --- a/src/tiffcomposite_int.hpp +++ b/src/tiffcomposite_int.hpp @@ -864,7 +864,7 @@ class TiffDirectory : public TiffComponent { //! @name Protected Creators //@{ //! Copy constructor (used to implement clone()). - TiffDirectory(const TiffDirectory& rhs); + TiffDirectory(const TiffDirectory&) = default; //@} //! @name Protected Manipulators @@ -958,7 +958,7 @@ class TiffSubIfd : public TiffEntryBase { //! @name Protected Creators //@{ //! Copy constructor (used to implement clone()). - TiffSubIfd(const TiffSubIfd& rhs); + TiffSubIfd(const TiffSubIfd&) = default; TiffSubIfd& operator=(const TiffSubIfd&) = delete; //@} @@ -1360,7 +1360,7 @@ class TiffBinaryArray : public TiffEntryBase { //! @name Protected Creators //@{ //! Copy constructor (used to implement clone()). - TiffBinaryArray(const TiffBinaryArray& rhs); + TiffBinaryArray(const TiffBinaryArray&) = default; //@} //! @name Protected Manipulators From 07681aede74515aef326102d735c53d76c67bca4 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 26 Jan 2023 17:52:52 -0800 Subject: [PATCH 2/5] add missing const Found with cppcheck Signed-off-by: Rosen Penev --- src/futils.cpp | 2 +- src/olympusmn_int.cpp | 4 ++-- src/sonymn_int.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/futils.cpp b/src/futils.cpp index af969ed4f2..7ded06c9fb 100644 --- a/src/futils.cpp +++ b/src/futils.cpp @@ -195,7 +195,7 @@ size_t base64decode(const char* in, char* out, size_t out_size) { Protocol fileProtocol(const std::string& path) { Protocol result = pFile; - struct { + const struct { std::string name; Protocol prot; bool isUrl; // path.size() > name.size() diff --git a/src/olympusmn_int.cpp b/src/olympusmn_int.cpp index 3c91628283..31b05f663d 100644 --- a/src/olympusmn_int.cpp +++ b/src/olympusmn_int.cpp @@ -1243,7 +1243,7 @@ std::ostream& OlympusMakerNote::print0x0201(std::ostream& os, const Value& value // 6 numbers: 0. Make, 1. Unknown, 2. Model, 3. Sub-model, 4-5. Unknown. // Only the Make, Model and Sub-model are used to determine the lens model - static struct { + static const struct { byte val[3]; const char* label; } lensTypes[] = { @@ -1397,7 +1397,7 @@ std::ostream& OlympusMakerNote::print0x0209(std::ostream& os, const Value& value std::ostream& OlympusMakerNote::printEq0x0301(std::ostream& os, const Value& value, const ExifData*) { // 6 numbers: 0. Make, 1. Unknown, 2. Model, 3. Sub-model, 4-5. Unknown. // Only the Make and Model are used to determine the extender model - static struct { + static const struct { byte val[2]; const char* label; } extenderModels[] = { diff --git a/src/sonymn_int.cpp b/src/sonymn_int.cpp index 213d2b8a56..1a3bf08fd9 100644 --- a/src/sonymn_int.cpp +++ b/src/sonymn_int.cpp @@ -1864,7 +1864,7 @@ std::ostream& SonyMakerNote::printSony2FpFocusPosition2(std::ostream& os, const } // Ranges of models that do not support this tag - for (auto& m : {"DSC-", "Stellar"}) { + for (const auto& m : {"DSC-", "Stellar"}) { if (startsWith(model, m)) { os << N_("n/a"); return os; From e1410b364387d681e068ca065c53b496756e2c79 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 26 Jan 2023 17:46:55 -0800 Subject: [PATCH 3/5] use find instead of count Signed-off-by: Rosen Penev --- src/http.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index 1a954f3379..50ccd05336 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -131,13 +131,13 @@ static int makeNonBlocking(int sockfd) { } int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::string& errors) { - if (!request.count("verb")) + if (request.find("verb") == request.end()) request["verb"] = "GET"; - if (!request.count("header")) + if (request.find("header") == request.end()) request["header"] = ""; - if (!request.count("version")) + if (request.find("version") == request.end()) request["version"] = "1.0"; - if (!request.count("port")) + if (request.find("port") == request.end()) request["port"] = ""; std::string file; From 12bf3fd51efc4ce1a41b58011cf4f5a8a800645f Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 26 Jan 2023 18:47:04 -0800 Subject: [PATCH 4/5] replace substr with resize/pop_back Shorter and more efficient. Signed-off-by: Rosen Penev --- app/actions.cpp | 4 ++-- src/convert.cpp | 4 ++-- src/http.cpp | 4 ++-- src/pngimage.cpp | 2 +- src/properties.cpp | 8 ++++---- src/value.cpp | 14 +++++++------- src/xmp.cpp | 9 +++------ 7 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/actions.cpp b/app/actions.cpp index d7343f9866..bd7744c76f 100644 --- a/app/actions.cpp +++ b/app/actions.cpp @@ -631,7 +631,7 @@ int Rename::run(const std::string& path) { return 1; } std::string v = md->toString(); - if (v.length() == 0 || v[0] == ' ') { + if (v.empty() || v.front() == ' ') { std::cerr << _("Image file creation timestamp not set in the file") << " " << path << "\n"; return 1; } @@ -1602,7 +1602,7 @@ int Timestamp::touch(const std::string& path) const { //! @endcond int str2Tm(const std::string& timeStr, struct tm* tm) { - if (timeStr.length() == 0 || timeStr[0] == ' ') + if (timeStr.empty() || timeStr.front() == ' ') return 1; if (timeStr.length() < 19) return 2; diff --git a/src/convert.cpp b/src/convert.cpp index 577fd08000..51f9b12b84 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -740,7 +740,7 @@ void Converter::cnvExifDate(const char* from, const char* to) { } if (subsec.size() > 10) - subsec = subsec.substr(0, 10); + subsec.resize(10); snprintf(buf, sizeof(buf), "%4d-%02d-%02dT%02d:%02d:%02d%s", year, month, day, hour, min, sec, subsec.c_str()); buf[sizeof(buf) - 1] = 0; @@ -1141,7 +1141,7 @@ void Converter::cnvXmpGPSCoord(const char* from, const char* to) { double deg = 0.0; double min = 0.0; double sec = 0.0; - char ref = value[value.length() - 1]; + char ref = value.back(); char sep1 = '\0'; char sep2 = '\0'; diff --git a/src/http.cpp b/src/http.cpp index 50ccd05336..2c140ec0d2 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -305,8 +305,8 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st while (c && first_newline && c < first_newline && h < buffer + body) { std::string key(h); std::string value(c + 1); - key = key.substr(0, c - h); - value = value.substr(0, first_newline - c - 1); + key.resize(c - h); + value.resize(first_newline - c - 1); response[key] = value; h = first_newline + 1; c = strchr(h, C); diff --git a/src/pngimage.cpp b/src/pngimage.cpp index fc96a5b0e3..eed1a07136 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -247,7 +247,7 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, si } while (dataString.size() < iMax) dataString += ' '; - dataString = dataString.substr(0, iMax); + dataString.resize(iMax); if (bPrint) { io_->seek(dataOffset, BasicIo::cur); // jump to checksum diff --git a/src/properties.cpp b/src/properties.cpp index 005e2aa838..264f01b371 100644 --- a/src/properties.cpp +++ b/src/properties.cpp @@ -4942,8 +4942,8 @@ const XmpNsInfo* XmpProperties::lookupNsRegistryUnsafe(const XmpNsInfo::Prefix& void XmpProperties::registerNs(const std::string& ns, const std::string& prefix) { auto scopedWriteLock = std::scoped_lock(mutex_); std::string ns2 = ns; - if (ns2.substr(ns2.size() - 1, 1) != "/" && ns2.substr(ns2.size() - 1, 1) != "#") - ns2 += "/"; + if (ns2.back() != '/' && ns2.back() != '#') + ns2 += '/'; // Check if there is already a registered namespace with this prefix const XmpNsInfo* xnp = lookupNsRegistryUnsafe(XmpNsInfo::Prefix(prefix)); if (xnp) { @@ -4995,8 +4995,8 @@ void XmpProperties::unregisterNs() { std::string XmpProperties::prefix(const std::string& ns) { auto scoped_read_lock = std::scoped_lock(mutex_); std::string ns2 = ns; - if (ns2.substr(ns2.size() - 1, 1) != "/" && ns2.substr(ns2.size() - 1, 1) != "#") - ns2 += "/"; + if (ns2.back() != '/' && ns2.back() != '#') + ns2 += '/'; auto i = nsRegistry_.find(ns2); std::string p; diff --git a/src/value.cpp b/src/value.cpp index 3a9edabb57..c18a613c91 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -255,7 +255,7 @@ AsciiValue::AsciiValue(const std::string& buf) : StringValueBase(asciiString, bu int AsciiValue::read(const std::string& buf) { value_ = buf; // ensure count>0 and nul terminated # https://github.com/Exiv2/exiv2/issues/1484 - if (value_.empty() || value_.at(value_.size() - 1) != '\0') { + if (value_.empty() || value_.back() != '\0') { value_ += '\0'; } return 0; @@ -325,8 +325,8 @@ int CommentValue::read(const std::string& comment) { // Strip quotes (so you can also specify the charset without quotes) if (!name.empty() && name.front() == '"') name = name.substr(1); - if (!name.empty() && name[name.length() - 1] == '"') - name = name.substr(0, name.length() - 1); + if (!name.empty() && name.back() == '"') + name.pop_back(); charsetId = CharsetInfo::charsetIdByName(name); if (charsetId == invalidCharsetId) { #ifndef SUPPRESS_WARNINGS @@ -391,7 +391,7 @@ std::string CommentValue::comment(const char* encoding) const { bool bAscii = charsetId() == undefined || charsetId() == ascii; // # 1266 Remove trailing nulls if (bAscii && c.find('\0') != std::string::npos) { - c = c.substr(0, c.find('\0')); + c.resize(c.find('\0')); } return c; } @@ -503,8 +503,8 @@ int XmpTextValue::read(const std::string& buf) { // Strip quotes (so you can also specify the type without quotes) if (!type.empty() && type.front() == '"') type = type.substr(1); - if (!type.empty() && type[type.length() - 1] == '"') - type = type.substr(0, type.length() - 1); + if (!type.empty() && type.back() == '"') + type.pop_back(); b.clear(); if (pos != std::string::npos) b = buf.substr(pos + 1); @@ -670,7 +670,7 @@ int LangAltValue::read(const std::string& buf) { if (lang.empty() || lang.find('"') != lang.length() - 1) throw Error(ErrorCode::kerInvalidLangAltValue, buf); - lang = lang.substr(0, lang.length() - 1); + lang.pop_back(); } if (lang.empty()) diff --git a/src/xmp.cpp b/src/xmp.cpp index f136f65114..3c528dc906 100644 --- a/src/xmp.cpp +++ b/src/xmp.cpp @@ -585,11 +585,8 @@ static XMP_Status nsDumper(void* refCon, XMP_StringPtr buffer, XMP_StringLen buf bool bNS = out.find(':') != std::string::npos && !bURI; // pop trailing ':' on a namespace - if (bNS && !out.empty()) { - std::size_t length = out.length(); - if (out[length - 1] == ':') - out = out.substr(0, length - 1); - } + if (bNS && !out.empty() && out.back() == ':') + out.pop_back(); if (bURI || bNS) { auto p = static_cast*>(refCon); @@ -706,7 +703,7 @@ int XmpParser::decode(XmpData& xmpData, const std::string& xmpPacket) { bool ret = SXMPMeta::GetNamespacePrefix(schemaNs.c_str(), &prefix); if (!ret) throw Error(ErrorCode::kerSchemaNamespaceNotRegistered, schemaNs); - prefix = prefix.substr(0, prefix.size() - 1); + prefix.pop_back(); XmpProperties::registerNs(schemaNs, prefix); } continue; From a6b4da3edfc42d4a2d06cca5abf2434a16f742c7 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 26 Jan 2023 19:45:41 -0800 Subject: [PATCH 5/5] remove unused variable Signed-off-by: Rosen Penev --- include/exiv2/asfvideo.hpp | 6 +++--- include/exiv2/riffvideo.hpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/exiv2/asfvideo.hpp b/include/exiv2/asfvideo.hpp index afcca1909e..cd9eb0aa25 100644 --- a/include/exiv2/asfvideo.hpp +++ b/include/exiv2/asfvideo.hpp @@ -58,9 +58,9 @@ class EXIV2API AsfVideo : public Image { explicit AsfVideo(BasicIo::UniquePtr io); //! Copy constructor - AsfVideo(const AsfVideo& rhs) = delete; + AsfVideo(const AsfVideo&) = delete; //! Assignment operator - AsfVideo& operator=(const AsfVideo& rhs) = delete; + AsfVideo& operator=(const AsfVideo&) = delete; //@} //! @name Manipulators @@ -170,4 +170,4 @@ EXIV2API bool isAsfType(BasicIo& iIo, bool advance); } // namespace Exiv2 -#endif // #ifndef ASFVIDEO_HPP_ \ No newline at end of file +#endif // #ifndef ASFVIDEO_HPP_ diff --git a/include/exiv2/riffvideo.hpp b/include/exiv2/riffvideo.hpp index d564c7018a..7982bf7160 100644 --- a/include/exiv2/riffvideo.hpp +++ b/include/exiv2/riffvideo.hpp @@ -55,9 +55,9 @@ class EXIV2API RiffVideo : public Image { explicit RiffVideo(BasicIo::UniquePtr io); //! Copy constructor - RiffVideo(const RiffVideo& rhs) = delete; + RiffVideo(const RiffVideo&) = delete; //! Assignment operator - RiffVideo& operator=(const RiffVideo& rhs) = delete; + RiffVideo& operator=(const RiffVideo&) = delete; //@}