From 879718eadc186aef960e4c0581395cd937f0b9cb Mon Sep 17 00:00:00 2001 From: Fedor Chelnokov Date: Thu, 29 Jan 2026 21:20:32 +0300 Subject: [PATCH] Image::sample avoids invalid memory access if the image is empty --- source/MRMesh/MRImage.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/source/MRMesh/MRImage.cpp b/source/MRMesh/MRImage.cpp index a7e324e2a74b..aa4928313960 100644 --- a/source/MRMesh/MRImage.cpp +++ b/source/MRMesh/MRImage.cpp @@ -5,6 +5,11 @@ namespace MR Color Image::sampleDiscrete( const UVCoord & pos ) const { + if ( resolution.x <= 0 || resolution.y <= 0 ) + { + assert( false ); + return {}; + } const float x = std::clamp( pos.x, 0.0f, 1.0f ) * ( resolution.x - 1 ); const float y = std::clamp( pos.y, 0.0f, 1.0f ) * ( resolution.y - 1 ); return operator[]( { (int)std::lround( x ), (int)std::lround( y ) } ); @@ -12,6 +17,11 @@ Color Image::sampleDiscrete( const UVCoord & pos ) const Color Image::sampleBilinear( const UVCoord & pos ) const { + if ( resolution.x <= 0 || resolution.y <= 0 ) + { + assert( false ); + return {}; + } const float x = std::clamp( pos.x, 0.0f, 1.0f ) * ( resolution.x - 1 ); const float y = std::clamp( pos.y, 0.0f, 1.0f ) * ( resolution.y - 1 );