Skip to content

Commit c3746eb

Browse files
authored
Merge pull request #48 from lordnn/master
Fixed some vulnerabilities.
2 parents 79a22b5 + 6c18bb6 commit c3746eb

4 files changed

Lines changed: 24 additions & 28 deletions

File tree

Source/Plugins/PluginPNG.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,6 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
769769

770770
static FIBOOL DLL_CALLCONV
771771
Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
772-
png_colorp palette{};
773772
png_uint_32 width, height;
774773
FIBOOL has_alpha_channel = FALSE;
775774

@@ -808,6 +807,8 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
808807
return FALSE;
809808
}
810809

810+
std::unique_ptr<png_color, std::function<void(png_colorp)>> safePalette(nullptr, [&png_ptr](png_colorp p){ png_free(png_ptr.get(), p); });
811+
811812
// init the IO
812813

813814
png_set_write_fn(png_ptr.get(), &fio, _WriteProc, _FlushProc);
@@ -897,7 +898,8 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
897898
// set the palette
898899

899900
palette_entries = 1 << bit_depth;
900-
palette = (png_colorp)png_malloc(png_ptr.get(), palette_entries * sizeof (png_color));
901+
safePalette.reset(static_cast<png_colorp>(png_malloc(png_ptr.get(), palette_entries * sizeof (png_color))));
902+
auto palette = safePalette.get();
901903
pal = FreeImage_GetPalette(dib);
902904

903905
for (int i = 0; i < palette_entries; i++) {
@@ -1029,11 +1031,6 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
10291031

10301032
png_write_end(png_ptr.get(), info_ptr.get());
10311033

1032-
// clean up after the write, and free any memory allocated
1033-
if (palette) {
1034-
png_free(png_ptr.get(), palette);
1035-
}
1036-
10371034
return TRUE;
10381035

10391036
} catch (const char *text) {

Source/Plugins/PluginRAS.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
199199
FIBOOL isRGB; // TRUE if file type is RT_FORMAT_RGB
200200
uint8_t fillchar;
201201

202-
FIBITMAP *dib{};
203-
uint8_t *bits; // Pointer to dib data
202+
uint8_t *bits{}; // Pointer to dib data
204203
uint16_t x, y;
205204

206205
if (!handle) {
@@ -232,6 +231,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
232231
if (header.magic != RAS_MAGIC) {
233232
throw FI_MSG_ERROR_MAGIC_NUMBER;
234233
}
234+
if (header.width > 65500 || header.height > 65500) {
235+
throw FI_MSG_ERROR_DIB_MEMORY;
236+
}
235237

236238
// Allocate a new DIB
237239
std::unique_ptr<FIBITMAP, decltype(&FreeImage_Unload)> dib(nullptr, &FreeImage_Unload);
@@ -353,7 +355,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
353355
// Each row is multiple of 16 bits (2 bytes).
354356

355357
if (header.depth == 1) {
356-
linelength = (uint16_t)((header.width / 8) + (header.width % 8 ? 1 : 0));
358+
linelength = (uint16_t)((header.width + 7) / 8);
357359
} else {
358360
linelength = (uint16_t)header.width;
359361
}

Source/Plugins/PluginWebP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Open(FreeImageIO *io, fi_handle handle, FIBOOL read) {
175175
// create the MUX object
176176
mux = WebPMuxCreate(&bitstream, copy_data);
177177
// no longer needed since copy_data == 1
178-
free((void*)bitstream.bytes);
178+
delete[] bitstream.bytes;
179179
if (!mux) {
180180
FreeImage_OutputMessageProc(s_format_id, "Failed to create mux object from file");
181181
}

Source/Plugins/PluginXBM.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ readLine(char *str, int n, FreeImageIO *io, fi_handle handle) {
5656
do {
5757
count = io->read_proc(&c, 1, 1, handle);
5858
str[i++] = c;
59-
} while ((c != '\n') && (i < n));
59+
} while ((c != '\n') && (i < n - 1));
6060
if (count <= 0)
6161
return nullptr;
6262
str[i] = '\0';
@@ -88,34 +88,31 @@ Read an XBM file into a buffer
8888
static const char*
8989
readXBMFile(FreeImageIO *io, fi_handle handle, int *widthP, int *heightP, std::unique_ptr<void, decltype(&free)> &dataP) {
9090
char line[MAX_LINE], name_and_type[MAX_LINE];
91-
char* ptr;
92-
char* t;
91+
char *ptr{};
92+
char *t{};
9393
int version = 0;
9494
size_t bytes, bytes_per_line, raster_length;
9595
int v, padding;
9696
int c1, c2, value1, value2;
9797
int hex_table[256];
98-
FIBOOL found_declaration;
98+
bool found_declaration{}; // haven't found it yet; haven't even looked
9999
/* in scanning through the bitmap file, we have found the first
100100
line of the C declaration of the array (the "static char ..."
101101
or whatever line)
102102
*/
103-
FIBOOL eof; // we've encountered end of file while searching file
103+
bool eof{}; // we've encountered end of file while searching file
104104

105105
*widthP = *heightP = -1;
106106

107-
found_declaration = FALSE; // haven't found it yet; haven't even looked
108-
eof = FALSE; // haven't encountered end of file yet
109-
110107
while (!found_declaration && !eof) {
111108

112109
if (!readLine(line, MAX_LINE, io, handle)) {
113-
eof = TRUE;
110+
eof = true;
114111
}
115112
else {
116113
if (strlen(line) == MAX_LINE - 1)
117114
return( ERR_XBM_LINE );
118-
if (sscanf(line, "#define %s %d", name_and_type, &v) == 2) {
115+
if (sscanf_s(line, "#define %s %d", name_and_type, MAX_LINE, &v) == 2) {
119116
if ((t = strrchr(name_and_type, '_')) == nullptr)
120117
t = name_and_type;
121118
else
@@ -127,17 +124,17 @@ readXBMFile(FreeImageIO *io, fi_handle handle, int *widthP, int *heightP, std::u
127124
continue;
128125
}
129126

130-
if (sscanf(line, "static short %s = {", name_and_type) == 1) {
127+
if (sscanf_s(line, "static short %s = {", name_and_type, MAX_LINE) == 1) {
131128
version = 10;
132-
found_declaration = TRUE;
129+
found_declaration = true;
133130
}
134-
else if (sscanf( line, "static char %s = {", name_and_type ) == 1) {
131+
else if (sscanf_s(line, "static char %s = {", name_and_type, MAX_LINE) == 1) {
135132
version = 11;
136-
found_declaration = TRUE;
133+
found_declaration = true;
137134
}
138-
else if (sscanf(line, "static unsigned char %s = {", name_and_type ) == 1) {
135+
else if (sscanf_s(line, "static unsigned char %s = {", name_and_type, MAX_LINE) == 1) {
139136
version = 11;
140-
found_declaration = TRUE;
137+
found_declaration = true;
141138
}
142139
}
143140
}
@@ -292,7 +289,7 @@ MimeType() {
292289
static FIBOOL DLL_CALLCONV
293290
Validate(FreeImageIO *io, fi_handle handle) {
294291
char magic[8];
295-
if (readLine(magic, 7, io, handle)) {
292+
if (readLine(magic, 8, io, handle)) {
296293
if (strcmp(magic, "#define") == 0)
297294
return TRUE;
298295
}

0 commit comments

Comments
 (0)