Skip to content

Commit

Permalink
bar, block, json, sched: fix different signedness comparisons
Browse files Browse the repository at this point in the history
This fixes 3 comparisons between different signedness integers.

The first one is the integer variable used to increment over the blocks
in for loops. The number of blocks is defined as an unsigned int in
bar.h. Thus, the iterate variable i has to be the same signedness. This
commit fixes all declarations of that variable.

The second one is the signal type defined in block structure. In C,
signal numbers are defined as int. This commit fixes the definition in
block.h.

The third one is the comparison between the result of snprint()(ie. int)
and a sizeof variable (ie. size_t). This comparison requires the int to
be casted in unsigned, since the if statement ensures that the value is
not negative. This commit casts the int variable in size_t when it is
compared to a size_t.
  • Loading branch information
gportay committed Jan 3, 2019
1 parent 30fa33e commit 232f094
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions bar.c
Expand Up @@ -50,7 +50,7 @@ static struct {

static bool i3bar_is_string(const char *key)
{
int i;
unsigned int i;

for (i = 0; i < sizeof(i3bar_keys) / sizeof(i3bar_keys[0]); i++)
if (strcmp(i3bar_keys[i].key, key) == 0)
Expand Down Expand Up @@ -102,7 +102,7 @@ static void i3bar_dump_block(struct block *block)

static void i3bar_dump(struct bar *bar)
{
int i;
unsigned int i;

fprintf(stdout, ",[{\"full_text\":\"\"}");

Expand Down Expand Up @@ -215,7 +215,7 @@ static struct block *bar_find(struct bar *bar, const struct map *map)
const char *block_name, *block_instance;
const char *map_name, *map_instance;
struct block *block;
int i;
unsigned int i;

/* "name" and "instance" are the only identifiers provided by i3bar */
map_name = map_get(map, "name") ? : "";
Expand Down Expand Up @@ -347,7 +347,7 @@ void bar_schedule(struct bar *bar)

void bar_destroy(struct bar *bar)
{
int i;
unsigned int i;

i3bar_stop(bar);

Expand Down
2 changes: 1 addition & 1 deletion block.h
Expand Up @@ -42,7 +42,7 @@ struct block {
/* Shortcuts */
const char *command;
int interval;
unsigned signal;
int signal;
unsigned format;

/* Runtime info */
Expand Down
2 changes: 1 addition & 1 deletion json.c
Expand Up @@ -452,7 +452,7 @@ int json_escape(const char *str, char *buf, size_t size)
}

/* Ensure the result was not truncated */
if (len < 0 || len >= size)
if (len < 0 || (size_t)len >= size)
return -ENOSPC;

size -= len;
Expand Down
17 changes: 9 additions & 8 deletions sched.c
Expand Up @@ -39,8 +39,7 @@ gcd(int a, int b)
static unsigned int
longest_sleep(struct bar *bar)
{
unsigned int time = 0;
int i;
unsigned int i, time = 0;

if (bar->num > 0 && bar->blocks->interval > 0)
time = bar->blocks->interval; /* first block's interval */
Expand Down Expand Up @@ -158,7 +157,7 @@ sched_init(struct bar *bar)

static void sched_poll_timed(struct bar *bar)
{
int i;
unsigned int i;

for (i = 0; i < bar->num; i++) {
struct block *block = bar->blocks + i;
Expand All @@ -173,7 +172,7 @@ static void sched_poll_timed(struct bar *bar)

static void sched_poll_expired(struct bar *bar)
{
int i;
unsigned int i;

for (i = 0; i < bar->num; i++) {
struct block *block = bar->blocks + i;
Expand All @@ -198,7 +197,7 @@ static void sched_poll_expired(struct bar *bar)

static void sched_poll_signaled(struct bar *bar, int sig)
{
int i;
unsigned int i;

for (i = 0; i < bar->num; i++) {
struct block *block = bar->blocks + i;
Expand All @@ -213,8 +212,9 @@ static void sched_poll_signaled(struct bar *bar, int sig)

static void sched_poll_exited(struct bar *bar)
{
unsigned int i;
pid_t pid;
int i, err;
int err;

for (;;) {
err = sys_waitid(&pid);
Expand Down Expand Up @@ -246,7 +246,7 @@ static void sched_poll_exited(struct bar *bar)

static void sched_poll_readable(struct bar *bar, const int fd)
{
int i;
unsigned int i;

for (i = 0; i < bar->num; i++) {
struct block *block = bar->blocks + i;
Expand All @@ -263,7 +263,8 @@ void
sched_start(struct bar *bar)
{
struct block *block;
int i, sig, fd;
unsigned int i;
int sig, fd;
int err;

/* First forks (for commands with an interval) */
Expand Down

0 comments on commit 232f094

Please sign in to comment.