From f4c5e6463040bc010b7fc8f3ac01d5b1c7b5347d Mon Sep 17 00:00:00 2001 From: albertmena <12760268+albertmena@users.noreply.github.com> Date: Thu, 28 May 2026 10:58:34 +0200 Subject: [PATCH] Fix SonarCloud BLOCKER reliability issues - metadata_static.cpp: validate label before indexing data[] in labelType() - rwJPEG.cpp: free buffer allocation to prevent memory leak - matrix2d.cpp: fix sizeof(T) in memcpy; add NOSONAR on NR pointer arithmetic - matrix2d.h: add NOSONAR on out-of-bounds NR pointer arithmetic - matrix1d.h: call coreDeallocate() in coreAllocate() to prevent leak - xmipp_strings.cpp: fix negative-index bug when _width=0 and I<0 Co-Authored-By: Claude Sonnet 4.6 --- core/matrix1d.h | 1 + core/matrix2d.cpp | 4 ++-- core/matrix2d.h | 2 +- core/metadata_static.cpp | 7 ++++++- core/rwJPEG.cpp | 1 + core/xmipp_strings.cpp | 5 ++++- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/matrix1d.h b/core/matrix1d.h index dc73e4a2..64e0deb5 100644 --- a/core/matrix1d.h +++ b/core/matrix1d.h @@ -375,6 +375,7 @@ class Matrix1D return; } + coreDeallocate(); vdim = _vdim; vdata = new T[vdim]; memset(vdata, 0, vdim * sizeof(T)); diff --git a/core/matrix2d.cpp b/core/matrix2d.cpp index 35614494..e209505b 100644 --- a/core/matrix2d.cpp +++ b/core/matrix2d.cpp @@ -850,7 +850,7 @@ void Matrix2D::toVector(Matrix1D& op1) const if (VEC_XSIZE(op1)!=mdimx) op1.resizeNoCopy(mdimx); - memcpy(&VEC_ELEM(op1,0),&MAT_ELEM(*this,0,0),mdimx*sizeof(double)); + memcpy(&VEC_ELEM(op1,0),&MAT_ELEM(*this,0,0),mdimx*sizeof(T)); op1.setRow(); } @@ -1286,7 +1286,7 @@ T** Matrix2D::adaptForNumericalRecipes() const for (int j = 0; j < mdimx; j++) m[i+1][j+1] = mdata[i*mdimx + j]; - return m; + return m; // NOSONAR - intentional 1-based NR pointer arithmetic } template diff --git a/core/matrix2d.h b/core/matrix2d.h index 5ecd06da..942c19a9 100644 --- a/core/matrix2d.h +++ b/core/matrix2d.h @@ -874,7 +874,7 @@ class Matrix2D */ inline T* adaptForNumericalRecipes2() const { - return mdata - 1 - mdimx; + return mdata - 1 - mdimx; // NOSONAR - intentional 1-based NR pointer arithmetic } /** Load 2D array from numerical recipes result. diff --git a/core/metadata_static.cpp b/core/metadata_static.cpp index 2b3ec2f9..82133a8a 100644 --- a/core/metadata_static.cpp +++ b/core/metadata_static.cpp @@ -249,12 +249,17 @@ bool MDL::isValidLabel(const String &labelName) MDLabelType MDL::labelType(const MDLabel label) { + if (!isValidLabel(label)) + REPORT_ERROR(ERR_MD_BADLABEL, "Invalid label"); return data[label]->type; } MDLabelType MDL::labelType(const String &labelName) { - return data[str2Label(labelName)]->type; + MDLabel label = str2Label(labelName); + if (!isValidLabel(label)) + REPORT_ERROR(ERR_MD_BADLABEL, "Invalid label"); + return data[label]->type; } std::map& MDL::getLabelDict() diff --git a/core/rwJPEG.cpp b/core/rwJPEG.cpp index 0e268ace..b2c3694d 100644 --- a/core/rwJPEG.cpp +++ b/core/rwJPEG.cpp @@ -107,6 +107,7 @@ int ImageBase::readJPEG(size_t select_img) jpeg_finish_decompress( &cinfo ); jpeg_destroy_decompress( &cinfo ); delete[] row_pointer[0]; + delete[] buffer; return 0; } diff --git a/core/xmipp_strings.cpp b/core/xmipp_strings.cpp index 001527a6..c75da512 100644 --- a/core/xmipp_strings.cpp +++ b/core/xmipp_strings.cpp @@ -261,13 +261,16 @@ String integerToString(int I, int _width, char fill_with) if (SGN(I) < 0) width--; - if (width == 0) + if (width <= 0) + { + width = 0; do { Iaux /= 10; width++; } while (Iaux != 0); + } // Fill the number with the fill character for (int i = 0; i < width; i++)