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.
This commit is contained in:
Bert Münnich 2017-11-23 14:35:32 +01:00
parent 9dabc5f988
commit f02661879f
3 changed files with 26 additions and 18 deletions

View file

@ -1,4 +1,4 @@
VERSION = 24 VERSION = git-20171123
srcdir = . srcdir = .
VPATH = $(srcdir) VPATH = $(srcdir)

32
image.c
View file

@ -293,21 +293,35 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
} }
#endif /* HAVE_GIFLIB */ #endif /* HAVE_GIFLIB */
Imlib_Image img_open(const fileinfo_t *file)
{
struct stat st;
Imlib_Image im = NULL;
if (access(file->path, R_OK) == 0 &&
stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
{
im = imlib_load_image(file->path);
if (im != NULL) {
imlib_context_set_image(im);
if (imlib_image_get_data_for_reading_only() == NULL) {
imlib_free_image();
im = NULL;
}
}
}
if (im == NULL && (file->flags & FF_WARN))
error(0, 0, "%s: Error opening image", file->name);
return im;
}
bool img_load(img_t *img, const fileinfo_t *file) bool img_load(img_t *img, const fileinfo_t *file)
{ {
const char *fmt; const char *fmt;
struct stat st;
if (access(file->path, R_OK) == -1 || if ((img->im = img_open(file)) == NULL)
stat(file->path, &st) == -1 || !S_ISREG(st.st_mode) ||
(img->im = imlib_load_image(file->path)) == NULL)
{
if (file->flags & FF_WARN)
error(0, 0, "%s: Error opening image", file->name);
return false; return false;
}
imlib_context_set_image(img->im);
imlib_image_set_changes_on_disk(); imlib_image_set_changes_on_disk();
#if HAVE_LIBEXIF #if HAVE_LIBEXIF

View file

@ -32,6 +32,7 @@
#include <libexif/exif-data.h> #include <libexif/exif-data.h>
void exif_auto_orientate(const fileinfo_t*); void exif_auto_orientate(const fileinfo_t*);
#endif #endif
Imlib_Image img_open(const fileinfo_t*);
static char *cache_dir; static char *cache_dir;
@ -237,7 +238,6 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
char *cfile; char *cfile;
thumb_t *t; thumb_t *t;
fileinfo_t *file; fileinfo_t *file;
struct stat st;
Imlib_Image im = NULL; Imlib_Image im = NULL;
if (n < 0 || n >= *tns->cnt) if (n < 0 || n >= *tns->cnt)
@ -331,15 +331,9 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
} }
if (im == NULL) { if (im == NULL) {
if (access(file->path, R_OK) == -1 || if ((im = img_open(file)) == NULL)
stat(file->path, &st) == -1 || !S_ISREG(st.st_mode) ||
(im = imlib_load_image(file->path)) == NULL)
{
if (file->flags & FF_WARN)
error(0, 0, "%s: Error opening image", file->name);
return false; return false;
} }
}
imlib_context_set_image(im); imlib_context_set_image(im);
if (!cache_hit) { if (!cache_hit) {