Skip to content

Commit 0fde65a

Browse files
authored
Fix memory leak in LLImageDimensionsInfo (#2679)
1 parent b48c1b5 commit 0fde65a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

indra/llimage/llimagedimensionsinfo.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool LLImageDimensionsInfo::load(const std::string& src_filename,U32 codec)
7575
bool LLImageDimensionsInfo::getImageDimensionsBmp()
7676
{
7777
// Make sure the file is long enough.
78-
const S32 DATA_LEN = 26; // BMP header (14) + DIB header size (4) + width (4) + height (4)
78+
constexpr S32 DATA_LEN = 26; // BMP header (14) + DIB header size (4) + width (4) + height (4)
7979
if (!checkFileLength(DATA_LEN))
8080
{
8181
LL_WARNS() << "Premature end of file" << LL_ENDL;
@@ -105,7 +105,7 @@ bool LLImageDimensionsInfo::getImageDimensionsBmp()
105105

106106
bool LLImageDimensionsInfo::getImageDimensionsTga()
107107
{
108-
const S32 TGA_FILE_HEADER_SIZE = 12;
108+
constexpr S32 TGA_FILE_HEADER_SIZE = 12;
109109

110110
// Make sure the file is long enough.
111111
if (!checkFileLength(TGA_FILE_HEADER_SIZE + 1 /* width */ + 1 /* height */))
@@ -124,7 +124,7 @@ bool LLImageDimensionsInfo::getImageDimensionsTga()
124124

125125
bool LLImageDimensionsInfo::getImageDimensionsPng()
126126
{
127-
const S32 PNG_MAGIC_SIZE = 8;
127+
constexpr S32 PNG_MAGIC_SIZE = 8;
128128

129129
// Make sure the file is long enough.
130130
if (!checkFileLength(PNG_MAGIC_SIZE + 8 + sizeof(S32) * 2 /* width, height */))
@@ -134,7 +134,7 @@ bool LLImageDimensionsInfo::getImageDimensionsPng()
134134
}
135135

136136
// Read PNG signature.
137-
const U8 png_magic[PNG_MAGIC_SIZE] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
137+
constexpr U8 png_magic[PNG_MAGIC_SIZE] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
138138
U8 signature[PNG_MAGIC_SIZE];
139139
mInfile.read((void*)signature, PNG_MAGIC_SIZE);
140140

@@ -166,34 +166,36 @@ bool LLImageDimensionsInfo::getImageDimensionsJpeg()
166166
{
167167
sJpegErrorEncountered = false;
168168
clean();
169-
FILE *fp = LLFile::fopen(mSrcFilename, "rb");
170-
if (fp == NULL)
169+
FILE* fp = LLFile::fopen(mSrcFilename, "rb");
170+
if (!fp)
171171
{
172172
setLastError("Unable to open file for reading", mSrcFilename);
173173
return false;
174174
}
175175

176176
/* Make sure this is a JPEG file. */
177-
const size_t JPEG_MAGIC_SIZE = 2;
178-
const U8 jpeg_magic[JPEG_MAGIC_SIZE] = {0xFF, 0xD8};
177+
constexpr size_t JPEG_MAGIC_SIZE = 2;
178+
constexpr U8 jpeg_magic[JPEG_MAGIC_SIZE] = {0xFF, 0xD8};
179179
U8 signature[JPEG_MAGIC_SIZE];
180180

181181
if (fread(signature, sizeof(signature), 1, fp) != 1)
182182
{
183183
LL_WARNS() << "Premature end of file" << LL_ENDL;
184+
fclose(fp);
184185
return false;
185186
}
186187
if (memcmp(signature, jpeg_magic, JPEG_MAGIC_SIZE) != 0)
187188
{
188189
LL_WARNS() << "Not a JPEG" << LL_ENDL;
189190
mWarning = "texture_load_format_error";
191+
fclose(fp);
190192
return false;
191193
}
192194
fseek(fp, 0, SEEK_SET); // go back to start of the file
193195

194196
/* Init jpeg */
195197
jpeg_error_mgr jerr;
196-
jpeg_decompress_struct cinfo;
198+
jpeg_decompress_struct cinfo{};
197199
cinfo.err = jpeg_std_error(&jerr);
198200
// Call our function instead of exit() if Libjpeg encounters an error.
199201
// This is done to avoid crash in this case (STORM-472).

indra/llimage/llimagedimensionsinfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LLImageDimensionsInfo
3838
{
3939
public:
4040
LLImageDimensionsInfo():
41-
mData(NULL)
41+
mData(nullptr)
4242
,mHeight(0)
4343
,mWidth(0)
4444
{}
@@ -67,7 +67,7 @@ class LLImageDimensionsInfo
6767
{
6868
mInfile.close();
6969
delete[] mData;
70-
mData = NULL;
70+
mData = nullptr;
7171
mWidth = 0;
7272
mHeight = 0;
7373
}

0 commit comments

Comments
 (0)