signedness: Fix less obvious issues

* My compilers mark these as errors
* Attempt to be more correct
* Tested on gcc-5.2.0 and clang-3.6.2
This commit is contained in:
Kyle Manna 2015-08-17 18:52:36 -07:00
parent e933510236
commit 376b63fb81
6 changed files with 15 additions and 15 deletions

View file

@ -161,7 +161,7 @@ capn_write_mem(struct capn *c, uint8_t *p, size_t sz, int packed)
{ {
struct capn_segment *seg; struct capn_segment *seg;
struct capn_ptr root; struct capn_ptr root;
int i; uint32_t i;
uint32_t headerlen; uint32_t headerlen;
size_t datasz; size_t datasz;
uint32_t *header; uint32_t *header;

View file

@ -79,7 +79,7 @@ int capn_deflate(struct capn_stream* s) {
continue; continue;
default: default:
if (s->avail_out < 1 + sz) if (s->avail_out < 1U + sz)
return CAPN_NEED_MORE; return CAPN_NEED_MORE;
*(s->next_out++) = hdr; *(s->next_out++) = hdr;
@ -166,7 +166,7 @@ int capn_inflate(struct capn_stream* s) {
if (hdr & (1 << i)) if (hdr & (1 << i))
sz++; sz++;
} }
if (s->avail_in < 2 + sz) if (s->avail_in < 2U + sz)
return CAPN_NEED_MORE; return CAPN_NEED_MORE;
s->next_in += 2; s->next_in += 2;

View file

@ -273,11 +273,11 @@ static void getSegments(struct capn *c, struct capn_segment **s, size_t num) {
ASSERT_EQ(num, c->segnum); ASSERT_EQ(num, c->segnum);
s[0] = c->seglist; s[0] = c->seglist;
for (int i = 1; i < num; i++) { for (size_t i = 1; i < num; i++) {
s[i] = s[i-1]->next; s[i] = s[i-1]->next;
} }
for (int i = 0; i < num; i++) { for (size_t i = 0; i < num; i++) {
EXPECT_EQ(s[i]->id, i); EXPECT_EQ(s[i]->id, i);
} }
} }

10
capn.c
View file

@ -231,7 +231,7 @@ static struct capn_segment *lookup_segment(struct capn* c, struct capn_segment *
static uint64_t lookup_double(struct capn_segment **s, char **d, uint64_t val) { static uint64_t lookup_double(struct capn_segment **s, char **d, uint64_t val) {
uint64_t far, tag; uint64_t far, tag;
uint32_t off = (U32(val) >> 3) * 8; size_t off = (U32(val) >> 3) * 8;
char *p; char *p;
if ((*s = lookup_segment((*s)->capn, *s, U32(val >> 32))) == NULL) { if ((*s = lookup_segment((*s)->capn, *s, U32(val >> 32))) == NULL) {
@ -266,7 +266,7 @@ static uint64_t lookup_double(struct capn_segment **s, char **d, uint64_t val) {
} }
static uint64_t lookup_far(struct capn_segment **s, char **d, uint64_t val) { static uint64_t lookup_far(struct capn_segment **s, char **d, uint64_t val) {
uint32_t off = (U32(val) >> 3) * 8; size_t off = (U32(val) >> 3) * 8;
if ((*s = lookup_segment((*s)->capn, *s, U32(val >> 32))) == NULL) { if ((*s = lookup_segment((*s)->capn, *s, U32(val >> 32))) == NULL) {
return 0; return 0;
@ -371,7 +371,7 @@ static capn_ptr read_ptr(struct capn_segment *s, char *d) {
e = d + ret.len * 8; e = d + ret.len * 8;
break; break;
case COMPOSITE_LIST: case COMPOSITE_LIST:
if (d+8-s->data > s->len) { if ((size_t)((d+8) - s->data) > s->len) {
goto err; goto err;
} }
@ -396,7 +396,7 @@ static capn_ptr read_ptr(struct capn_segment *s, char *d) {
goto err; goto err;
} }
if (e - s->data > s->len) if ((size_t)(e - s->data) > s->len)
goto err; goto err;
ret.data = d; ret.data = d;
@ -666,7 +666,7 @@ static int copy_ptr(struct capn_segment *seg, char *data, struct capn_ptr *t, st
struct capn_segment *cs = c->copylist; struct capn_segment *cs = c->copylist;
/* need to allocate a struct copy */ /* need to allocate a struct copy */
if (!cs || cs->len + sizeof(*n) > cs->cap) { if (!cs || cs->len + (int)sizeof(*n) > cs->cap) {
cs = c->create_local ? c->create_local(c->user, sizeof(*n)) : NULL; cs = c->create_local ? c->create_local(c->user, sizeof(*n)) : NULL;
if (!cs) { if (!cs) {
/* can't allocate a copy structure */ /* can't allocate a copy structure */

6
capn.h
View file

@ -94,7 +94,7 @@ struct capn_segment {
uint32_t id; uint32_t id;
/* user settable */ /* user settable */
char *data; char *data;
int len, cap; size_t len, cap;
void *user; void *user;
}; };
@ -261,9 +261,9 @@ void capn_reset_copy(struct capn *c);
*/ */
struct capn_stream { struct capn_stream {
const uint8_t *next_in; const uint8_t *next_in;
int avail_in; size_t avail_in;
uint8_t *next_out; uint8_t *next_out;
int avail_out; size_t avail_out;
int zeros, raw; int zeros, raw;
}; };

View file

@ -1216,7 +1216,7 @@ int main() {
fprintf(srcf, "static const capn_ptr capn_null = {CAPN_NULL};\n"); fprintf(srcf, "static const capn_ptr capn_null = {CAPN_NULL};\n");
if (g_valseg.len > 8) { if (g_valseg.len > 8) {
fprintf(srcf, "static const uint8_t capn_buf[%d] = {", g_valseg.len-8); fprintf(srcf, "static const uint8_t capn_buf[%lu] = {", g_valseg.len-8);
for (j = 8; j < g_valseg.len; j++) { for (j = 8; j < g_valseg.len; j++) {
if (j > 8) if (j > 8)
fprintf(srcf, ","); fprintf(srcf, ",");
@ -1226,7 +1226,7 @@ int main() {
} }
fprintf(srcf, "\n};\n"); fprintf(srcf, "\n};\n");
fprintf(srcf, "static const struct capn_segment capn_seg = {{0},0,0,0,(char*)&capn_buf[0],%d,%d};\n", fprintf(srcf, "static const struct capn_segment capn_seg = {{0},0,0,0,(char*)&capn_buf[0],%lu,%lu};\n",
g_valseg.len-8, g_valseg.len-8); g_valseg.len-8, g_valseg.len-8);
} }