diff --git a/capn-stream.c b/capn-stream.c index b734dc7..bfd607d 100644 --- a/capn-stream.c +++ b/capn-stream.c @@ -80,7 +80,7 @@ int capn_deflate(struct capn_stream* s) { continue; default: - if (s->avail_out < 1 + sz) + if (s->avail_out < 1U + sz) return CAPN_NEED_MORE; *(s->next_out++) = hdr; @@ -167,7 +167,7 @@ int capn_inflate(struct capn_stream* s) { if (hdr & (1 << i)) sz++; } - if (s->avail_in < 1 + sz) + if (s->avail_in < 1U + sz) return CAPN_NEED_MORE; s->next_in += 1; diff --git a/capn-test.cpp b/capn-test.cpp index cf76bc0..b9a0b9f 100644 --- a/capn-test.cpp +++ b/capn-test.cpp @@ -274,11 +274,11 @@ static void getSegments(struct capn *c, struct capn_segment **s, size_t num) { ASSERT_EQ(num, c->segnum); s[0] = c->seglist; - for (unsigned i = 1; i < num; i++) { + for (size_t i = 1; i < num; i++) { s[i] = s[i-1]->next; } - for (unsigned i = 0; i < num; i++) { + for (size_t i = 0; i < num; i++) { EXPECT_EQ(s[i]->id, i); } } @@ -315,7 +315,7 @@ TEST(WireFormat, StructRoundTrip_OneSegmentPerAllocation) { struct capn ctx2; memset(&ctx2, 0, sizeof(ctx2)); - for (unsigned i = 0; i < sizeof(segments)/sizeof(segments[0]); i++) { + for (size_t i = 0; i < sizeof(segments)/sizeof(segments[0]); i++) { capn_append_segment(&ctx2, segments[i]); } @@ -371,7 +371,7 @@ TEST(WireFormat, StructRoundTrip_OneSegmentPerAllocation_NoTag) { struct capn ctx2; memset(&ctx2, 0, sizeof(ctx2)); - for (unsigned i = 0; i < sizeof(segments)/sizeof(segments[0]); i++) { + for (size_t i = 0; i < sizeof(segments)/sizeof(segments[0]); i++) { capn_append_segment(&ctx2, segments[i]); } @@ -421,7 +421,7 @@ TEST(WireFormat, StructRoundTrip_MultipleSegmentsWithMultipleAllocations) { struct capn ctx2; memset(&ctx2, 0, sizeof(ctx2)); - for (unsigned i = 0; i < sizeof(segments)/sizeof(segments[0]); i++) { + for (size_t i = 0; i < sizeof(segments)/sizeof(segments[0]); i++) { capn_append_segment(&ctx2, segments[i]); } diff --git a/capn.c b/capn.c index 256755a..9ae5633 100644 --- a/capn.c +++ b/capn.c @@ -186,7 +186,7 @@ end: } static struct capn_segment *lookup_segment(struct capn* c, struct capn_segment *s, uint32_t id) { - struct capn_tree **x = &c->segtree; + struct capn_tree **x; struct capn_segment *y = NULL; if (s && s->id == id) @@ -195,6 +195,7 @@ static struct capn_segment *lookup_segment(struct capn* c, struct capn_segment * return NULL; if (id < c->segnum) { + x = &c->segtree; while (*x) { y = (struct capn_segment*) *x; if (id == y->id) { @@ -205,6 +206,9 @@ static struct capn_segment *lookup_segment(struct capn* c, struct capn_segment * x = &y->hdr.link[1]; } } + } else { + /* Otherwise `x` may be uninitialized */ + return NULL; } s = c->lookup ? c->lookup(c->user, id) : NULL; @@ -229,7 +233,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) { uint64_t far, tag; - uint32_t off = (U32(val) >> 3) * 8; + size_t off = (U32(val) >> 3) * 8; char *p; if ((*s = lookup_segment((*s)->capn, *s, U32(val >> 32))) == NULL) { @@ -264,7 +268,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) { - 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) { return 0; @@ -369,7 +373,7 @@ static capn_ptr read_ptr(struct capn_segment *s, char *d) { e = d + ret.len * 8; break; case COMPOSITE_LIST: - if (d+8-s->data > s->len) { + if ((size_t)((d+8) - s->data) > s->len) { goto err; } @@ -394,7 +398,7 @@ static capn_ptr read_ptr(struct capn_segment *s, char *d) { goto err; } - if (e - s->data > s->len) + if ((size_t)(e - s->data) > s->len) goto err; ret.data = d; @@ -664,7 +668,7 @@ static int copy_ptr(struct capn_segment *seg, char *data, struct capn_ptr *t, st struct capn_segment *cs = c->copylist; /* 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; if (!cs) { /* can't allocate a copy structure */ diff --git a/capn.h b/capn.h index 26050a1..2af882d 100644 --- a/capn.h +++ b/capn.h @@ -94,7 +94,7 @@ struct capn_segment { uint32_t id; /* user settable */ char *data; - unsigned len, cap; + size_t len, cap; void *user; }; @@ -262,9 +262,9 @@ void capn_reset_copy(struct capn *c); */ struct capn_stream { const uint8_t *next_in; - unsigned avail_in; + size_t avail_in; uint8_t *next_out; - unsigned avail_out; + size_t avail_out; unsigned zeros, raw; }; diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index c8675a4..42f0d3a 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -930,7 +930,7 @@ static void define_struct(struct node *n) { } #if 0 -Commenting out interfaces until the RPC protocol has been specified +/* Commenting out interfaces until the RPC protocol has been spec'd */ static int find_offset(struct str *v, int inc, uint64_t mask) { int i, j; union {uint64_t u; char c[8];} umask; @@ -1248,7 +1248,7 @@ int main() { fprintf(srcf, "static const capn_ptr capn_null = {CAPN_NULL};\n"); 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++) { if (j > 8) fprintf(srcf, ","); @@ -1258,7 +1258,7 @@ int main() { } 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); }