From c91a51f39de4282580902d7cb7417791bd19e713 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 14:40:19 -0800 Subject: [PATCH 1/9] make conversions explicit Found with MSVC's C4244 Signed-off-by: Rosen Penev --- app/exiv2.cpp | 2 +- src/futils.cpp | 2 +- src/image.cpp | 6 +++--- src/sonymn_int.cpp | 2 +- src/version.cpp | 3 +-- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/exiv2.cpp b/app/exiv2.cpp index e0838b932d..874f11cbf9 100644 --- a/app/exiv2.cpp +++ b/app/exiv2.cpp @@ -469,7 +469,7 @@ int Params::option(int opt, const std::string& optArg, int optOpt) { int Params::setLogLevel(const std::string& optArg) { int rc = 0; - const char logLevel = tolower(optArg[0]); + const auto logLevel = static_cast(tolower(optArg[0])); switch (logLevel) { case 'd': Exiv2::LogMsg::setLevel(Exiv2::LogMsg::debug); diff --git a/src/futils.cpp b/src/futils.cpp index 38896ced88..02764115f2 100644 --- a/src/futils.cpp +++ b/src/futils.cpp @@ -81,7 +81,7 @@ char to_hex(char code) { /// @brief Convert a hex character to its integer value. char from_hex(char ch) { - return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; + return isdigit(ch) ? ch - '0' : static_cast(tolower(ch)) - 'a' + 10; } std::string urlencode(const std::string& str) { diff --git a/src/image.cpp b/src/image.cpp index 9a23515899..7857896fb6 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -469,9 +469,9 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct if (bNikon) { // tag is an embedded tiff const long byteslen = count - jump; - DataBuf bytes(byteslen); // allocate a buffer - io.readOrThrow(bytes.data(), byteslen, ErrorCode::kerCorruptedMetadata); // read - MemIo memIo(bytes.c_data(), byteslen); // create a file + auto b = DataBuf(byteslen); // allocate a buffer + io.readOrThrow(b.data(), byteslen, ErrorCode::kerCorruptedMetadata); // read + MemIo memIo(b.c_data(), byteslen); // create a file printTiffStructure(memIo, out, option, depth + 1); } else { // tag is an IFD diff --git a/src/sonymn_int.cpp b/src/sonymn_int.cpp index cd30995445..751f8aff1e 100644 --- a/src/sonymn_int.cpp +++ b/src/sonymn_int.cpp @@ -2289,7 +2289,7 @@ static DataBuf sonyTagCipher(uint16_t /* tag */, const byte* bytes, size_t size, byte code[256]; for (uint32_t i = 0; i < 249; i++) { if (bDecipher) { - code[(i * i * i) % 249] = i; + code[(i * i * i) % 249] = static_cast(i); } else { code[i] = (i * i * i) % 249; } diff --git a/src/version.cpp b/src/version.cpp index 919f7101f9..afe94fb458 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -127,8 +127,7 @@ static std::vector getLoadedLibraries() { char szFilename[_MAX_PATH]; for (DWORD h = 0; h < cbNeeded / sizeof(handles[0]); h++) { GetModuleFileNameA(handles[h], szFilename, static_cast(std::size(szFilename))); - std::string path(szFilename); - pushPath(path, libs, paths); + pushPath(szFilename, libs, paths); } } #elif defined(__APPLE__) From 412fd7f04702d206a3bad4b8066ed8262787793b Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 14:31:18 -0800 Subject: [PATCH 2/9] use upper() function Signed-off-by: Rosen Penev --- src/asfvideo.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/asfvideo.cpp b/src/asfvideo.cpp index 21e7e8f246..d58a72ff47 100644 --- a/src/asfvideo.cpp +++ b/src/asfvideo.cpp @@ -12,6 +12,7 @@ #include "error.hpp" #include "futils.hpp" #include "helper_functions.hpp" +#include "utils.hpp" // ***************************************************************************** // class member definitions namespace Exiv2 { @@ -71,13 +72,9 @@ std::string AsfVideo::GUIDTag::to_string() { } // Concatenate all strings into a single string - std::string strGuid = ss.str(); // Convert the string to uppercase - for (auto& c : strGuid) { - c = toupper(c); - } // Example of output 399595EC-8667-4E2D-8FDB-98814CE76C1E - return strGuid; + return Internal::upper(ss.str()); } bool AsfVideo::GUIDTag::operator<(const GUIDTag& other) const { From 8c1e79d583c5e3113b49635980883872685374bf Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 14:28:22 -0800 Subject: [PATCH 3/9] fix memory leak Found with cppcheck. Signed-off-by: Rosen Penev --- src/http.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/http.cpp b/src/http.cpp index 2c140ec0d2..ecea29bc4a 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -215,8 +215,10 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st // http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzab6/rzab6uafinet.htm if (serv_addr.sin_addr.s_addr == static_cast(INADDR_NONE)) { auto host = gethostbyname(servername_p); - if (!host) + if (!host) { + closesocket(sockfd); return error(errors, "no such host", servername_p); + } memcpy(&serv_addr.sin_addr, host->h_addr, sizeof(serv_addr.sin_addr)); } From 4748c8b312b86e22a9bb5a1827966a6b4f6af275 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 14:05:01 -0800 Subject: [PATCH 4/9] replace localtime with _s/_r variant cppcheck warns on localtime which is not necessarily threadsafe. Signed-off-by: Rosen Penev --- src/crwimage_int.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index 10746418b3..69b1a3f30c 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -741,7 +741,12 @@ void CrwMap::decode0x180e(const CiffComponent& ciffComponent, const CrwMapping* ULongValue v; v.read(ciffComponent.pData(), 8, byteOrder); time_t t = v.value_.at(0); - auto tm = std::localtime(&t); + struct tm r; +#ifdef _WIN32 + auto tm = localtime_s(&r, &t) ? nullptr : &r; +#else + auto tm = localtime_r(&t, &r); +#endif if (tm) { const size_t m = 20; char s[m]; From 38e4dbe36e9f9c1d662a16654354e3e43e2aee41 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 15:02:42 -0800 Subject: [PATCH 5/9] explicit conversion Signed-off-by: Rosen Penev --- src/sonymn_int.cpp | 2 +- src/utils.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sonymn_int.cpp b/src/sonymn_int.cpp index 751f8aff1e..4442dcda8d 100644 --- a/src/sonymn_int.cpp +++ b/src/sonymn_int.cpp @@ -2295,7 +2295,7 @@ static DataBuf sonyTagCipher(uint16_t /* tag */, const byte* bytes, size_t size, } } for (uint32_t i = 249; i < 256; i++) { - code[i] = i; + code[i] = static_cast(i); } // code byte-by-byte diff --git a/src/utils.cpp b/src/utils.cpp index 9671b19873..82d6d8b9b3 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -7,13 +7,13 @@ namespace Exiv2::Internal { std::string upper(const std::string& str) { std::string result = str; - std::transform(str.begin(), str.end(), result.begin(), ::toupper); + std::transform(str.begin(), str.end(), result.begin(), [](int c) { return static_cast(toupper(c)); }); return result; } std::string lower(const std::string& a) { std::string b = a; - std::transform(a.begin(), a.end(), b.begin(), ::tolower); + std::transform(a.begin(), a.end(), b.begin(), [](int c) { return static_cast(tolower(c)); }); return b; } From 4c36e2e40c631835ec7c1524ef206a74139509e4 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 16:43:14 -0800 Subject: [PATCH 6/9] fix wrong snprintf format Signed-off-by: Rosen Penev --- src/iptc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/iptc.cpp b/src/iptc.cpp index fa843647f3..9d8e19c7f4 100644 --- a/src/iptc.cpp +++ b/src/iptc.cpp @@ -292,7 +292,7 @@ void IptcData::printStructure(std::ostream& out, const Slice& bytes, size uint16_t dataset = bytes.at(i + 2); Internal::enforce(bytes.size() - i >= 5, ErrorCode::kerCorruptedMetadata); uint16_t len = getUShort(bytes.subSlice(i + 3, bytes.size()), bigEndian); - snprintf(buff, sizeof(buff), " %6d | %7d | %-24s | %6d | ", record, dataset, + snprintf(buff, sizeof(buff), " %6hu | %7hu | %-24s | %6hu | ", record, dataset, Exiv2::IptcDataSets::dataSetName(dataset, record).c_str(), len); Internal::enforce(bytes.size() - i >= 5 + static_cast(len), ErrorCode::kerCorruptedMetadata); From fadade30814b6d7ae81d858647a065e0257a8cf7 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 17:44:33 -0800 Subject: [PATCH 7/9] replace array with vector Using the constructor is one less line. Plus it seems to fix bugs in Apple's clang. Signed-off-by: Rosen Penev --- src/preview.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/preview.cpp b/src/preview.cpp index 19d2b62024..208d721258 100644 --- a/src/preview.cpp +++ b/src/preview.cpp @@ -823,8 +823,7 @@ bool LoaderXmpJpeg::readDimensions() { DataBuf decodeHex(const byte* src, size_t srcSize) { // create decoding table byte invalid = 16; - std::array decodeHexTable; - decodeHexTable.fill(invalid); + auto decodeHexTable = std::vector(256, invalid); for (byte i = 0; i < 10; i++) decodeHexTable[static_cast('0') + i] = i; for (byte i = 0; i < 6; i++) @@ -865,8 +864,7 @@ DataBuf decodeBase64(const std::string& src) { // create decoding table unsigned long invalid = 64; - std::array decodeBase64Table; - decodeBase64Table.fill(invalid); + auto decodeBase64Table = std::vector(256, invalid); for (unsigned long i = 0; i < 64; i++) decodeBase64Table[static_cast(encodeBase64Table[i])] = i; From f921e88cdc28352a102ce4272d0211773a760a65 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 17:54:10 -0800 Subject: [PATCH 8/9] use count_if Signed-off-by: Rosen Penev --- src/preview.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/preview.cpp b/src/preview.cpp index 208d721258..a7f9412326 100644 --- a/src/preview.cpp +++ b/src/preview.cpp @@ -860,8 +860,6 @@ DataBuf decodeHex(const byte* src, size_t srcSize) { const char encodeBase64Table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; DataBuf decodeBase64(const std::string& src) { - const size_t srcSize = src.size(); - // create decoding table unsigned long invalid = 64; auto decodeBase64Table = std::vector(256, invalid); @@ -869,11 +867,8 @@ DataBuf decodeBase64(const std::string& src) { decodeBase64Table[static_cast(encodeBase64Table[i])] = i; // calculate dest size - unsigned long validSrcSize = 0; - for (unsigned long srcPos = 0; srcPos < srcSize; srcPos++) { - if (decodeBase64Table[static_cast(src[srcPos])] != invalid) - validSrcSize++; - } + auto validSrcSize = static_cast( + std::count_if(src.begin(), src.end(), [=](unsigned char c) { return decodeBase64Table.at(c) != invalid; })); if (validSrcSize > ULONG_MAX / 3) return {}; // avoid integer overflow const unsigned long destSize = (validSrcSize * 3) / 4; @@ -886,7 +881,7 @@ DataBuf decodeBase64(const std::string& src) { // decode for (unsigned long srcPos = 0, destPos = 0; destPos < destSize;) { unsigned long buffer = 0; - for (int bufferPos = 3; bufferPos >= 0 && srcPos < srcSize; srcPos++) { + for (int bufferPos = 3; bufferPos >= 0 && srcPos < src.size(); srcPos++) { unsigned long srcValue = decodeBase64Table[static_cast(src[srcPos])]; if (srcValue == invalid) continue; From 2722e0008e450f7763fa6cd49e5d3f582d341408 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 11 Feb 2023 18:09:30 -0800 Subject: [PATCH 9/9] use std::replace shorter Signed-off-by: Rosen Penev --- src/pentaxmn_int.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/pentaxmn_int.cpp b/src/pentaxmn_int.cpp index 88cb7e8c16..ab3942a873 100644 --- a/src/pentaxmn_int.cpp +++ b/src/pentaxmn_int.cpp @@ -892,20 +892,14 @@ constexpr TagDetails pentaxHighISONoiseReduction[] = { std::ostream& PentaxMakerNote::printVersion(std::ostream& os, const Value& value, const ExifData*) { std::string val = value.toString(); - size_t i = 0; - while ((i = val.find(' ', i)) != std::string::npos && i != val.length() - 1) { - val.replace(i, 1, "."); - } + std::replace(val.begin(), val.end(), ' ', '.'); os << val; return os; } std::ostream& PentaxMakerNote::printResolution(std::ostream& os, const Value& value, const ExifData*) { std::string val = value.toString(); - size_t i = 0; - while ((i = val.find(' ', i)) != std::string::npos && i != val.length() - 1) { - val.replace(i, 1, "x"); - } + std::replace(val.begin(), val.end(), ' ', 'x'); os << val; return os; }