# HG changeset patch # User Sebastien Jodogne # Date 1774013835 -3600 # Node ID 68675600a967581d80a2453bc8c1cd8c37379e83 # Parent 2640cf0fba208ded6e9da989ae887181daac7f0f added safeguards in ImageAccessor.cpp diff -r 2640cf0fba20 -r 68675600a967 OrthancFramework/Sources/Images/ImageAccessor.cpp --- a/OrthancFramework/Sources/Images/ImageAccessor.cpp Fri Mar 20 14:17:28 2026 +0100 +++ b/OrthancFramework/Sources/Images/ImageAccessor.cpp Fri Mar 20 14:37:15 2026 +0100 @@ -165,7 +165,14 @@ { if (buffer_ != NULL) { - return buffer_ + static_cast(y) * static_cast(pitch_); + if (y < height_) + { + return buffer_ + static_cast(y) * static_cast(pitch_); + } + else + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } } else { @@ -184,7 +191,14 @@ if (buffer_ != NULL) { - return buffer_ + static_cast(y) * static_cast(pitch_); + if (y < height_) + { + return buffer_ + static_cast(y) * static_cast(pitch_); + } + else + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } } else { @@ -210,17 +224,20 @@ unsigned int pitch, const void *buffer) { + const uint64_t size = static_cast(height) * static_cast(pitch); + + if (static_cast(GetBytesPerPixel() * width) > static_cast(pitch) || + static_cast(static_cast(size)) != size) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + readOnly_ = true; format_ = format; width_ = width; height_ = height; pitch_ = pitch; buffer_ = reinterpret_cast(const_cast(buffer)); - - if (GetBytesPerPixel() * width_ > pitch_) - { - throw OrthancException(ErrorCode_ParameterOutOfRange); - } } void ImageAccessor::GetReadOnlyAccessor(ImageAccessor &target) const @@ -235,17 +252,20 @@ unsigned int pitch, void *buffer) { + const uint64_t size = static_cast(height) * static_cast(pitch); + + if (static_cast(GetBytesPerPixel() * width) > static_cast(pitch) || + static_cast(static_cast(size)) != size) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + readOnly_ = false; format_ = format; width_ = width; height_ = height; pitch_ = pitch; buffer_ = reinterpret_cast(buffer); - - if (GetBytesPerPixel() * width_ > pitch_) - { - throw OrthancException(ErrorCode_ParameterOutOfRange); - } } diff -r 2640cf0fba20 -r 68675600a967 OrthancFramework/Sources/Images/ImageBuffer.cpp --- a/OrthancFramework/Sources/Images/ImageBuffer.cpp Fri Mar 20 14:17:28 2026 +0100 +++ b/OrthancFramework/Sources/Images/ImageBuffer.cpp Fri Mar 20 14:37:15 2026 +0100 @@ -46,8 +46,16 @@ } */ - pitch_ = GetBytesPerPixel() * width_; - size_t size = static_cast(pitch_) * static_cast(height_); + const uint64_t tmpPitch = static_cast(GetBytesPerPixel()) * static_cast(width_); + const uint64_t size = tmpPitch * static_cast(height_); + + if (static_cast(static_cast(tmpPitch)) != tmpPitch || + static_cast(static_cast(size)) != size) + { + throw OrthancException(ErrorCode_NotEnoughMemory); + } + + pitch_ = static_cast(tmpPitch); if (size == 0) { @@ -55,7 +63,7 @@ } else { - buffer_ = malloc(size); + buffer_ = malloc(static_cast(size)); if (buffer_ == NULL) { throw OrthancException(ErrorCode_NotEnoughMemory,