summaryrefslogtreecommitdiffstats
path: root/image.c
diff options
context:
space:
mode:
authorBert Münnich <ber.t@posteo.de>2017-11-23 14:35:32 +0100
committerBert Münnich <ber.t@posteo.de>2017-11-23 14:35:34 +0100
commitf02661879ff0efefc6751d927c2e721e64ad79f0 (patch)
tree863e487a8dbb6621616793adb762090eb10dbe38 /image.c
parent9dabc5f9883b80286b91f73ea4dcf9fd3d1ad11c (diff)
downloadnsxiv-f02661879ff0efefc6751d927c2e721e64ad79f0.tar.zst
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.
Diffstat (limited to 'image.c')
-rw-r--r--image.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/image.c b/image.c
index 75dc346..6803846 100644
--- a/image.c
+++ b/image.c
@@ -293,21 +293,35 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
}
#endif /* HAVE_GIFLIB */
-bool img_load(img_t *img, const fileinfo_t *file)
+Imlib_Image img_open(const fileinfo_t *file)
{
- const char *fmt;
struct stat st;
+ Imlib_Image im = NULL;
- if (access(file->path, R_OK) == -1 ||
- stat(file->path, &st) == -1 || !S_ISREG(st.st_mode) ||
- (img->im = imlib_load_image(file->path)) == NULL)
+ if (access(file->path, R_OK) == 0 &&
+ stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
{
- if (file->flags & FF_WARN)
- error(0, 0, "%s: Error opening image", file->name);
- return false;
+ 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)
+{
+ const char *fmt;
+
+ if ((img->im = img_open(file)) == NULL)
+ return false;
- imlib_context_set_image(img->im);
imlib_image_set_changes_on_disk();
#if HAVE_LIBEXIF