Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checks: Fix -Wsign-compare warnings #2761

Merged
merged 4 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/drivers/dbf/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int add_table(char *table, char *name)
#else
res = snprintf(db.tables[db.ntables].file, buf_s, "%s/%s", db.name, name);
#endif
if (res >= buf_s) {
if (res < 0 || (size_t)res >= buf_s) {
db_d_append_error(_("Unable to add table %s to %s. "
"The file path is too long."),
name, db.name);
Expand Down
2 changes: 1 addition & 1 deletion display/d.legend/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void draw(const char *map_name, int maptype, int color, int thin, int lines,
if (maxCat > 0.0) {
size_t b_s = sizeof(DispFormat);
int log_maxCat = (int)(log10(fabs(maxCat))) + 1;
if (snprintf(DispFormat, b_s, "%%%dd", log_maxCat) >= b_s)
if (snprintf(DispFormat, b_s, "%%%dd", log_maxCat) >= (int)b_s)
G_fatal_error(
_("Failed to create format string with maxCat=%f."),
maxCat);
Expand Down
6 changes: 3 additions & 3 deletions display/d.linegraph/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ int main(int argc, char **argv)

/* TODO: put this to variables, and avoid -Wsign-compare */
for (i = 0; i < 3; i++) {
for (j = 0; j < strlen(title[i]->answer); j++)
for (j = 0; (size_t)j < strlen(title[i]->answer); j++)
if (title[i]->answer[j] == '_')
title[i]->answer[j] = ' ';
}
Expand Down Expand Up @@ -519,9 +519,9 @@ int main(int argc, char **argv)
c = 0;
j = 1;
if (y_color_opt->answer != NULL) {
for (i = 0; i <= (strlen(y_color_opt->answer)); i++) {
for (i = 0; (size_t)i <= (strlen(y_color_opt->answer)); i++) {
if ((y_color_opt->answer[i] == ',') ||
(i == (strlen(y_color_opt->answer)))) {
((size_t)i == (strlen(y_color_opt->answer)))) {
color_name[c] = '\0';
in[j].color = D_translate_color(color_name);
j++;
Expand Down
22 changes: 11 additions & 11 deletions display/d.mon/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,47 +186,47 @@ int start_mon(const char *name, const char *output, int select, int width,
if (G_strncasecmp(name, "wx", 2) == 0) {
sprintf(buf, "GRASS_RENDER_IMMEDIATE=default\n"); /* TODO: read settings
from wxGUI */
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
sprintf(buf, "GRASS_RENDER_FILE_READ=FALSE\n");
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
sprintf(buf, "GRASS_RENDER_TRANSPARENT=TRUE\n");
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
}
else {
sprintf(buf, "GRASS_RENDER_IMMEDIATE=%s\n", name);
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
sprintf(buf, "GRASS_RENDER_FILE_READ=TRUE\n");
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
}
sprintf(buf, "GRASS_RENDER_FILE=%s\n", out_file);
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
sprintf(buf, "GRASS_RENDER_WIDTH=%d\n", width);
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
sprintf(buf, "GRASS_RENDER_HEIGHT=%d\n", height);
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
sprintf(buf, "GRASS_LEGEND_FILE=%s\n", leg_file);
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);

if (bgcolor) {
if (strcmp(bgcolor, "none") == 0)
sprintf(buf, "GRASS_RENDER_TRANSPARENT=TRUE\n");
else
sprintf(buf, "GRASS_RENDER_BACKGROUNDCOLOR=%s\n", bgcolor);
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
}
if (truecolor) {
sprintf(buf, "GRASS_RENDER_TRUECOLOR=TRUE\n");
if (write(fd, buf, strlen(buf)) != strlen(buf))
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
G_fatal_error(_("Failed to write to file <%s>"), env_file);
}
close(fd);
Expand Down
6 changes: 3 additions & 3 deletions general/g.mkfontcap/stroke_fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct font_desc {
};

static struct font_desc *font_descriptions = NULL;
static int num_descriptions = 0;
static unsigned int num_descriptions = 0;

static int load_font_descriptions(const char *);
static void free_font_descriptions(void);
Expand Down Expand Up @@ -160,7 +160,7 @@ static int load_font_descriptions(const char *descfile)

static const char *get_desc(const char *filename)
{
int i;
unsigned int i;

for (i = 0; i < num_descriptions; i++)
if (G_strcasecmp(filename, font_descriptions[i].filename) == 0)
Expand All @@ -179,7 +179,7 @@ static const char *get_desc(const char *filename)

static void free_font_descriptions(void)
{
int i;
unsigned int i;

for (i = 0; i < num_descriptions; i++) {
G_free(font_descriptions[i].filename);
Expand Down
10 changes: 5 additions & 5 deletions imagery/i.zc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ int main(int argc, char *argv[])
CELL *cell_row;
float Width;

size_t i, j; /* Loop control variables */
int or, oc; /* Original dimensions of image */
int rows, cols; /* Smallest powers of 2 >= number of rows & columns */
size_t size; /* the length of one side */
size_t totsize; /* the Total number of data points */
size_t i, j; /* Loop control variables */
unsigned int or, oc; /* Original dimensions of image */
int rows, cols; /* Smallest powers of 2 >= number of rows & columns */
size_t size; /* the length of one side */
size_t totsize; /* the Total number of data points */
double
*data[2]; /* Data structure containing real & complex values of FFT */
struct GModule *module;
Expand Down
11 changes: 6 additions & 5 deletions lib/cairodriver/read_bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static int read_bmp_header(const unsigned char *p)
if (*p++ != 'M')
return 0;

if (get_4(&p) != HEADER_SIZE + ca.width * ca.height * 4)
if (get_4(&p) != (unsigned int)HEADER_SIZE + ca.width * ca.height * 4)
return 0;

get_4(&p);
Expand All @@ -57,9 +57,9 @@ static int read_bmp_header(const unsigned char *p)
if (get_4(&p) != 40)
return 0;

if (get_4(&p) != ca.width)
if (get_4(&p) != (unsigned int)ca.width)
return 0;
if (get_4(&p) != -ca.height)
if (get_4(&p) != (unsigned int)-ca.height)
return 0;

get_2(&p);
Expand All @@ -68,7 +68,7 @@ static int read_bmp_header(const unsigned char *p)

if (get_4(&p) != 0)
return 0;
if (get_4(&p) != ca.width * ca.height * 4)
if (get_4(&p) != (unsigned int)ca.width * ca.height * 4)
return 0;

get_4(&p);
Expand All @@ -94,7 +94,8 @@ void cairo_read_bmp(void)
if (!read_bmp_header(header))
G_fatal_error(_("Cairo: Invalid BMP header for <%s>"), ca.file_name);

if (fread(ca.grid, ca.stride, ca.height, input) != ca.height) {
if (fread(ca.grid, ca.stride, ca.height, input) !=
(unsigned int)ca.height) {
if (feof(input))
G_fatal_error(_("Cairo: error reading BMP file <%s>: "
"unexpected end of file"),
Expand Down
2 changes: 1 addition & 1 deletion lib/calc/xrand.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int f_rand(int argc, const int *argt, void **args)
lo = hi;
hi = tmp;
}
res[i] = (lo == hi) ? lo : lo + x % (unsigned int)(hi - lo);
res[i] = (lo == hi) ? lo : (int)(lo + x % (unsigned int)(hi - lo));
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/db/dbmi_driver/d_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void db_d_append_error(const char *fmt, ...)
count = vfprintf(fp, fmt, ap);
if (count >= 0 && (work = G_calloc(count + 1, 1))) {
rewind(fp);
if (fread(work, 1, count, fp) != count) {
if (fread(work, 1, count, fp) != (size_t)count) {
if (ferror(fp))
G_fatal_error(_("DBMI-%s driver file reading error: %s"),
st->driver_name, strerror(errno));
Expand Down
4 changes: 2 additions & 2 deletions lib/driver/font2.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ static void read_hersh(const char *filename)
char buf[8];
struct glyph *glyph;
int coords;
unsigned int idx, count;
int c, i;
unsigned int i, idx, count;
int c;

switch (c = fgetc(fp)) {
case '\r':
Expand Down
3 changes: 2 additions & 1 deletion lib/dspf/dspf_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ int dfwrite_header(file_info *headp)
if (1 != fwrite(&linep->nthres, isize, 1, fp))
return (-1);
/* write the array of thresholds out */
if ((fwrite(linep->tvalue, flsize, linep->nthres, fp)) != linep->nthres) {
if ((fwrite(linep->tvalue, flsize, linep->nthres, fp)) !=
(size_t)linep->nthres) {
fprintf(stderr, "ERROR: fwrite in dspf_header.c\n");
return (-1);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/gis/asprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int G_rasprintf(char **out, size_t *size, const char *fmt, ...)
va_start(ap, fmt);
count = vsnprintf(buf, osize, fmt, ap);
va_end(ap);
if (count >= 0 && count < osize)
if (count >= 0 && (size_t)count < osize)
break;
if (count > -1)
osize = count + 1;
Expand Down
6 changes: 3 additions & 3 deletions lib/gis/cmprbzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ int G_bz2_compress(unsigned char *src, int src_sz, unsigned char *dst,
int dst_sz)
{
int err;
int i, buf_sz;
unsigned int nbytes;
int buf_sz;
unsigned int i, nbytes;
unsigned char *buf;

#ifndef HAVE_BZLIB_H
Expand Down Expand Up @@ -212,7 +212,7 @@ int G_bz2_expand(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz)
* updated buffer size
*/

if (nbytes != dst_sz) {
if (dst_sz < 0 || nbytes != (unsigned int)dst_sz) {
/* TODO: it is not an error if destination is larger than needed */
G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes,
dst_sz);
Expand Down
9 changes: 4 additions & 5 deletions lib/gis/cmprzlib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
****************************************************************************
/*****************************************************************************
* -- GRASS Development Team --
*
* MODULE: GRASS gis library
Expand Down Expand Up @@ -116,7 +115,7 @@ int G_zlib_compress(unsigned char *src, int src_sz, unsigned char *dst,
/* Output buffer should be large enough for single pass compression */
buf = dst;
buf_sz = G_zlib_compress_bound(src_sz);
if (buf_sz > dst_sz) {
if (dst_sz < 0 || buf_sz > (unsigned int)dst_sz) {
G_warning(
"G_zlib_compress(): programmer error, destination is too small");
if (NULL ==
Expand Down Expand Up @@ -145,7 +144,7 @@ int G_zlib_compress(unsigned char *src, int src_sz, unsigned char *dst,
}

/* updated buf_sz is bytes of compressed data */
if (nbytes >= src_sz) {
if (src_sz < 0 || nbytes >= (unsigned int)src_sz) {
/* compression not possible */
if (buf != dst)
G_free(buf);
Expand Down Expand Up @@ -205,7 +204,7 @@ int G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
* updated buffer size
*/

if (nbytes != dst_sz) {
if (dst_sz < 0 || nbytes != (unsigned int)dst_sz) {
/* TODO: it is not an error if destination is larger than needed */
G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes,
dst_sz);
Expand Down
2 changes: 1 addition & 1 deletion lib/gis/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void G_ls_format(char **list, int num_items, int perline, FILE *stream)
#endif

if (perline == 0) {
int max_len = 0;
unsigned int max_len = 0;

for (i = 0; i < num_items; i++) {
/* Find maximum filename length */
Expand Down
5 changes: 4 additions & 1 deletion lib/gis/parser_dependencies.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,11 @@ void G__describe_option_rules_xml(FILE *fp)
for (i = 0; i < rules.count; i++) {
const struct rule *rule = &((const struct rule *)rules.data)[i];

if (rule->count < 0)
G_fatal_error(_("Internal error: the number of options is < 0"));

fprintf(fp, "\t\t<rule type=\"%s\">\n", rule_types[rule->type]);
for (j = 0; j < rule->count; j++) {
for (j = 0; j < (unsigned int)rule->count; j++) {
void *p = rule->opts[j];

if (is_flag(p)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/gis/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int G_snprintf(char *str, size_t size, const char *fmt, ...)
va_end(ap);

/* Windows' vsnprintf() doesn't always NUL-terminate the buffer */
if (count == size)
if (count >= 0 && (unsigned int)count == size)
str[--count] = '\0';

return count;
Expand Down
5 changes: 4 additions & 1 deletion lib/gis/user_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#ifndef __MINGW32__
#include <pwd.h>
#endif
Expand Down Expand Up @@ -155,10 +157,11 @@ static int _elem_count_split(char *elems)
/* Some basic assertions */
assert(elems != NULL);
assert((len = strlen(elems)) > 0);
assert(len < PTRDIFF_MAX);
assert(*elems != '/');

begin = elems;
for (i = 0; begin != NULL && len > begin - elems; i++) {
for (i = 0; begin != NULL && (ptrdiff_t)len > begin - elems; i++) {
/* check '.' condition */
if (*begin == '.')
return 0;
Expand Down
Loading