Skip to content

Commit eb7f3c9

Browse files
authored
delete m_Data before overwriting the pointer (#1294)
1 parent fea0326 commit eb7f3c9

1 file changed

Lines changed: 36 additions & 27 deletions

File tree

IccProfLib/IccSparseMatrix.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ CIccSparseMatrix::CIccSparseMatrix(const CIccSparseMatrix &mtx)
3636
if (mtx.m_Data) {
3737
switch (m_nType) {
3838
case icSparseMatrixUInt8:
39-
m_Data = new CIccSparseMatrixUInt8();
39+
m_Data = new (std::nothrow)CIccSparseMatrixUInt8();
4040
break;
4141
case icSparseMatrixUInt16:
42-
m_Data = new CIccSparseMatrixUInt16();
42+
m_Data = new (std::nothrow)CIccSparseMatrixUInt16();
4343
break;
4444
case icSparseMatrixFloat16:
45-
m_Data = new CIccSparseMatrixFloat16();
45+
m_Data = new (std::nothrow)CIccSparseMatrixFloat16();
4646
break;
4747
case icSparseMatrixFloat32:
48-
m_Data = new CIccSparseMatrixFloat32();
48+
m_Data = new (std::nothrow)CIccSparseMatrixFloat32();
4949
break;
5050
case icSparseMatrixFloatNum:
51-
m_Data = new CIccSparseMatrixFloatNum();
51+
m_Data = new (std::nothrow)CIccSparseMatrixFloatNum();
5252
break; // without this, we have a memory leak!
5353
default:
5454
m_Data = NULL;
@@ -78,24 +78,25 @@ CIccSparseMatrix &CIccSparseMatrix::operator=(const CIccSparseMatrix &mtx)
7878
m_nRawSize = mtx.m_nRawSize;
7979
m_nType = mtx.m_nType;
8080

81-
if (mtx.m_Data) {
82-
delete m_Data;
81+
delete m_Data;
82+
m_Data = NULL;
8383

84+
if (mtx.m_Data) {
8485
switch (m_nType) {
8586
case icSparseMatrixUInt8:
86-
m_Data = new CIccSparseMatrixUInt8();
87+
m_Data = new (std::nothrow)CIccSparseMatrixUInt8();
8788
break;
8889
case icSparseMatrixUInt16:
89-
m_Data = new CIccSparseMatrixUInt16();
90+
m_Data = new (std::nothrow)CIccSparseMatrixUInt16();
9091
break;
9192
case icSparseMatrixFloat16:
92-
m_Data = new CIccSparseMatrixFloat16();
93+
m_Data = new (std::nothrow)CIccSparseMatrixFloat16();
9394
break;
9495
case icSparseMatrixFloat32:
95-
m_Data = new CIccSparseMatrixFloat32();
96+
m_Data = new (std::nothrow)CIccSparseMatrixFloat32();
9697
break;
9798
case icSparseMatrixFloatNum:
98-
m_Data = new CIccSparseMatrixFloatNum();
99+
m_Data = new (std::nothrow)CIccSparseMatrixFloatNum();
99100
break; // without this, we have a memory leak!
100101
default:
101102
m_Data = NULL;
@@ -154,6 +155,7 @@ bool CIccSparseMatrix::Init(icUInt16Number nRows, icUInt16Number nCols, bool bSe
154155
if (nRows > kMaxSparseMatrixDim || nCols > kMaxSparseMatrixDim) {
155156
m_nRows = 0;
156157
m_nCols = 0;
158+
delete m_Data;
157159
m_Data = NULL;
158160
m_RowStart = NULL;
159161
m_ColumnIndices = NULL;
@@ -164,36 +166,42 @@ bool CIccSparseMatrix::Init(icUInt16Number nRows, icUInt16Number nCols, bool bSe
164166
icUInt16Number *Dim = (icUInt16Number*)m_pMatrix;
165167

166168
delete m_Data;
169+
m_Data = NULL;
167170

168171
switch (m_nType) {
169172
case icSparseMatrixUInt8:
170-
m_Data = new CIccSparseMatrixUInt8();
173+
m_Data = new (std::nothrow)CIccSparseMatrixUInt8();
171174
break;
172175
case icSparseMatrixUInt16:
173-
m_Data = new CIccSparseMatrixUInt16();
176+
m_Data = new (std::nothrow)CIccSparseMatrixUInt16();
174177
break;
175178
case icSparseMatrixFloat16:
176-
m_Data = new CIccSparseMatrixFloat16();
179+
m_Data = new (std::nothrow)CIccSparseMatrixFloat16();
177180
break;
178181
case icSparseMatrixFloat32:
179-
m_Data = new CIccSparseMatrixFloat32();
182+
m_Data = new (std::nothrow)CIccSparseMatrixFloat32();
180183
break;
181184
case icSparseMatrixFloatNum:
182-
m_Data = new CIccSparseMatrixFloatNum();
185+
m_Data = new (std::nothrow)CIccSparseMatrixFloatNum();
183186
break;
184187
default:
185-
m_nRows = 0;
186-
m_nCols = 0;
187-
if (bSetData) {
188-
Dim[0] = 0;
189-
Dim[1] = 0;
190-
}
191188
m_Data = NULL;
192-
m_RowStart = NULL;
193-
m_ColumnIndices = NULL;
194-
m_nMaxEntries = 0;
195-
return false;
189+
break;
190+
}
191+
192+
if (!m_Data) {
193+
m_nRows = 0;
194+
m_nCols = 0;
195+
if (bSetData) {
196+
Dim[0] = 0;
197+
Dim[1] = 0;
198+
}
199+
m_RowStart = NULL;
200+
m_ColumnIndices = NULL;
201+
m_nMaxEntries = 0;
202+
return false;
196203
}
204+
197205
m_nRows = nRows;
198206
m_nCols = nCols;
199207
if (bSetData) {
@@ -211,6 +219,7 @@ bool CIccSparseMatrix::Init(icUInt16Number nRows, icUInt16Number nCols, bool bSe
211219
Dim[0] = 0;
212220
Dim[1] = 0;
213221
}
222+
delete m_Data;
214223
m_Data = NULL;
215224
m_RowStart = NULL;
216225
m_ColumnIndices = NULL;

0 commit comments

Comments
 (0)