Krolyxon
Commit 01f3cf2

Branches

Refs →

nsxiv01f3cf2

use assertions instead of ignoring bogus arguments (#406)

instead of silently ignoring bogus arguments (i.e programming errors),
which can make debugging harder, it's better to assert them so that they
get caught faster in debug builds.
Author
NRK
Date
2023-01-28 10:27 +0000
Parent
6ffc64a
Commit
01f3cf2e4778940a82001e48cfeec767841f52c4

+9 −13

3 files changed

  1. M thumbs.c +4 −8
  2. M util.c +1 −2
  3. M window.c +4 −3
M thumbs.c View
@@ -21,6 +21,7 @@
2121 #define INCLUDE_THUMBS_CONFIG
2222 #include "config.h"
2323
24+#include <assert.h>
2425 #include <errno.h>
2526 #include <stdio.h>
2627 #include <stdlib.h>
@@ -40,8 +41,7 @@ static char* tns_cache_filepath(const char *filepath)
4041 size_t len;
4142 char *cfile = NULL;
4243
43− if (*filepath != '/')
44− return NULL;
44+ assert(*filepath == '/' && "filepath should be result of realpath(3)");
4545
4646 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
4747 /* don't cache images inside the cache directory! */
@@ -355,9 +355,7 @@ void tns_unload(tns_t *tns, int n)
355355 {
356356 thumb_t *t;
357357
358− if (n < 0 || n >= *tns->cnt)
359− return;
360−
358+ assert(n >= 0 && n < *tns->cnt);
361359 t = &tns->thumbs[n];
362360
363361 if (t->im != NULL) {
@@ -371,9 +369,7 @@ static void tns_check_view(tns_t *tns, bool scrolled)
371369 {
372370 int r;
373371
374− if (tns == NULL)
375− return;
376−
372+ assert(tns != NULL);
377373 tns->first -= tns->first % tns->cols;
378374 r = *tns->sel % tns->cols;
379375
M util.c View
@@ -226,8 +226,7 @@ void construct_argv(char **argv, unsigned int len, ...)
226226 for (i = 0; i < len; ++i)
227227 argv[i] = va_arg(args, char *);
228228 va_end(args);
229− if (argv[len-1] != NULL)
230− error(EXIT_FAILURE, 0, "argv not NULL terminated");
229+ assert(argv[len-1] == NULL && "argv should be NULL terminated");
231230 }
232231
233232 static int mkspawn_pipe(posix_spawn_file_actions_t *fa, const char *cmd, int *pfd, int dupidx)
M window.c View
@@ -22,6 +22,7 @@
2222 #include "config.h"
2323 #include "icon/data.h"
2424
25+#include <assert.h>
2526 #include <locale.h>
2627 #include <stdlib.h>
2728 #include <string.h>
@@ -450,10 +451,10 @@ static void win_draw_bar(win_t *win)
450451 win_bar_t *l, *r;
451452 XftDraw *d;
452453
453− if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
454− return;
455−
456454 e = &win->env;
455+ l = &win->bar.l;
456+ r = &win->bar.r;
457+ assert(l->buf != NULL && r->buf != NULL);
457458 y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
458459 w = win->w - 2*H_TEXT_PAD;
459460 d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);