Mercurial > hg > orthanc
changeset 6754:a7dbb19622fd
clang-tidy (cont)
line wrap: on
line diff
--- a/OrthancFramework/Resources/ThirdParty/base64/base64.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Resources/ThirdParty/base64/base64.cpp Mon May 11 15:49:34 2026 +0200 @@ -1,7 +1,7 @@ /* base64.cpp and base64.h - Copyright (C) 2004-2008 René Nyffenegger + Copyright (C) 2004-2008 Ren� Nyffenegger This source code is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages @@ -21,7 +21,7 @@ 3. This notice may not be removed or altered from any source distribution. - René Nyffenegger rene.nyffenegger@adp-gmbh.ch + Ren� Nyffenegger rene.nyffenegger@adp-gmbh.ch ------------------------------ This version has been modified (changed the interface + use another decoding algorithm @@ -43,8 +43,12 @@ void base64_encode(std::string& result, const std::string& stringToEncode) { - const unsigned char* bytes_to_encode = reinterpret_cast<const unsigned char*> - (stringToEncode.size() > 0 ? &stringToEncode[0] : NULL); + if (stringToEncode.size() == 0) + { + return; + } + + const unsigned char* bytes_to_encode = reinterpret_cast<const unsigned char*>(stringToEncode.c_str()); size_t in_len = stringToEncode.size(); result.reserve(result.size() + in_len * 4 / 3 + 10); @@ -87,7 +91,7 @@ } } -// old code from René Nyffenegger. This code is slower +// old code from Ren� Nyffenegger. This code is slower void base64_decode_old(std::string& result, const std::string& encoded_string) { size_t in_len = encoded_string.size(); int i = 0; @@ -101,14 +105,14 @@ char_array_4[i++] = encoded_string[in_]; in_++; if (i ==4) { for (i = 0; i <4; i++) - char_array_4[i] = base64_chars.find(char_array_4[i]); + char_array_4[i] = base64_chars.find(static_cast<char>(char_array_4[i])); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) - result += char_array_3[i]; + result += static_cast<char>(char_array_3[i]); i = 0; } } @@ -118,14 +122,14 @@ char_array_4[j] = 0; for (j = 0; j <4; j++) - char_array_4[j] = base64_chars.find(char_array_4[j]); + char_array_4[j] = base64_chars.find(static_cast<char>(char_array_4[j])); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) - result += char_array_3[j]; + result += static_cast<char>(char_array_3[j]); } } @@ -166,7 +170,7 @@ int val=0, valb=-8; for (std::string::const_iterator c = stringToDecode.begin(); c != stringToDecode.end(); ++c) { - size_t index = static_cast<size_t>(*c); + size_t index = static_cast<size_t>(static_cast<unsigned char>(*c)); if (decode_indexes[index] == -1) break; val = (val<<6) + decode_indexes[index];
--- a/OrthancFramework/Sources/DicomNetworking/DicomAssociationParameters.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/DicomNetworking/DicomAssociationParameters.cpp Mon May 11 15:49:34 2026 +0200 @@ -343,7 +343,7 @@ result.remote_ = RemoteModalityParameters(serialized[REMOTE]); result.localAet_ = SerializationToolbox::ReadString(serialized, LOCAL_AET); - result.timeout_ = SerializationToolbox::ReadInteger(serialized, TIMEOUT, GetDefaultTimeout()); + result.timeout_ = SerializationToolbox::ReadUnsignedInteger(serialized, TIMEOUT, GetDefaultTimeout()); // The calls to "isMember()" below are for compatibility with Orthanc <= 1.8.2 serialization if (serialized.isMember(MAXIMUM_PDU_LENGTH))
--- a/OrthancFramework/Sources/DicomParsing/DcmtkTranscoder.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/DcmtkTranscoder.cpp Mon May 11 15:49:34 2026 +0200 @@ -178,7 +178,7 @@ else { // Check out "dcmjpeg/apps/dcmcjpeg.cc" - DJ_RPLossy parameters(lossyQuality); + DJ_RPLossy parameters(static_cast<int>(lossyQuality)); if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGProcess1, ¶meters)) { @@ -205,7 +205,7 @@ else { // Check out "dcmjpeg/apps/dcmcjpeg.cc" - DJ_RPLossy parameters(lossyQuality); + DJ_RPLossy parameters(static_cast<int>(lossyQuality)); if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGProcess2_4, ¶meters)) { fixer.Apply(dicom);
--- a/OrthancFramework/Sources/DicomParsing/Internals/DicomFrameIndex.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/Internals/DicomFrameIndex.cpp Mon May 11 15:49:34 2026 +0200 @@ -29,10 +29,9 @@ #include "../../DicomFormat/DicomImageInformation.h" #include "../FromDcmtkBridge.h" #include "../../Endianness.h" +#include "../../SerializationToolbox.h" #include "DicomImageDecoder.h" -#include <boost/lexical_cast.hpp> - #include <dcmtk/dcmdata/dcdeftag.h> #include <dcmtk/dcmdata/dcpxitem.h> #include <dcmtk/dcmdata/dcpixseq.h> @@ -378,22 +377,15 @@ return 1; } - int count = -1; - try + unsigned int count = 0; + + if (SerializationToolbox::ParseUnsignedInteger(count, tmp)) { - count = boost::lexical_cast<int>(tmp); - } - catch (boost::bad_lexical_cast&) - { - } - - if (count < 0) - { - throw OrthancException(ErrorCode_BadFileFormat); + return count; } else { - return static_cast<unsigned int>(count); + throw OrthancException(ErrorCode_BadFileFormat, "Invalid number of frames"); } }
--- a/OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp Mon May 11 15:49:34 2026 +0200 @@ -346,8 +346,12 @@ return true; } - - unsigned int block = boost::lexical_cast<unsigned int>(*blockUri); + unsigned int block; + + if (!SerializationToolbox::ParseUnsignedInteger(block, *blockUri)) + { + throw OrthancException(ErrorCode_BadFileFormat, "Invalid Block URI: not a number"); + } if (block < GetPixelDataBlockCount(pixelData, transferSyntax)) { @@ -385,13 +389,9 @@ } } } - catch (boost::bad_lexical_cast&) - { - // The URI entered by the user is not a number - } catch (std::bad_cast&) { - // This should never happen + throw OrthancException(ErrorCode_InternalError); // This should never happen / but I don't know why this std::bad_cast has been introduced (probably a std::bad_cast from DCMTK) } return false; @@ -453,12 +453,8 @@ // Go down in the tag hierarchy according to the URI for (size_t pos = 0; pos < uri.size() / 2; pos++) { - size_t index; - try - { - index = boost::lexical_cast<size_t>(uri[2 * pos + 1]); - } - catch (boost::bad_lexical_cast&) + unsigned int index; + if (!SerializationToolbox::ParseUnsignedInteger(index, uri[2 * pos + 1])) { return; } @@ -466,7 +462,7 @@ DcmTagKey k; DcmItem *child = NULL; ParseTagAndGroup(k, uri[2 * pos]); - if (!dicom->findAndGetSequenceItem(k, child, index).good() || + if (!dicom->findAndGetSequenceItem(k, child, static_cast<signed long>(index)).good() || child == NULL) { return;
--- a/OrthancFramework/Sources/HttpServer/HttpServer.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpServer.cpp Mon May 11 15:49:34 2026 +0200 @@ -1695,7 +1695,7 @@ server->GetExceptionFormatter()->Format(output, e, method, requestUri); } } - catch (OrthancException&) + catch (OrthancException&) // NOLINT(bugprone-empty-catch) { // An exception here reflects the fact that the status code // was already set by the HTTP handler.
--- a/OrthancFramework/Sources/Logging.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/Logging.cpp Mon May 11 15:49:34 2026 +0200 @@ -40,6 +40,8 @@ * Common section *********************************************************/ +// NOLINTBEGIN(bugprone-reserved-identifier) // for all identifiers starting with _Orthanc + namespace Orthanc { namespace Logging @@ -1165,5 +1167,5 @@ } } - #endif // ORTHANC_ENABLE_LOGGING +// NOLINTEND(bugprone-reserved-identifier) \ No newline at end of file
--- a/OrthancFramework/Sources/SQLite/Connection.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/SQLite/Connection.cpp Mon May 11 15:49:34 2026 +0200 @@ -382,7 +382,7 @@ { int err = sqlite3_create_function_v2(db_, func->GetName(), - func->GetCardinality(), + static_cast<int>(func->GetCardinality()), SQLITE_UTF8, func, ScalarFunctionCaller,
--- a/OrthancFramework/Sources/SerializationToolbox.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/SerializationToolbox.cpp Mon May 11 15:49:34 2026 +0200 @@ -33,6 +33,7 @@ #endif #include <boost/lexical_cast.hpp> +#include <limits> namespace Orthanc @@ -92,16 +93,21 @@ const std::string& field) { if (value.type() != Json::objectValue || - !value.isMember(field.c_str()) || - (value[field.c_str()].type() != Json::intValue && - value[field.c_str()].type() != Json::uintValue)) + !value.isMember(field) || + (value[field].type() != Json::intValue && + value[field].type() != Json::uintValue)) { throw OrthancException(ErrorCode_BadFileFormat, "Integer value expected in field: " + field); } else { - return value[field.c_str()].asInt(); + int64_t v64 = value[field].asInt64(); + if (v64 > static_cast<int64_t>(std::numeric_limits<int>::max())) + { + throw OrthancException(ErrorCode_BadParameterType, "Value too large to fit in an integer in field '" + field + "'"); + } + return static_cast<int>(v64); } } @@ -124,17 +130,25 @@ unsigned int SerializationToolbox::ReadUnsignedInteger(const Json::Value& value, const std::string& field) { - int tmp = ReadInteger(value, field); - - if (tmp < 0) + if (value.type() != Json::objectValue || + !value.isMember(field) || + (value[field].type() != Json::intValue && + value[field].type() != Json::uintValue)) { throw OrthancException(ErrorCode_BadFileFormat, - "Unsigned integer value expected in field: " + field); + "Integer value expected in field: " + field); } else { - return static_cast<unsigned int>(tmp); - } + int64_t v64 = value[field].asInt64(); + + if (v64 < 0) + { + throw OrthancException(ErrorCode_BadParameterType, "Negative value found while expecting an unsigned int in field '" + field + "'"); + } + + return static_cast<unsigned int>(v64); + } } @@ -621,7 +635,14 @@ { return ParseValue<uint64_t, false>(target, source); } + + bool SerializationToolbox::ParseUnsignedInteger(unsigned int& target, + const std::string& source) + { + return ParseValue<unsigned int, false>(target, source); + } + bool SerializationToolbox::ParseFloat(float& target, const std::string& source)
--- a/OrthancFramework/Sources/SerializationToolbox.h Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/Sources/SerializationToolbox.h Mon May 11 15:49:34 2026 +0200 @@ -130,6 +130,9 @@ static bool ParseInteger64(int64_t& result, const std::string& value); + static bool ParseUnsignedInteger(unsigned int& result, + const std::string& value); + static bool ParseUnsignedInteger32(uint32_t& result, const std::string& value);
--- a/OrthancFramework/UnitTestsSources/JobsTests.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/JobsTests.cpp Mon May 11 15:49:34 2026 +0200 @@ -1721,6 +1721,18 @@ TEST(SerializationToolbox, Numbers) { { + Json::Value v; + + v["field"] = -1; + ASSERT_THROW(SerializationToolbox::ReadUnsignedInteger(v, "field"), OrthancException); + ASSERT_EQ(-1, SerializationToolbox::ReadInteger(v, "field")); + + v["field"] = 3000000000ul; + ASSERT_THROW(SerializationToolbox::ReadInteger(v, "field"), OrthancException); + ASSERT_EQ(3000000000ul, SerializationToolbox::ReadUnsignedInteger(v, "field")); + } + + { int32_t i; ASSERT_FALSE(SerializationToolbox::ParseInteger32(i, "")); ASSERT_FALSE(SerializationToolbox::ParseInteger32(i, "ee"));
--- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Mon May 11 15:49:34 2026 +0200 @@ -110,7 +110,7 @@ toEncode.clear(); toEncode.reserve(size); for (size_t i = 0; i < size; i++) - toEncode.push_back(i % 256); + toEncode.push_back(static_cast<char>(i % 256)); Toolbox::EncodeBase64(base64Result, toEncode); Toolbox::DecodeBase64(decodedResult, base64Result);
--- a/OrthancServer/Sources/ServerIndex.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancServer/Sources/ServerIndex.cpp Mon May 11 15:49:34 2026 +0200 @@ -460,14 +460,14 @@ { Logging::ScopedThreadNameSetter setter("UNSTABLE-MON"); - int stableAge; + unsigned int stableAge = 0; { OrthancConfiguration::ReaderLock lock; stableAge = lock.GetConfiguration().GetUnsignedIntegerParameter("StableAge", 60); } - if (stableAge <= 0) + if (stableAge <= 1) { stableAge = 60; }
--- a/OrthancServer/UnitTestsSources/ServerJobsTests.cpp Mon May 11 12:32:58 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerJobsTests.cpp Mon May 11 15:49:34 2026 +0200 @@ -430,8 +430,8 @@ operation.Serialize(s); } - ASSERT_THROW(unserializer.UnserializeJob(s), OrthancException); - ASSERT_THROW(unserializer.UnserializeValue(s), OrthancException); + ASSERT_THROW(unserializer.UnserializeJob(s), OrthancException); // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks) + ASSERT_THROW(unserializer.UnserializeValue(s), OrthancException); // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks) { std::unique_ptr<IJobOperation> operation;
