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.
+9 −13
3 files changed
| @@ -21,6 +21,7 @@ | |||
| 21 | 21 | #define INCLUDE_THUMBS_CONFIG | |
| 22 | 22 | #include "config.h" | |
| 23 | 23 | ||
| 24 | + | #include <assert.h> | |
| 24 | 25 | #include <errno.h> | |
| 25 | 26 | #include <stdio.h> | |
| 26 | 27 | #include <stdlib.h> | |
| @@ -40,8 +41,7 @@ static char* tns_cache_filepath(const char *filepath) | |||
| 40 | 41 | size_t len; | |
| 41 | 42 | char *cfile = NULL; | |
| 42 | 43 | ||
| 43 | − | if (*filepath != '/') | |
| 44 | − | return NULL; | |
| 44 | + | assert(*filepath == '/' && "filepath should be result of realpath(3)"); | |
| 45 | 45 | ||
| 46 | 46 | if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { | |
| 47 | 47 | /* don't cache images inside the cache directory! */ | |
| @@ -355,9 +355,7 @@ void tns_unload(tns_t *tns, int n) | |||
| 355 | 355 | { | |
| 356 | 356 | thumb_t *t; | |
| 357 | 357 | ||
| 358 | − | if (n < 0 || n >= *tns->cnt) | |
| 359 | − | return; | |
| 360 | − | ||
| 358 | + | assert(n >= 0 && n < *tns->cnt); | |
| 361 | 359 | t = &tns->thumbs[n]; | |
| 362 | 360 | ||
| 363 | 361 | if (t->im != NULL) { | |
| @@ -371,9 +369,7 @@ static void tns_check_view(tns_t *tns, bool scrolled) | |||
| 371 | 369 | { | |
| 372 | 370 | int r; | |
| 373 | 371 | ||
| 374 | − | if (tns == NULL) | |
| 375 | − | return; | |
| 376 | − | ||
| 372 | + | assert(tns != NULL); | |
| 377 | 373 | tns->first -= tns->first % tns->cols; | |
| 378 | 374 | r = *tns->sel % tns->cols; | |
| 379 | 375 | ||
| @@ -226,8 +226,7 @@ void construct_argv(char **argv, unsigned int len, ...) | |||
| 226 | 226 | for (i = 0; i < len; ++i) | |
| 227 | 227 | argv[i] = va_arg(args, char *); | |
| 228 | 228 | 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"); | |
| 231 | 230 | } | |
| 232 | 231 | ||
| 233 | 232 | static int mkspawn_pipe(posix_spawn_file_actions_t *fa, const char *cmd, int *pfd, int dupidx) | |
| @@ -22,6 +22,7 @@ | |||
| 22 | 22 | #include "config.h" | |
| 23 | 23 | #include "icon/data.h" | |
| 24 | 24 | ||
| 25 | + | #include <assert.h> | |
| 25 | 26 | #include <locale.h> | |
| 26 | 27 | #include <stdlib.h> | |
| 27 | 28 | #include <string.h> | |
| @@ -450,10 +451,10 @@ static void win_draw_bar(win_t *win) | |||
| 450 | 451 | win_bar_t *l, *r; | |
| 451 | 452 | XftDraw *d; | |
| 452 | 453 | ||
| 453 | − | if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL) | |
| 454 | − | return; | |
| 455 | − | ||
| 456 | 454 | e = &win->env; | |
| 455 | + | l = &win->bar.l; | |
| 456 | + | r = &win->bar.r; | |
| 457 | + | assert(l->buf != NULL && r->buf != NULL); | |
| 457 | 458 | y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD; | |
| 458 | 459 | w = win->w - 2*H_TEXT_PAD; | |
| 459 | 460 | d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap); | |