From e0e96977b348772cac67282a6591d6c58eeaa927 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bert=20M=C3=BCnnich?= <ber.t@posteo.de>
Date: Wed, 28 Oct 2015 20:59:48 +0100
Subject: [PATCH] Removed overcautious parameter checks

---
 Makefile |  2 +-
 image.c  | 55 +++----------------------------------------------------
 main.c   | 11 +++--------
 thumbs.c | 38 +-------------------------------------
 util.c   | 15 +++------------
 window.c | 37 -------------------------------------
 6 files changed, 11 insertions(+), 147 deletions(-)

diff --git a/Makefile b/Makefile
index 54342a3..127a418 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION   := git-20150819
+VERSION   := git-20151028
 
 PREFIX    := /usr/local
 MANPREFIX := $(PREFIX)/share/man
diff --git a/image.c b/image.c
index ddc75df..da46cab 100644
--- a/image.c
+++ b/image.c
@@ -51,9 +51,6 @@ void img_init(img_t *img, win_t *win)
 	zoom_min = zoom_levels[0] / 100.0;
 	zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
 
-	if (img == NULL || win == NULL)
-		return;
-
 	imlib_context_set_display(win->env.dpy);
 	imlib_context_set_visual(win->env.vis);
 	imlib_context_set_colormap(win->env.cmap);
@@ -300,9 +297,6 @@ bool img_load(img_t *img, const fileinfo_t *file)
 {
 	const char *fmt;
 
-	if (img == NULL || file == NULL || file->name == NULL || file->path == NULL)
-		return false;
-
 	if (access(file->path, R_OK) < 0 ||
 	    (img->im = imlib_load_image(file->path)) == NULL)
 	{
@@ -336,9 +330,6 @@ void img_close(img_t *img, bool decache)
 {
 	int i;
 
-	if (img == NULL)
-		return;
-
 	if (img->multi.cnt > 0) {
 		for (i = 0; i < img->multi.cnt; i++) {
 			imlib_context_set_image(img->multi.frames[i].im);
@@ -362,9 +353,6 @@ void img_check_pan(img_t *img, bool moved)
 	int ox, oy;
 	float w, h;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return;
-
 	win = img->win;
 	w = img->w * img->zoom;
 	h = img->h * img->zoom;
@@ -392,8 +380,6 @@ bool img_fit(img_t *img)
 {
 	float z, zmax, zw, zh;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return false;
 	if (img->scalemode == SCALE_ZOOM)
 		return false;
 
@@ -433,9 +419,6 @@ void img_render(img_t *img)
 	Imlib_Image bg;
 	unsigned long c;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return;
-
 	win = img->win;
 	img_fit(img);
 
@@ -521,9 +504,6 @@ bool img_fit_win(img_t *img, scalemode_t sm)
 {
 	float oz;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return false;
-
 	oz = img->zoom;
 	img->scalemode = sm;
 
@@ -539,9 +519,6 @@ bool img_fit_win(img_t *img, scalemode_t sm)
 
 bool img_zoom(img_t *img, float z)
 {
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return false;
-
 	z = MAX(z, zoom_min);
 	z = MIN(z, zoom_max);
 
@@ -564,9 +541,6 @@ bool img_zoom_in(img_t *img)
 	int i;
 	float z;
 
-	if (img == NULL || img->im == NULL)
-		return false;
-
 	for (i = 1; i < ARRLEN(zoom_levels); i++) {
 		z = zoom_levels[i] / 100.0;
 		if (zoomdiff(z, img->zoom) > 0)
@@ -580,9 +554,6 @@ bool img_zoom_out(img_t *img)
 	int i;
 	float z;
 
-	if (img == NULL || img->im == NULL)
-		return false;
-
 	for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
 		z = zoom_levels[i] / 100.0;
 		if (zoomdiff(z, img->zoom) < 0)
@@ -595,9 +566,6 @@ bool img_move(img_t *img, float dx, float dy)
 {
 	float ox, oy;
 
-	if (img == NULL || img->im == NULL)
-		return false;
-
 	ox = img->x;
 	oy = img->y;
 
@@ -622,9 +590,6 @@ bool img_pan(img_t *img, direction_t dir, int d)
 	 */
 	float x, y;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return false;
-
 	if (d > 0) {
 		x = y = MAX(1, (float) d * img->zoom);
 	} else {
@@ -649,9 +614,6 @@ bool img_pan_edge(img_t *img, direction_t dir)
 {
 	int ox, oy;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return false;
-
 	ox = img->x;
 	oy = img->y;
 
@@ -678,9 +640,6 @@ void img_rotate(img_t *img, degree_t d)
 {
 	int i, ox, oy, tmp;
 
-	if (img == NULL || img->im == NULL || img->win == NULL)
-		return;
-
 	imlib_context_set_image(img->im);
 	imlib_image_orientate(d);
 
@@ -716,7 +675,7 @@ void img_flip(img_t *img, flipdir_t d)
 
 	d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
 
-	if (img == NULL || img->im == NULL || d < 0 || d >= ARRLEN(imlib_flip_op))
+	if (d < 0 || d >= ARRLEN(imlib_flip_op))
 		return;
 
 	imlib_context_set_image(img->im);
@@ -733,9 +692,6 @@ void img_flip(img_t *img, flipdir_t d)
 
 void img_toggle_antialias(img_t *img)
 {
-	if (img == NULL || img->im == NULL)
-		return;
-
 	img->aa = !img->aa;
 	imlib_context_set_image(img->im);
 	imlib_context_set_anti_alias(img->aa);
@@ -751,9 +707,6 @@ bool img_change_gamma(img_t *img, int d)
 	int gamma;
 	double range;
 
-	if (img == NULL)
-		return false;
-
 	if (d == 0)
 		gamma = 0;
 	else
@@ -775,8 +728,6 @@ bool img_change_gamma(img_t *img, int d)
 
 bool img_frame_goto(img_t *img, int n)
 {
-	if (img == NULL || img->im == NULL)
-		return false;
 	if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
 		return false;
 
@@ -794,7 +745,7 @@ bool img_frame_goto(img_t *img, int n)
 
 bool img_frame_navigate(img_t *img, int d)
 {
-	if (img == NULL|| img->im == NULL || img->multi.cnt == 0 || d == 0)
+	if (img->multi.cnt == 0 || d == 0)
 		return false;
 
 	d += img->multi.sel;
@@ -808,7 +759,7 @@ bool img_frame_navigate(img_t *img, int d)
 
 bool img_frame_animate(img_t *img)
 {
-	if (img == NULL || img->im == NULL || img->multi.cnt == 0)
+	if (img->multi.cnt == 0)
 		return false;
 
 	if (img->multi.sel + 1 >= img->multi.cnt)
diff --git a/main.c b/main.c
index 179dcc0..f5b5158 100644
--- a/main.c
+++ b/main.c
@@ -117,7 +117,7 @@ void check_add_file(char *filename, bool given)
 {
 	const char *bn;
 
-	if (filename == NULL || *filename == '\0')
+	if (*filename == '\0')
 		return;
 
 	if (access(filename, R_OK) < 0) {
@@ -323,7 +323,8 @@ void load_image(int new)
 	if (new < 0 || new >= filecnt)
 		return;
 
-	win_set_cursor(&win, CURSOR_WATCH);
+	if (win.xwin != None)
+		win_set_cursor(&win, CURSOR_WATCH);
 	reset_timeout(slideshow);
 
 	if (new != current)
@@ -590,9 +591,6 @@ void on_keypress(XKeyEvent *kev)
 	char key;
 	bool dirty = false;
 
-	if (kev == NULL)
-		return;
-
 	if (kev->state & ShiftMask) {
 		kev->state &= ~ShiftMask;
 		XLookupString(kev, &key, 1, &shksym, NULL);
@@ -633,9 +631,6 @@ void on_buttonpress(XButtonEvent *bev)
 	bool dirty = false;
 	static Time firstclick;
 
-	if (bev == NULL)
-		return;
-
 	if (mode == MODE_IMAGE) {
 		win_set_cursor(&win, CURSOR_ARROW);
 		set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
diff --git a/thumbs.c b/thumbs.c
index 3c18c27..d08e972 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -43,7 +43,7 @@ char* tns_cache_filepath(const char *filepath)
 	size_t len;
 	char *cfile = NULL;
 
-	if (cache_dir == NULL || filepath == NULL || *filepath != '/')
+	if (*filepath != '/')
 		return NULL;
 	
 	if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
@@ -61,8 +61,6 @@ Imlib_Image tns_cache_load(const char *filepath, bool *outdated)
 	struct stat cstats, fstats;
 	Imlib_Image im = NULL;
 
-	if (filepath == NULL)
-		return NULL;
 	if (stat(filepath, &fstats) < 0)
 		return NULL;
 
@@ -85,8 +83,6 @@ void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
 	struct utimbuf times;
 	Imlib_Load_Error err = 0;
 
-	if (im == NULL || filepath == NULL)
-		return;
 	if (stat(filepath, &fstats) < 0)
 		return;
 
@@ -126,9 +122,6 @@ void tns_clean_cache(tns_t *tns)
 	char *cfile, *filename, *tpos;
 	r_dir_t dir;
 
-	if (cache_dir == NULL)
-		return;
-	
 	if (r_opendir(&dir, cache_dir) < 0) {
 		warn("could not open thumbnail cache directory: %s", cache_dir);
 		return;
@@ -162,9 +155,6 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
 	int len;
 	const char *homedir, *dsuffix = "";
 
-	if (tns == NULL)
-		return;
-
 	if (cnt != NULL && *cnt > 0) {
 		tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t));
 		memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
@@ -200,9 +190,6 @@ void tns_free(tns_t *tns)
 {
 	int i;
 
-	if (tns == NULL)
-		return;
-
 	if (tns->thumbs != NULL) {
 		for (i = 0; i < *tns->cnt; i++) {
 			if (tns->thumbs[i].im != NULL) {
@@ -253,8 +240,6 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
 	fileinfo_t *file;
 	Imlib_Image im = NULL;
 
-	if (tns == NULL || tns->thumbs == NULL)
-		return false;
 	if (n < 0 || n >= *tns->cnt)
 		return false;
 	file = &tns->files[n];
@@ -386,8 +371,6 @@ void tns_unload(tns_t *tns, int n)
 {
 	thumb_t *t;
 
-	if (tns == NULL || tns->thumbs == NULL)
-		return;
 	if (n < 0 || n >= *tns->cnt)
 		return;
 
@@ -434,8 +417,6 @@ void tns_render(tns_t *tns)
 	win_t *win;
 	int i, cnt, r, x, y;
 
-	if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
-		return;
 	if (!tns->dirty)
 		return;
 
@@ -495,9 +476,6 @@ void tns_render(tns_t *tns)
 
 void tns_mark(tns_t *tns, int n, bool mark)
 {
-	if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
-		return;
-
 	if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
 		win_t *win = tns->win;
 		thumb_t *t = &tns->thumbs[n];
@@ -519,9 +497,6 @@ void tns_mark(tns_t *tns, int n, bool mark)
 
 void tns_highlight(tns_t *tns, int n, bool hl)
 {
-	if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
-		return;
-
 	if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
 		win_t *win = tns->win;
 		thumb_t *t = &tns->thumbs[n];
@@ -545,9 +520,6 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
 {
 	int old, max;
 
-	if (tns == NULL || tns->thumbs == NULL)
-		return false;
-
 	old = *tns->sel;
 	cnt = cnt > 1 ? cnt : 1;
 
@@ -581,9 +553,6 @@ bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
 {
 	int d, max, old;
 
-	if (tns == NULL)
-		return false;
-
 	old = tns->first;
 	d = tns->cols * (screen ? tns->rows : 1);
 
@@ -607,9 +576,6 @@ bool tns_zoom(tns_t *tns, int d)
 {
 	int i, oldzl;
 
-	if (tns == NULL || tns->thumbs == NULL)
-		return false;
-
 	oldzl = tns->zl;
 	tns->zl += -(d < 0) + (d > 0);
 	tns->zl = MAX(tns->zl, 0);
@@ -631,8 +597,6 @@ int tns_translate(tns_t *tns, int x, int y)
 {
 	int n;
 
-	if (tns == NULL || tns->thumbs == NULL)
-		return -1;
 	if (x < tns->x || y < tns->y)
 		return -1;
 
diff --git a/util.c b/util.c
index 3a5ae9a..4c09e97 100644
--- a/util.c
+++ b/util.c
@@ -103,9 +103,6 @@ ssize_t get_line(char **buf, size_t *n, FILE *stream)
 	size_t len;
 	char *s;
 
-	if (stream == NULL || feof(stream) || ferror(stream))
-		return -1;
-
 	if (*buf == NULL || *n == 0) {
 		*n = BUF_SIZE;
 		*buf = (char*) s_malloc(*n);
@@ -148,7 +145,7 @@ char* absolute_path(const char *filename)
 	char *dir, *dirname = NULL, *path = NULL, *s;
 	char *cwd = NULL, *twd = NULL;
 
-	if (filename == NULL || *filename == '\0' || *filename == '/')
+	if (*filename == '\0' || *filename == '/')
 		return NULL;
 
 	len = FNAME_LEN;
@@ -211,7 +208,7 @@ end:
 
 int r_opendir(r_dir_t *rdir, const char *dirname)
 {
-	if (rdir == NULL || dirname == NULL || *dirname == '\0')
+	if (*dirname == '\0')
 		return -1;
 
 	if ((rdir->dir = opendir(dirname)) == NULL) {
@@ -234,9 +231,6 @@ int r_closedir(r_dir_t *rdir)
 {
 	int ret = 0;
 
-	if (rdir == NULL)
-		return -1;
-	
 	if (rdir->stack != NULL) {
 		while (rdir->stlen > 0)
 			free(rdir->stack[--rdir->stlen]);
@@ -264,9 +258,6 @@ char* r_readdir(r_dir_t *rdir)
 	struct dirent *dentry;
 	struct stat fstats;
 
-	if (rdir == NULL || rdir->dir == NULL || rdir->name == NULL)
-		return NULL;
-
 	while (true) {
 		if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
 			if (dentry->d_name[0] == '.')
@@ -316,7 +307,7 @@ int r_mkdir(const char *path)
 	struct stat stats;
 	int err = 0;
 
-	if (path == NULL || *path == '\0')
+	if (*path == '\0')
 		return -1;
 
 	if (stat(path, &stats) == 0)
diff --git a/window.c b/window.c
index ff7f116..ec6cc04 100644
--- a/window.c
+++ b/window.c
@@ -94,8 +94,6 @@ unsigned long win_alloc_color(win_t *win, const char *name)
 {
 	XColor col;
 
-	if (win == NULL)
-		return 0UL;
 	if (XAllocNamedColor(win->env.dpy,
 	                     DefaultColormap(win->env.dpy, win->env.scr),
 	                     name, &col, &col) == 0)
@@ -142,9 +140,6 @@ void win_init(win_t *win)
 {
 	win_env_t *e;
 
-	if (win == NULL)
-		return;
-
 	memset(win, 0, sizeof(win_t));
 
 	e = &win->env;
@@ -199,9 +194,6 @@ void win_open(win_t *win)
 	XSizeHints sizehints;
 	Bool fullscreen = options->fullscreen && fs_support;
 
-	if (win == NULL)
-		return;
-
 	e = &win->env;
 
 	sizehints.flags = PWinGravity;
@@ -317,9 +309,6 @@ void win_open(win_t *win)
 
 void win_close(win_t *win)
 {
-	if (win == NULL || win->xwin == None)
-		return;
-
 	XFreeCursor(win->env.dpy, carrow);
 	XFreeCursor(win->env.dpy, cnone);
 	XFreeCursor(win->env.dpy, chand);
@@ -335,9 +324,6 @@ bool win_configure(win_t *win, XConfigureEvent *c)
 {
 	bool changed;
 
-	if (win == NULL || c == NULL)
-		return false;
-
 	changed = win->w != c->width || win->h + win->bar.h != c->height;
 
 	win->x = c->x;
@@ -354,9 +340,6 @@ void win_toggle_fullscreen(win_t *win)
 	XEvent ev;
 	XClientMessageEvent *cm;
 
-	if (win == NULL || win->xwin == None)
-		return;
-
 	if (!fs_support) {
 		if (!fs_warned) {
 			warn("window manager does not support fullscreen");
@@ -383,9 +366,6 @@ void win_toggle_fullscreen(win_t *win)
 
 void win_toggle_bar(win_t *win)
 {
-	if (win == NULL || win->xwin == None)
-		return;
-
 	if (win->bar.h != 0) {
 		win->h += win->bar.h;
 		win->bar.h = 0;
@@ -399,9 +379,6 @@ void win_clear(win_t *win)
 {
 	win_env_t *e;
 
-	if (win == NULL || win->xwin == None)
-		return;
-
 	e = &win->env;
 
 	if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
@@ -423,8 +400,6 @@ void win_draw_bar(win_t *win)
 	win_env_t *e;
 	win_bar_t *l, *r;
 
-	if (win == NULL || win->xwin == None)
-		return;
 	if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
 		return;
 
@@ -473,9 +448,6 @@ void win_draw_bar(win_t *win)
 
 void win_draw(win_t *win)
 {
-	if (win == NULL || win->xwin == None)
-		return;
-
 	if (win->bar.h > 0)
 		win_draw_bar(win);
 
@@ -489,9 +461,6 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
 {
 	XGCValues gcval;
 
-	if (win == NULL || win->buf.pm == None)
-		return;
-
 	gcval.line_width = lw;
 	gcval.foreground = col;
 	XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
@@ -517,9 +486,6 @@ int win_textwidth(const char *text, unsigned int len, bool with_padding)
 
 void win_set_title(win_t *win, const char *title)
 {
-	if (win == NULL || win->xwin == None)
-		return;
-
 	if (title == NULL)
 		title = "sxiv";
 
@@ -536,9 +502,6 @@ void win_set_title(win_t *win, const char *title)
 
 void win_set_cursor(win_t *win, cursor_t cursor)
 {
-	if (win == NULL || win->xwin == None)
-		return;
-
 	switch (cursor) {
 		case CURSOR_NONE:
 			XDefineCursor(win->env.dpy, win->xwin, cnone);