Krolyxon
Commit f026618

Branches

Refs →

nsxivf026618

Reject text files resembling TGA images

Fixes issue #295

The imlib2 TGA loader returns an imlib image handle without any actual data
when given a text file like this:

    T
    Content-Type: application/javascript
    Content-Length: 3836
    Last-Modified: Wed, 23 Sep 2015 12:25:47 GMT
    Etag: "56029a4b-efc"
    Expires: Sat, 20 Aug 2016 15:14:33 GMT
    Cache-Control: max-age=604800, public
    Accept-Ranges: bytes

Fortunately, `imlib_image_get_data()` returns NULL in this case, so that we can
use it as an additional check when opening files.
Author
Bert Münnich
Date
2017-11-23 14:35 +0100
Committer
Bert Münnich · 2017-11-23 14:35 +0100
Parent
9dabc5f
Commit
f02661879ff0efefc6751d927c2e721e64ad79f0

+26 −18

3 files changed

  1. M Makefile +1 −1
  2. M image.c +23 −9
  3. M thumbs.c +2 −8
M Makefile View
@@ -1,4 +1,4 @@
1−VERSION = 24
1+VERSION = git-20171123
22
33 srcdir = .
44 VPATH = $(srcdir)
M image.c View
@@ -293,21 +293,35 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
293293 }
294294 #endif /* HAVE_GIFLIB */
295295
296−bool img_load(img_t *img, const fileinfo_t *file)
296+Imlib_Image img_open(const fileinfo_t *file)
297297 {
298− const char *fmt;
299298 struct stat st;
299+ Imlib_Image im = NULL;
300300
301− if (access(file->path, R_OK) == -1 ||
302− stat(file->path, &st) == -1 || !S_ISREG(st.st_mode) ||
303− (img->im = imlib_load_image(file->path)) == NULL)
301+ if (access(file->path, R_OK) == 0 &&
302+ stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
304303 {
305− if (file->flags & FF_WARN)
306− error(0, 0, "%s: Error opening image", file->name);
307− return false;
304+ im = imlib_load_image(file->path);
305+ if (im != NULL) {
306+ imlib_context_set_image(im);
307+ if (imlib_image_get_data_for_reading_only() == NULL) {
308+ imlib_free_image();
309+ im = NULL;
310+ }
311+ }
308312 }
313+ if (im == NULL && (file->flags & FF_WARN))
314+ error(0, 0, "%s: Error opening image", file->name);
315+ return im;
316+}
317+
318+bool img_load(img_t *img, const fileinfo_t *file)
319+{
320+ const char *fmt;
321+
322+ if ((img->im = img_open(file)) == NULL)
323+ return false;
309324
310− imlib_context_set_image(img->im);
311325 imlib_image_set_changes_on_disk();
312326
313327 #if HAVE_LIBEXIF
M thumbs.c View
@@ -32,6 +32,7 @@
3232 #include <libexif/exif-data.h>
3333 void exif_auto_orientate(const fileinfo_t*);
3434 #endif
35+Imlib_Image img_open(const fileinfo_t*);
3536
3637 static char *cache_dir;
3738
@@ -237,7 +238,6 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
237238 char *cfile;
238239 thumb_t *t;
239240 fileinfo_t *file;
240− struct stat st;
241241 Imlib_Image im = NULL;
242242
243243 if (n < 0 || n >= *tns->cnt)
@@ -331,14 +331,8 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
331331 }
332332
333333 if (im == NULL) {
334− if (access(file->path, R_OK) == -1 ||
335− stat(file->path, &st) == -1 || !S_ISREG(st.st_mode) ||
336− (im = imlib_load_image(file->path)) == NULL)
337− {
338− if (file->flags & FF_WARN)
339− error(0, 0, "%s: Error opening image", file->name);
334+ if ((im = img_open(file)) == NULL)
340335 return false;
341− }
342336 }
343337 imlib_context_set_image(im);
344338