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.

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/336
Reviewed-by: explosion-mental <explosion-mental@noreply.codeberg.org>
This commit is contained in:
NRK 2022-07-15 22:46:23 +02:00
parent 5cab2fb525
commit c131b1ed83
7 changed files with 30 additions and 28 deletions

View File

@ -10,4 +10,4 @@
-Wbad-function-cast -Wdeclaration-after-statement
-Wmissing-prototypes -Wstrict-prototypes
# silence
-Wno-sign-compare -Wno-unused-parameter -Wno-missing-field-initializers
-Wno-unused-parameter -Wno-missing-field-initializers

View File

@ -36,7 +36,7 @@ bool cg_quit(arg_t status)
unsigned int i;
if (options->to_stdout && markcnt > 0) {
for (i = 0; i < filecnt; i++) {
for (i = 0; i < (unsigned int)filecnt; i++) {
if (files[i].flags & FF_MARK)
printf("%s%c", files[i].name, options->using_null ? '\0' : '\n');
}
@ -319,7 +319,7 @@ bool ci_drag(arg_t drag_mode)
float px, py;
XEvent e;
if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h)
if ((int)(img.w * img.zoom) <= (int)win.w && (int)(img.h * img.zoom) <= (int)win.h)
return false;
win_set_cursor(&win, drag_mode == DRAG_ABSOLUTE ? CURSOR_DRAG_ABSOLUTE : CURSOR_DRAG_RELATIVE);

22
image.c
View File

@ -473,7 +473,7 @@ bool img_load(img_t *img, const fileinfo_t *file)
CLEANUP void img_close(img_t *img, bool decache)
{
int i;
unsigned int i;
if (img->multi.cnt > 0) {
for (i = 0; i < img->multi.cnt; i++) {
@ -671,7 +671,9 @@ bool img_zoom_to(img_t *img, float z)
int x, y;
if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
win_cursor_pos(img->win, &x, &y);
if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
if (x < 0 || (unsigned int)x >= img->win->w ||
y < 0 || (unsigned int)y >= img->win->h)
{
x = img->win->w / 2;
y = img->win->h / 2;
}
@ -688,13 +690,13 @@ bool img_zoom_to(img_t *img, float z)
bool img_zoom(img_t *img, int d)
{
int i = d > 0 ? 0 : ARRLEN(zoom_levels)-1;
while (i >= 0 && i < ARRLEN(zoom_levels) && (d > 0 ?
zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom))
int i = d > 0 ? 0 : (int)ARRLEN(zoom_levels)-1;
while (i >= 0 && i < (int)ARRLEN(zoom_levels) &&
(d > 0 ? zoom_levels[i]/100 <= img->zoom : zoom_levels[i]/100 >= img->zoom))
{
i += d;
}
i = MIN(MAX(i, 0), ARRLEN(zoom_levels)-1);
i = MIN(MAX(i, 0), (int)ARRLEN(zoom_levels)-1);
return img_zoom_to(img, zoom_levels[i]/100);
}
@ -787,7 +789,7 @@ bool img_pan_edge(img_t *img, direction_t dir)
void img_rotate(img_t *img, degree_t d)
{
int i, tmp;
unsigned int i, tmp;
float ox, oy;
imlib_context_set_image(img->im);
@ -816,7 +818,7 @@ void img_rotate(img_t *img, degree_t d)
void img_flip(img_t *img, flipdir_t d)
{
int i;
unsigned int i;
void (*imlib_flip_op[3])(void) = {
imlib_image_flip_horizontal,
imlib_image_flip_vertical,
@ -878,7 +880,7 @@ bool img_change_gamma(img_t *img, int d)
static bool img_frame_goto(img_t *img, int n)
{
if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
if (n < 0 || (unsigned int)n >= img->multi.cnt || (unsigned int)n == img->multi.sel)
return false;
img->multi.sel = n;
@ -899,7 +901,7 @@ bool img_frame_navigate(img_t *img, int d)
return false;
d += img->multi.sel;
d = MAX(0, MIN(d, img->multi.cnt - 1));
d = MAX(0, MIN(d, (int)img->multi.cnt - 1));
return img_frame_goto(img, d);
}

8
main.c
View File

@ -224,7 +224,7 @@ static bool check_timeouts(int *t)
int i = 0, tdiff, tmin = -1;
struct timeval now;
while (i < ARRLEN(timeouts)) {
while (i < (int)ARRLEN(timeouts)) {
if (timeouts[i].active) {
gettimeofday(&now, 0);
tdiff = TV_DIFF(&timeouts[i].when, &now);
@ -434,11 +434,11 @@ int nav_button(void)
win_cursor_pos(&win, &x, &y);
nw = NAV_IS_REL ? win.w * NAV_WIDTH / 100 : NAV_WIDTH;
nw = MIN(nw, (win.w + 1) / 2);
nw = MIN(nw, ((int)win.w + 1) / 2);
if (x < nw)
return 0;
else if (x < win.w-nw)
else if (x < (int)win.w - nw)
return 1;
else
return 2;
@ -917,7 +917,7 @@ int main(int argc, char *argv[])
const char *name[] = { "image-info", "thumb-info", "key-handler", "win-title" };
const char *s = "/nsxiv/exec/";
for (i = 0; i < ARRLEN(cmd); i++) {
for (i = 0; i < (int)ARRLEN(cmd); i++) {
n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1;
cmd[i]->cmd = emalloc(n);
snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]);

10
nsxiv.h
View File

@ -162,9 +162,9 @@ typedef struct {
typedef struct {
img_frame_t *frames;
int cap;
int cnt;
int sel;
unsigned int cap;
unsigned int cnt;
unsigned int sel;
bool animate;
unsigned int framedelay;
int length;
@ -403,8 +403,8 @@ struct win {
unsigned int bw;
struct {
int w;
int h;
unsigned int w;
unsigned int h;
Pixmap pm;
} buf;

View File

@ -561,7 +561,7 @@ bool tns_zoom(tns_t *tns, int d)
oldzl = tns->zl;
tns->zl += -(d < 0) + (d > 0);
tns->zl = MAX(tns->zl, 0);
tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
tns->zl = MIN(tns->zl, (int)ARRLEN(thumb_sizes)-1);
tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
tns->bw = MIN(tns->bw, 4);

View File

@ -259,7 +259,7 @@ void win_open(win_t *win)
ButtonReleaseMask | ButtonPressMask | KeyPressMask |
PointerMotionMask | StructureNotifyMask);
for (i = 0; i < ARRLEN(cursors); i++) {
for (i = 0; i < (int)ARRLEN(cursors); i++) {
if (i != CURSOR_NONE)
cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
}
@ -274,12 +274,12 @@ void win_open(win_t *win)
n = icons[ARRLEN(icons)-1].size;
icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
for (i = 0; i < ARRLEN(icons); i++) {
for (i = 0; i < (int)ARRLEN(icons); i++) {
n = 0;
icon_data[n++] = icons[i].size;
icon_data[n++] = icons[i].size;
for (j = 0; j < icons[i].cnt; j++) {
for (j = 0; j < (int)icons[i].cnt; j++) {
for (c = icons[i].data[j] >> 4; c >= 0; c--)
icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
}
@ -345,7 +345,7 @@ bool win_configure(win_t *win, XConfigureEvent *c)
{
bool changed;
changed = win->w != c->width || win->h + win->bar.h != c->height;
changed = win->w != (unsigned int)c->width || win->h + win->bar.h != (unsigned int)c->height;
win->x = c->x;
win->y = c->y;
@ -516,7 +516,7 @@ void win_set_title(win_t *win, const char *title, size_t len)
{
int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
for (i = 0; i < ARRLEN(targets); ++i) {
for (i = 0; i < (int)ARRLEN(targets); ++i) {
XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
atoms[ATOM_UTF8_STRING], 8, PropModeReplace,
(unsigned char *)title, len);