nsxivc131b1e
fix: -Wsign-compare warnings (#336)
mixing signed and unsigned types in comparison can end up having
unintended results. for example:
if (-1 < 1U)
printf("true\n");
else
printf("false\n");
previously we silenced these warnings, instead just fix them properly
via necessary casting, and in cases where the value cannot be negative
(e.g width/height members) make them unsigned.
+30 −28
7 files changed
- M .woodpecker/CFLAGS +1 −1
- M commands.c +2 −2
- M image.c +12 −10
- M main.c +4 −4
- M nsxiv.h +5 −5
- M thumbs.c +1 −1
- M window.c +5 −5
| @@ -10,4 +10,4 @@ | |||
| 10 | 10 | -Wbad-function-cast -Wdeclaration-after-statement | |
| 11 | 11 | -Wmissing-prototypes -Wstrict-prototypes | |
| 12 | 12 | # silence | |
| 13 | − | -Wno-sign-compare -Wno-unused-parameter -Wno-missing-field-initializers | |
| 13 | + | -Wno-unused-parameter -Wno-missing-field-initializers | |
| @@ -36,7 +36,7 @@ bool cg_quit(arg_t status) | |||
| 36 | 36 | unsigned int i; | |
| 37 | 37 | ||
| 38 | 38 | if (options->to_stdout && markcnt > 0) { | |
| 39 | − | for (i = 0; i < filecnt; i++) { | |
| 39 | + | for (i = 0; i < (unsigned int)filecnt; i++) { | |
| 40 | 40 | if (files[i].flags & FF_MARK) | |
| 41 | 41 | printf("%s%c", files[i].name, options->using_null ? '\0' : '\n'); | |
| 42 | 42 | } | |
| @@ -319,7 +319,7 @@ bool ci_drag(arg_t drag_mode) | |||
| 319 | 319 | float px, py; | |
| 320 | 320 | XEvent e; | |
| 321 | 321 | ||
| 322 | − | if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h) | |
| 322 | + | if ((int)(img.w * img.zoom) <= (int)win.w && (int)(img.h * img.zoom) <= (int)win.h) | |
| 323 | 323 | return false; | |
| 324 | 324 | ||
| 325 | 325 | win_set_cursor(&win, drag_mode == DRAG_ABSOLUTE ? CURSOR_DRAG_ABSOLUTE : CURSOR_DRAG_RELATIVE); | |
| @@ -473,7 +473,7 @@ bool img_load(img_t *img, const fileinfo_t *file) | |||
| 473 | 473 | ||
| 474 | 474 | CLEANUP void img_close(img_t *img, bool decache) | |
| 475 | 475 | { | |
| 476 | − | int i; | |
| 476 | + | unsigned int i; | |
| 477 | 477 | ||
| 478 | 478 | if (img->multi.cnt > 0) { | |
| 479 | 479 | for (i = 0; i < img->multi.cnt; i++) { | |
| @@ -671,7 +671,9 @@ bool img_zoom_to(img_t *img, float z) | |||
| 671 | 671 | int x, y; | |
| 672 | 672 | if (ZOOM_MIN <= z && z <= ZOOM_MAX) { | |
| 673 | 673 | win_cursor_pos(img->win, &x, &y); | |
| 674 | − | if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) { | |
| 674 | + | if (x < 0 || (unsigned int)x >= img->win->w || | |
| 675 | + | y < 0 || (unsigned int)y >= img->win->h) | |
| 676 | + | { | |
| 675 | 677 | x = img->win->w / 2; | |
| 676 | 678 | y = img->win->h / 2; | |
| 677 | 679 | } | |
| @@ -688,13 +690,13 @@ bool img_zoom_to(img_t *img, float z) | |||
| 688 | 690 | ||
| 689 | 691 | bool img_zoom(img_t *img, int d) | |
| 690 | 692 | { | |
| 691 | − | int i = d > 0 ? 0 : ARRLEN(zoom_levels)-1; | |
| 692 | − | while (i >= 0 && i < ARRLEN(zoom_levels) && (d > 0 ? | |
| 693 | − | zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom)) | |
| 693 | + | int i = d > 0 ? 0 : (int)ARRLEN(zoom_levels)-1; | |
| 694 | + | while (i >= 0 && i < (int)ARRLEN(zoom_levels) && | |
| 695 | + | (d > 0 ? zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom)) | |
| 694 | 696 | { | |
| 695 | 697 | i += d; | |
| 696 | 698 | } | |
| 697 | − | i = MIN(MAX(i, 0), ARRLEN(zoom_levels)-1); | |
| 699 | + | i = MIN(MAX(i, 0), (int)ARRLEN(zoom_levels)-1); | |
| 698 | 700 | return img_zoom_to(img, zoom_levels[i]/100); | |
| 699 | 701 | } | |
| 700 | 702 | ||
| @@ -787,7 +789,7 @@ bool img_pan_edge(img_t *img, direction_t dir) | |||
| 787 | 789 | ||
| 788 | 790 | void img_rotate(img_t *img, degree_t d) | |
| 789 | 791 | { | |
| 790 | − | int i, tmp; | |
| 792 | + | unsigned int i, tmp; | |
| 791 | 793 | float ox, oy; | |
| 792 | 794 | ||
| 793 | 795 | imlib_context_set_image(img->im); | |
| @@ -816,7 +818,7 @@ void img_rotate(img_t *img, degree_t d) | |||
| 816 | 818 | ||
| 817 | 819 | void img_flip(img_t *img, flipdir_t d) | |
| 818 | 820 | { | |
| 819 | − | int i; | |
| 821 | + | unsigned int i; | |
| 820 | 822 | void (*imlib_flip_op[3])(void) = { | |
| 821 | 823 | imlib_image_flip_horizontal, | |
| 822 | 824 | imlib_image_flip_vertical, | |
| @@ -878,7 +880,7 @@ bool img_change_gamma(img_t *img, int d) | |||
| 878 | 880 | ||
| 879 | 881 | static bool img_frame_goto(img_t *img, int n) | |
| 880 | 882 | { | |
| 881 | − | if (n < 0 || n >= img->multi.cnt || n == img->multi.sel) | |
| 883 | + | if (n < 0 || (unsigned int)n >= img->multi.cnt || (unsigned int)n == img->multi.sel) | |
| 882 | 884 | return false; | |
| 883 | 885 | ||
| 884 | 886 | img->multi.sel = n; | |
| @@ -899,7 +901,7 @@ bool img_frame_navigate(img_t *img, int d) | |||
| 899 | 901 | return false; | |
| 900 | 902 | ||
| 901 | 903 | d += img->multi.sel; | |
| 902 | − | d = MAX(0, MIN(d, img->multi.cnt - 1)); | |
| 904 | + | d = MAX(0, MIN(d, (int)img->multi.cnt - 1)); | |
| 903 | 905 | ||
| 904 | 906 | return img_frame_goto(img, d); | |
| 905 | 907 | } | |
| @@ -224,7 +224,7 @@ static bool check_timeouts(int *t) | |||
| 224 | 224 | int i = 0, tdiff, tmin = -1; | |
| 225 | 225 | struct timeval now; | |
| 226 | 226 | ||
| 227 | − | while (i < ARRLEN(timeouts)) { | |
| 227 | + | while (i < (int)ARRLEN(timeouts)) { | |
| 228 | 228 | if (timeouts[i].active) { | |
| 229 | 229 | gettimeofday(&now, 0); | |
| 230 | 230 | tdiff = TV_DIFF(&timeouts[i].when, &now); | |
| @@ -434,11 +434,11 @@ int nav_button(void) | |||
| 434 | 434 | ||
| 435 | 435 | win_cursor_pos(&win, &x, &y); | |
| 436 | 436 | nw = NAV_IS_REL ? win.w * NAV_WIDTH / 100 : NAV_WIDTH; | |
| 437 | − | nw = MIN(nw, (win.w + 1) / 2); | |
| 437 | + | nw = MIN(nw, ((int)win.w + 1) / 2); | |
| 438 | 438 | ||
| 439 | 439 | if (x < nw) | |
| 440 | 440 | return 0; | |
| 441 | − | else if (x < win.w-nw) | |
| 441 | + | else if (x < (int)win.w - nw) | |
| 442 | 442 | return 1; | |
| 443 | 443 | else | |
| 444 | 444 | return 2; | |
| @@ -917,7 +917,7 @@ int main(int argc, char *argv[]) | |||
| 917 | 917 | const char *name[] = { "image-info", "thumb-info", "key-handler", "win-title" }; | |
| 918 | 918 | const char *s = "/nsxiv/exec/"; | |
| 919 | 919 | ||
| 920 | − | for (i = 0; i < ARRLEN(cmd); i++) { | |
| 920 | + | for (i = 0; i < (int)ARRLEN(cmd); i++) { | |
| 921 | 921 | n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1; | |
| 922 | 922 | cmd[i]->cmd = emalloc(n); | |
| 923 | 923 | snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]); | |
| @@ -162,9 +162,9 @@ typedef struct { | |||
| 162 | 162 | ||
| 163 | 163 | typedef struct { | |
| 164 | 164 | img_frame_t *frames; | |
| 165 | − | int cap; | |
| 166 | − | int cnt; | |
| 167 | − | int sel; | |
| 165 | + | unsigned int cap; | |
| 166 | + | unsigned int cnt; | |
| 167 | + | unsigned int sel; | |
| 168 | 168 | bool animate; | |
| 169 | 169 | unsigned int framedelay; | |
| 170 | 170 | int length; | |
| @@ -403,8 +403,8 @@ struct win { | |||
| 403 | 403 | unsigned int bw; | |
| 404 | 404 | ||
| 405 | 405 | struct { | |
| 406 | − | int w; | |
| 407 | − | int h; | |
| 406 | + | unsigned int w; | |
| 407 | + | unsigned int h; | |
| 408 | 408 | Pixmap pm; | |
| 409 | 409 | } buf; | |
| 410 | 410 | ||
| @@ -561,7 +561,7 @@ bool tns_zoom(tns_t *tns, int d) | |||
| 561 | 561 | oldzl = tns->zl; | |
| 562 | 562 | tns->zl += -(d < 0) + (d > 0); | |
| 563 | 563 | tns->zl = MAX(tns->zl, 0); | |
| 564 | − | tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1); | |
| 564 | + | tns->zl = MIN(tns->zl, (int)ARRLEN(thumb_sizes)-1); | |
| 565 | 565 | ||
| 566 | 566 | tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1; | |
| 567 | 567 | tns->bw = MIN(tns->bw, 4); | |
| @@ -259,7 +259,7 @@ void win_open(win_t *win) | |||
| 259 | 259 | ButtonReleaseMask | ButtonPressMask | KeyPressMask | | |
| 260 | 260 | PointerMotionMask | StructureNotifyMask); | |
| 261 | 261 | ||
| 262 | − | for (i = 0; i < ARRLEN(cursors); i++) { | |
| 262 | + | for (i = 0; i < (int)ARRLEN(cursors); i++) { | |
| 263 | 263 | if (i != CURSOR_NONE) | |
| 264 | 264 | cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name); | |
| 265 | 265 | } | |
| @@ -274,12 +274,12 @@ void win_open(win_t *win) | |||
| 274 | 274 | n = icons[ARRLEN(icons)-1].size; | |
| 275 | 275 | icon_data = emalloc((n * n + 2) * sizeof(*icon_data)); | |
| 276 | 276 | ||
| 277 | − | for (i = 0; i < ARRLEN(icons); i++) { | |
| 277 | + | for (i = 0; i < (int)ARRLEN(icons); i++) { | |
| 278 | 278 | n = 0; | |
| 279 | 279 | icon_data[n++] = icons[i].size; | |
| 280 | 280 | icon_data[n++] = icons[i].size; | |
| 281 | 281 | ||
| 282 | − | for (j = 0; j < icons[i].cnt; j++) { | |
| 282 | + | for (j = 0; j < (int)icons[i].cnt; j++) { | |
| 283 | 283 | for (c = icons[i].data[j] >> 4; c >= 0; c--) | |
| 284 | 284 | icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F]; | |
| 285 | 285 | } | |
| @@ -345,7 +345,7 @@ bool win_configure(win_t *win, XConfigureEvent *c) | |||
| 345 | 345 | { | |
| 346 | 346 | bool changed; | |
| 347 | 347 | ||
| 348 | − | changed = win->w != c->width || win->h + win->bar.h != c->height; | |
| 348 | + | changed = win->w != (unsigned int)c->width || win->h + win->bar.h != (unsigned int)c->height; | |
| 349 | 349 | ||
| 350 | 350 | win->x = c->x; | |
| 351 | 351 | win->y = c->y; | |
| @@ -516,7 +516,7 @@ void win_set_title(win_t *win, const char *title, size_t len) | |||
| 516 | 516 | { | |
| 517 | 517 | int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME }; | |
| 518 | 518 | ||
| 519 | − | for (i = 0; i < ARRLEN(targets); ++i) { | |
| 519 | + | for (i = 0; i < (int)ARRLEN(targets); ++i) { | |
| 520 | 520 | XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]], | |
| 521 | 521 | atoms[ATOM_UTF8_STRING], 8, PropModeReplace, | |
| 522 | 522 | (unsigned char *)title, len); | |