commit 0b1efab791ac446c3e44060e7988f1b42cc2f3eb
parent 2f4a1c4bec04ae94b6b19f84512c2f07b8bb252e
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sat, 24 Apr 2021 20:32:47 -0700
Fix segmentation fault
When the first given image was invalid, reloadimg was called which
assumed the dpy was not NULL.
Diffstat:
M | lel.c | | | 60 | ++++++++++++++++++++++++++++-------------------------------- |
1 file changed, 28 insertions(+), 32 deletions(-)
diff --git a/lel.c b/lel.c
@@ -24,7 +24,6 @@
enum { NONE = 0, LOADED = 1, SCALED = 2, DRAWN = 4 };
struct img {
- int index;
int state;
int width;
int height;
@@ -60,6 +59,7 @@ static float zoominc = 0.25;
static int paninc = 20;
static char **imgs;
+static int cimg;
static int done;
static int lastimg;
static int nimgs;
@@ -136,9 +136,10 @@ static void
ff_close(struct img *img)
{
img->state &= ~LOADED;
- if (img->buf)
+ if (img->buf) {
free(img->buf);
- img->buf = NULL;
+ img->buf = NULL;
+ }
}
static int
@@ -205,11 +206,11 @@ getbin(char *s)
static void
deleteimg(void)
{
- imgs[img.index] = NULL;
+ imgs[cimg] = NULL;
if (--nimgs <= 0)
exit(0);
nextimg();
- if (imgs[img.index] == NULL)
+ if (imgs[cimg] == NULL)
previmg();
}
@@ -221,60 +222,56 @@ loadimg(void)
FILE *fp;
char *argv[2];
- d = open(imgs[img.index], O_RDONLY);
- if (d == -1)
- die(imgs[img.index]);
+ d = open(imgs[cimg], O_RDONLY);
+ if (d == -1) {
+badimage:
+ warn(imgs[cimg]);
+ deleteimg();
+ return;
+ }
- *argv = getbin(imgs[img.index]);
+ *argv = getbin(imgs[cimg]);
if (*argv) {
argv[1] = NULL;
tmp = filter(d, argv);
if (close(d) || tmp == -1)
- die("proc");
+ goto badimage;
d = tmp;
}
fp = fdopen(d, "r");
- if (fp == NULL || ff_open(&img, fp) || ff_read(&img, fp) || fclose(fp)) {
- warn(imgs[img.index]);
- deleteimg();
- }
-
+ if (fp == NULL || ff_open(&img, fp) || ff_read(&img, fp) || fclose(fp))
+ goto badimage;
img.state &= ~(DRAWN | SCALED);
-}
-static void
-reloadimg(void)
-{
- loadimg();
XResizeWindow(dpy, win, img.width, img.height);
- XStoreName(dpy, win, imgs[img.index]);
+ XStoreName(dpy, win, imgs[cimg]);
XFlush(dpy);
}
static void
nextimg(void)
{
- if (img.index + 1 > lastimg)
+ if (cimg + 1 > lastimg)
return;
- if (imgs[++img.index] == NULL) {
+ if (imgs[++cimg] == NULL) {
nextimg();
} else {
ff_close(&img);
- reloadimg();
+ loadimg();
}
}
static void
previmg(void)
{
- if (img.index - 1 < 0)
+ if (cimg - 1 < 0)
return;
- if (imgs[--img.index] == NULL) {
+ if (imgs[--cimg] == NULL) {
previmg();
} else {
ff_close(&img);
- reloadimg();
+ loadimg();
}
}
@@ -487,7 +484,7 @@ buttonpress(XEvent *ev)
static void
printname(void)
{
- printf("%s\n", imgs[img.index]);
+ printf("%s\n", imgs[cimg]);
}
static void
@@ -616,14 +613,14 @@ setup(void)
xfd = ConnectionNumber(dpy);
screen = DefaultScreen(dpy);
- win = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, img.width, img.height, 0,
+ win = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, 720, 480, 0,
DefaultDepth(dpy, screen), InputOutput,
CopyFromParent, 0, NULL);
gc = XCreateGC(dpy, win, 0, NULL);
cmap = DefaultColormap(dpy, screen);
if (!XAllocNamedColor(dpy, cmap, bgcolor, &bg, &bg))
die("unable to allocate color");
- XStoreName(dpy, win, imgs[img.index]);
+ XStoreName(dpy, win, imgs[cimg]);
XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask |
ButtonPressMask);
XMapRaised(dpy, win);
@@ -661,7 +658,6 @@ main(int argc, char *argv[])
}
img.view.zoomfact = 1.0;
-
imgs = argv + optind;
nimgs = argc - optind;
if (!nimgs) {
@@ -670,8 +666,8 @@ main(int argc, char *argv[])
}
lastimg = nimgs - 1;
- loadimg();
setup();
+ loadimg();
run();
return 0;