7171
7272#include < cstdio>
7373#include < string>
74+ #include < climits>
7475#include " IccProfile.h"
7576#include " IccTagBasic.h"
7677#include " IccTagMPE.h"
@@ -144,7 +145,10 @@ class CubeFile
144145 return false ;
145146 }
146147 else if (line.substr (0 , 12 ) == " LUT_3D_SIZE " ) {
147- m_sizeLut3D = atoi (line.c_str () + 12 );
148+ int64_t temp = atoll ( line.c_str () + 12 );
149+ if (temp >= INT_MAX || temp <= 0 )
150+ return false ;
151+ m_sizeLut3D = (int )temp;
148152 }
149153 else if (line.substr (0 , 19 ) == " LUT_3D_INPUT_RANGE " ) {
150154 m_fMinInput[0 ] = m_fMinInput[1 ] = m_fMinInput[2 ] = (icFloatNumber)atof (line.c_str () + 19 );
@@ -215,10 +219,15 @@ class CubeFile
215219 int sizeLut3D () { return m_sizeLut3D; }
216220 bool parse3DTable (icFloatNumber* toLut, icUInt32Number nSizeLut)
217221 {
218- icUInt32Number num = m_sizeLut3D * m_sizeLut3D * m_sizeLut3D;
222+ if (m_sizeLut3D < 2 || nSizeLut <= 0 )
223+ return false ;
224+
225+ uint64_t temp = (uint64_t )m_sizeLut3D * (uint64_t )m_sizeLut3D * (uint64_t )m_sizeLut3D;
226+ if (temp > UINT_MAX)
227+ return false ;
228+ icUInt32Number num = (icUInt32Number)temp;
219229
220- //
221- if (!m_sizeLut3D || nSizeLut != num*3 )
230+ if (nSizeLut != num*3 )
222231 return false ;
223232
224233 const char * next;
@@ -406,8 +415,17 @@ int main(int argc, char* argv[])
406415
407416 CIccMpeCLUT* pMpeCLUT = new CIccMpeCLUT ();
408417 CIccCLUT* pCLUT = new CIccCLUT (3 , 3 );
409- pCLUT->Init (cube.sizeLut3D ());
418+
419+ if (!pCLUT->Init (cube.sizeLut3D ()) ) {
420+ printf (" Unable to create LUT from '%s'\n " , argv[1 ]);
421+ return -4 ;
422+ }
423+
410424 bool bSuccess = cube.parse3DTable (pCLUT->GetData (0 ), pCLUT->NumPoints ()*3 );
425+ if (!bSuccess) {
426+ printf (" Unable to parse LUT from '%s'\n " , argv[1 ]);
427+ return (-4 );
428+ }
411429
412430 pMpeCLUT->SetCLUT (pCLUT);
413431 pTag->Attach (pMpeCLUT);
@@ -416,11 +434,6 @@ int main(int argc, char* argv[])
416434
417435 cube.close ();
418436
419- if (!bSuccess) {
420- printf (" Unable to parse LUT from '%s'\n " , argv[1 ]);
421- return (-4 );
422- }
423-
424437 // Add description Tag
425438 CIccTagMultiLocalizedUnicode* pTextTag = new CIccTagMultiLocalizedUnicode ();
426439 std::string desc = cube.getDescription ();
0 commit comments