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.
+26 −18
3 files changed
| @@ -1,4 +1,4 @@ | |||
| 1 | − | VERSION = 24 | |
| 1 | + | VERSION = git-20171123 | |
| 2 | 2 | ||
| 3 | 3 | srcdir = . | |
| 4 | 4 | VPATH = $(srcdir) | |
| @@ -293,21 +293,35 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 293 | 293 | } | |
| 294 | 294 | #endif /* HAVE_GIFLIB */ | |
| 295 | 295 | ||
| 296 | − | bool img_load(img_t *img, const fileinfo_t *file) | |
| 296 | + | Imlib_Image img_open(const fileinfo_t *file) | |
| 297 | 297 | { | |
| 298 | − | const char *fmt; | |
| 299 | 298 | struct stat st; | |
| 299 | + | Imlib_Image im = NULL; | |
| 300 | 300 | ||
| 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)) | |
| 304 | 303 | { | |
| 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 | + | } | |
| 308 | 312 | } | |
| 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; | |
| 309 | 324 | ||
| 310 | − | imlib_context_set_image(img->im); | |
| 311 | 325 | imlib_image_set_changes_on_disk(); | |
| 312 | 326 | ||
| 313 | 327 | #if HAVE_LIBEXIF | |
| @@ -32,6 +32,7 @@ | |||
| 32 | 32 | #include <libexif/exif-data.h> | |
| 33 | 33 | void exif_auto_orientate(const fileinfo_t*); | |
| 34 | 34 | #endif | |
| 35 | + | Imlib_Image img_open(const fileinfo_t*); | |
| 35 | 36 | ||
| 36 | 37 | static char *cache_dir; | |
| 37 | 38 | ||
| @@ -237,7 +238,6 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only) | |||
| 237 | 238 | char *cfile; | |
| 238 | 239 | thumb_t *t; | |
| 239 | 240 | fileinfo_t *file; | |
| 240 | − | struct stat st; | |
| 241 | 241 | Imlib_Image im = NULL; | |
| 242 | 242 | ||
| 243 | 243 | if (n < 0 || n >= *tns->cnt) | |
| @@ -331,14 +331,8 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only) | |||
| 331 | 331 | } | |
| 332 | 332 | ||
| 333 | 333 | 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) | |
| 340 | 335 | return false; | |
| 341 | − | } | |
| 342 | 336 | } | |
| 343 | 337 | imlib_context_set_image(im); | |
| 344 | 338 | ||