Git fork

Merge branch 'ps/reftable-sign-compare'

The reftable/ library code has been made -Wsign-compare clean.

* ps/reftable-sign-compare:
reftable: address trivial -Wsign-compare warnings
reftable/blocksource: adjust `read_block()` to return `ssize_t`
reftable/blocksource: adjust type of the block length
reftable/block: adjust type of the restart length
reftable/block: adapt header and footer size to return a `size_t`
reftable/basics: adjust `hash_size()` to return `uint32_t`
reftable/basics: adjust `common_prefix_size()` to return `size_t`
reftable/record: handle overflows when decoding varints
reftable/record: drop unused `print` function pointer
meson: stop disabling -Wsign-compare

+156 -150
-1
meson.build
··· 708 708 # These are disabled because we have these all over the place. 709 709 '-Wno-empty-body', 710 710 '-Wno-missing-field-initializers', 711 - '-Wno-sign-compare', 712 711 ] 713 712 if compiler.has_argument(cflag) 714 713 libgit_c_args += cflag
+4 -6
reftable/basics.c
··· 263 263 return a[i] == b[i]; 264 264 } 265 265 266 - int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b) 266 + size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b) 267 267 { 268 - int p = 0; 269 - for (; p < a->len && p < b->len; p++) { 268 + size_t p = 0; 269 + for (; p < a->len && p < b->len; p++) 270 270 if (a->buf[p] != b->buf[p]) 271 271 break; 272 - } 273 - 274 272 return p; 275 273 } 276 274 277 - int hash_size(enum reftable_hash id) 275 + uint32_t hash_size(enum reftable_hash id) 278 276 { 279 277 if (!id) 280 278 return REFTABLE_HASH_SIZE_SHA1;
+2 -2
reftable/basics.h
··· 169 169 #endif 170 170 171 171 /* Find the longest shared prefix size of `a` and `b` */ 172 - int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b); 172 + size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b); 173 173 174 - int hash_size(enum reftable_hash id); 174 + uint32_t hash_size(enum reftable_hash id); 175 175 176 176 /* 177 177 * Format IDs that identify the hash function used by a reftable. Note that
+9 -11
reftable/block.c
··· 15 15 #include "system.h" 16 16 #include <zlib.h> 17 17 18 - int header_size(int version) 18 + size_t header_size(int version) 19 19 { 20 20 switch (version) { 21 21 case 1: ··· 26 26 abort(); 27 27 } 28 28 29 - int footer_size(int version) 29 + size_t footer_size(int version) 30 30 { 31 31 switch (version) { 32 32 case 1: ··· 40 40 static int block_writer_register_restart(struct block_writer *w, int n, 41 41 int is_restart, struct reftable_buf *key) 42 42 { 43 - int rlen, err; 43 + uint32_t rlen; 44 + int err; 44 45 45 46 rlen = w->restart_len; 46 - if (rlen >= MAX_RESTARTS) { 47 + if (rlen >= MAX_RESTARTS) 47 48 is_restart = 0; 48 - } 49 49 50 - if (is_restart) { 50 + if (is_restart) 51 51 rlen++; 52 - } 53 52 if (2 + 3 * rlen + n > w->block_size - w->next) 54 53 return -1; 55 54 if (is_restart) { ··· 72 71 } 73 72 74 73 int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block, 75 - uint32_t block_size, uint32_t header_off, int hash_size) 74 + uint32_t block_size, uint32_t header_off, uint32_t hash_size) 76 75 { 77 76 bw->block = block; 78 77 bw->hash_size = hash_size; ··· 148 147 149 148 int block_writer_finish(struct block_writer *w) 150 149 { 151 - int i; 152 - for (i = 0; i < w->restart_len; i++) { 150 + for (uint32_t i = 0; i < w->restart_len; i++) { 153 151 put_be24(w->block + w->next, w->restarts[i]); 154 152 w->next += 3; 155 153 } ··· 214 212 215 213 int block_reader_init(struct block_reader *br, struct reftable_block *block, 216 214 uint32_t header_off, uint32_t table_block_size, 217 - int hash_size) 215 + uint32_t hash_size) 218 216 { 219 217 uint32_t full_block_size = table_block_size; 220 218 uint8_t typ = block->data[header_off];
+7 -7
reftable/block.h
··· 30 30 31 31 /* How often to restart keys. */ 32 32 uint16_t restart_interval; 33 - int hash_size; 33 + uint32_t hash_size; 34 34 35 35 /* Offset of next uint8_t to write. */ 36 36 uint32_t next; ··· 48 48 * initializes the blockwriter to write `typ` entries, using `block` as temporary 49 49 * storage. `block` is not owned by the block_writer. */ 50 50 int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block, 51 - uint32_t block_size, uint32_t header_off, int hash_size); 51 + uint32_t block_size, uint32_t header_off, uint32_t hash_size); 52 52 53 53 /* returns the block type (eg. 'r' for ref records. */ 54 54 uint8_t block_writer_type(struct block_writer *bw); ··· 72 72 73 73 /* the memory block */ 74 74 struct reftable_block block; 75 - int hash_size; 75 + uint32_t hash_size; 76 76 77 77 /* Uncompressed data for log entries. */ 78 78 z_stream *zstream; ··· 92 92 /* initializes a block reader. */ 93 93 int block_reader_init(struct block_reader *br, struct reftable_block *bl, 94 94 uint32_t header_off, uint32_t table_block_size, 95 - int hash_size); 95 + uint32_t hash_size); 96 96 97 97 void block_reader_release(struct block_reader *br); 98 98 ··· 108 108 uint32_t next_off; 109 109 const unsigned char *block; 110 110 size_t block_len; 111 - int hash_size; 111 + uint32_t hash_size; 112 112 113 113 /* key for last entry we read. */ 114 114 struct reftable_buf last_key; ··· 137 137 void block_iter_close(struct block_iter *it); 138 138 139 139 /* size of file header, depending on format version */ 140 - int header_size(int version); 140 + size_t header_size(int version); 141 141 142 142 /* size of file footer, depending on format version */ 143 - int footer_size(int version); 143 + size_t footer_size(int version); 144 144 145 145 /* returns a block to its source. */ 146 146 void reftable_block_done(struct reftable_block *ret);
+4 -4
reftable/blocksource.c
··· 24 24 { 25 25 } 26 26 27 - static int reftable_buf_read_block(void *v, struct reftable_block *dest, 28 - uint64_t off, uint32_t size) 27 + static ssize_t reftable_buf_read_block(void *v, struct reftable_block *dest, 28 + uint64_t off, uint32_t size) 29 29 { 30 30 struct reftable_buf *b = v; 31 31 assert(off + size <= b->len); ··· 78 78 reftable_free(b); 79 79 } 80 80 81 - static int file_read_block(void *v, struct reftable_block *dest, uint64_t off, 82 - uint32_t size) 81 + static ssize_t file_read_block(void *v, struct reftable_block *dest, uint64_t off, 82 + uint32_t size) 83 83 { 84 84 struct file_block_source *b = v; 85 85 assert(off + size <= b->size);
+18 -14
reftable/reader.c
··· 20 20 return source->ops->size(source->arg); 21 21 } 22 22 23 - int block_source_read_block(struct reftable_block_source *source, 24 - struct reftable_block *dest, uint64_t off, 25 - uint32_t size) 23 + ssize_t block_source_read_block(struct reftable_block_source *source, 24 + struct reftable_block *dest, uint64_t off, 25 + uint32_t size) 26 26 { 27 - int result = source->ops->read_block(source->arg, dest, off, size); 27 + ssize_t result = source->ops->read_block(source->arg, dest, off, size); 28 28 dest->source = *source; 29 29 return result; 30 30 } ··· 57 57 struct reftable_block *dest, uint64_t off, 58 58 uint32_t sz) 59 59 { 60 + ssize_t bytes_read; 60 61 if (off >= r->size) 61 62 return 0; 63 + if (off + sz > r->size) 64 + sz = r->size - off; 62 65 63 - if (off + sz > r->size) { 64 - sz = r->size - off; 65 - } 66 + bytes_read = block_source_read_block(&r->source, dest, off, sz); 67 + if (bytes_read < 0) 68 + return (int)bytes_read; 66 69 67 - return block_source_read_block(&r->source, dest, off, sz); 70 + return 0; 68 71 } 69 72 70 73 enum reftable_hash reftable_reader_hash_id(struct reftable_reader *r) ··· 601 604 struct reftable_reader *r; 602 605 uint64_t file_size = block_source_size(source); 603 606 uint32_t read_size; 607 + ssize_t bytes_read; 604 608 int err; 605 609 606 610 REFTABLE_CALLOC_ARRAY(r, 1); ··· 619 623 goto done; 620 624 } 621 625 622 - err = block_source_read_block(source, &header, 0, read_size); 623 - if (err != read_size) { 626 + bytes_read = block_source_read_block(source, &header, 0, read_size); 627 + if (bytes_read < 0 || (size_t)bytes_read != read_size) { 624 628 err = REFTABLE_IO_ERROR; 625 629 goto done; 626 630 } ··· 645 649 r->hash_id = 0; 646 650 r->refcount = 1; 647 651 648 - err = block_source_read_block(source, &footer, r->size, 649 - footer_size(r->version)); 650 - if (err != footer_size(r->version)) { 652 + bytes_read = block_source_read_block(source, &footer, r->size, 653 + footer_size(r->version)); 654 + if (bytes_read < 0 || (size_t)bytes_read != footer_size(r->version)) { 651 655 err = REFTABLE_IO_ERROR; 652 656 goto done; 653 657 } ··· 750 754 struct table_iter *ti; 751 755 struct filtering_ref_iterator *filter = NULL; 752 756 struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT; 753 - int oid_len = hash_size(r->hash_id); 757 + uint32_t oid_len = hash_size(r->hash_id); 754 758 int err; 755 759 756 760 REFTABLE_ALLOC_ARRAY(ti, 1);
+3 -3
reftable/reader.h
··· 16 16 17 17 uint64_t block_source_size(struct reftable_block_source *source); 18 18 19 - int block_source_read_block(struct reftable_block_source *source, 20 - struct reftable_block *dest, uint64_t off, 21 - uint32_t size); 19 + ssize_t block_source_read_block(struct reftable_block_source *source, 20 + struct reftable_block *dest, uint64_t off, 21 + uint32_t size); 22 22 void block_source_close(struct reftable_block_source *source); 23 23 24 24 /* metadata for a block type */
+58 -65
reftable/record.c
··· 21 21 22 22 int get_var_int(uint64_t *dest, struct string_view *in) 23 23 { 24 - int ptr = 0; 24 + const unsigned char *buf = in->buf; 25 + unsigned char c; 25 26 uint64_t val; 26 27 27 - if (in->len == 0) 28 + if (!in->len) 28 29 return -1; 29 - val = in->buf[ptr] & 0x7f; 30 + c = *buf++; 31 + val = c & 0x7f; 30 32 31 - while (in->buf[ptr] & 0x80) { 32 - ptr++; 33 - if (ptr > in->len) { 33 + while (c & 0x80) { 34 + /* 35 + * We use a micro-optimization here: whenever we see that the 36 + * 0x80 bit is set, we know that the remainder of the value 37 + * cannot be 0. The zero-values thus doesn't need to be encoded 38 + * at all, which is why we subtract 1 when encoding and add 1 39 + * when decoding. 40 + * 41 + * This allows us to save a byte in some edge cases. 42 + */ 43 + val += 1; 44 + if (!val || (val & (uint64_t)(~0ULL << (64 - 7)))) 45 + return -1; /* overflow */ 46 + if (buf >= in->buf + in->len) 34 47 return -1; 35 - } 36 - val = (val + 1) << 7 | (uint64_t)(in->buf[ptr] & 0x7f); 48 + c = *buf++; 49 + val = (val << 7) + (c & 0x7f); 37 50 } 38 51 39 52 *dest = val; 40 - return ptr + 1; 53 + return buf - in->buf; 41 54 } 42 55 43 - int put_var_int(struct string_view *dest, uint64_t val) 56 + int put_var_int(struct string_view *dest, uint64_t value) 44 57 { 45 - uint8_t buf[10] = { 0 }; 46 - int i = 9; 47 - int n = 0; 48 - buf[i] = (uint8_t)(val & 0x7f); 49 - i--; 50 - while (1) { 51 - val >>= 7; 52 - if (!val) { 53 - break; 54 - } 55 - val--; 56 - buf[i] = 0x80 | (uint8_t)(val & 0x7f); 57 - i--; 58 - } 59 - 60 - n = sizeof(buf) - i - 1; 61 - if (dest->len < n) 58 + unsigned char varint[10]; 59 + unsigned pos = sizeof(varint) - 1; 60 + varint[pos] = value & 0x7f; 61 + while (value >>= 7) 62 + varint[--pos] = 0x80 | (--value & 0x7f); 63 + if (dest->len < sizeof(varint) - pos) 62 64 return -1; 63 - memcpy(dest->buf, &buf[i + 1], n); 64 - return n; 65 + memcpy(dest->buf, varint + pos, sizeof(varint) - pos); 66 + return sizeof(varint) - pos; 65 67 } 66 68 67 69 int reftable_is_block_type(uint8_t typ) ··· 124 126 static int encode_string(const char *str, struct string_view s) 125 127 { 126 128 struct string_view start = s; 127 - int l = strlen(str); 129 + size_t l = strlen(str); 128 130 int n = put_var_int(&s, l); 129 131 if (n < 0) 130 132 return -1; ··· 142 144 uint8_t extra) 143 145 { 144 146 struct string_view start = dest; 145 - int prefix_len = common_prefix_size(&prev_key, &key); 147 + size_t prefix_len = common_prefix_size(&prev_key, &key); 146 148 uint64_t suffix_len = key.len - prefix_len; 147 - int n = put_var_int(&dest, (uint64_t)prefix_len); 149 + int n = put_var_int(&dest, prefix_len); 148 150 if (n < 0) 149 151 return -1; 150 152 string_view_consume(&dest, n); ··· 227 229 } 228 230 229 231 static int reftable_ref_record_copy_from(void *rec, const void *src_rec, 230 - int hash_size) 232 + uint32_t hash_size) 231 233 { 232 234 struct reftable_ref_record *ref = rec; 233 235 const struct reftable_ref_record *src = src_rec; 234 236 char *refname = NULL; 235 237 size_t refname_cap = 0; 236 238 int err; 237 - 238 - assert(hash_size > 0); 239 239 240 240 SWAP(refname, ref->refname); 241 241 SWAP(refname_cap, ref->refname_cap); ··· 317 317 } 318 318 319 319 static int reftable_ref_record_encode(const void *rec, struct string_view s, 320 - int hash_size) 320 + uint32_t hash_size) 321 321 { 322 322 const struct reftable_ref_record *r = 323 323 (const struct reftable_ref_record *)rec; 324 324 struct string_view start = s; 325 325 int n = put_var_int(&s, r->update_index); 326 - assert(hash_size > 0); 327 326 if (n < 0) 328 327 return -1; 329 328 string_view_consume(&s, n); ··· 363 362 364 363 static int reftable_ref_record_decode(void *rec, struct reftable_buf key, 365 364 uint8_t val_type, struct string_view in, 366 - int hash_size, struct reftable_buf *scratch) 365 + uint32_t hash_size, struct reftable_buf *scratch) 367 366 { 368 367 struct reftable_ref_record *r = rec; 369 368 struct string_view start = in; ··· 371 370 const char *refname = NULL; 372 371 size_t refname_cap = 0; 373 372 int n, err; 374 - 375 - assert(hash_size > 0); 376 373 377 374 n = get_var_int(&update_index, &in); 378 375 if (n < 0) ··· 449 446 } 450 447 451 448 static int reftable_ref_record_equal_void(const void *a, 452 - const void *b, int hash_size) 449 + const void *b, uint32_t hash_size) 453 450 { 454 451 struct reftable_ref_record *ra = (struct reftable_ref_record *) a; 455 452 struct reftable_ref_record *rb = (struct reftable_ref_record *) b; ··· 493 490 } 494 491 495 492 static int reftable_obj_record_copy_from(void *rec, const void *src_rec, 496 - int hash_size UNUSED) 493 + uint32_t hash_size UNUSED) 497 494 { 498 495 struct reftable_obj_record *obj = rec; 499 496 const struct reftable_obj_record *src = src_rec; ··· 525 522 } 526 523 527 524 static int reftable_obj_record_encode(const void *rec, struct string_view s, 528 - int hash_size UNUSED) 525 + uint32_t hash_size UNUSED) 529 526 { 530 527 const struct reftable_obj_record *r = rec; 531 528 struct string_view start = s; ··· 560 557 561 558 static int reftable_obj_record_decode(void *rec, struct reftable_buf key, 562 559 uint8_t val_type, struct string_view in, 563 - int hash_size UNUSED, 560 + uint32_t hash_size UNUSED, 564 561 struct reftable_buf *scratch UNUSED) 565 562 { 566 563 struct string_view start = in; ··· 568 565 uint64_t count = val_type; 569 566 int n = 0; 570 567 uint64_t last; 571 - int j; 572 568 573 569 reftable_obj_record_release(r); 574 570 ··· 603 599 string_view_consume(&in, n); 604 600 605 601 last = r->offsets[0]; 606 - j = 1; 607 - while (j < count) { 602 + for (uint64_t j = 1; j < count; j++) { 608 603 uint64_t delta = 0; 609 604 int n = get_var_int(&delta, &in); 610 605 if (n < 0) { ··· 613 608 string_view_consume(&in, n); 614 609 615 610 last = r->offsets[j] = (delta + last); 616 - j++; 617 611 } 618 612 return start.len - in.len; 619 613 } ··· 624 618 } 625 619 626 620 static int reftable_obj_record_equal_void(const void *a, const void *b, 627 - int hash_size UNUSED) 621 + uint32_t hash_size UNUSED) 628 622 { 629 623 struct reftable_obj_record *ra = (struct reftable_obj_record *) a; 630 624 struct reftable_obj_record *rb = (struct reftable_obj_record *) b; ··· 699 693 } 700 694 701 695 static int reftable_log_record_copy_from(void *rec, const void *src_rec, 702 - int hash_size) 696 + uint32_t hash_size) 703 697 { 704 698 struct reftable_log_record *dst = rec; 705 699 const struct reftable_log_record *src = ··· 780 774 } 781 775 782 776 static int reftable_log_record_encode(const void *rec, struct string_view s, 783 - int hash_size) 777 + uint32_t hash_size) 784 778 { 785 779 const struct reftable_log_record *r = rec; 786 780 struct string_view start = s; ··· 828 822 829 823 static int reftable_log_record_decode(void *rec, struct reftable_buf key, 830 824 uint8_t val_type, struct string_view in, 831 - int hash_size, struct reftable_buf *scratch) 825 + uint32_t hash_size, struct reftable_buf *scratch) 832 826 { 833 827 struct string_view start = in; 834 828 struct reftable_log_record *r = rec; ··· 976 970 } 977 971 978 972 static int reftable_log_record_equal_void(const void *a, 979 - const void *b, int hash_size) 973 + const void *b, uint32_t hash_size) 980 974 { 981 975 return reftable_log_record_equal((struct reftable_log_record *) a, 982 976 (struct reftable_log_record *) b, ··· 1000 994 } 1001 995 1002 996 int reftable_log_record_equal(const struct reftable_log_record *a, 1003 - const struct reftable_log_record *b, int hash_size) 997 + const struct reftable_log_record *b, uint32_t hash_size) 1004 998 { 1005 999 if (!(null_streq(a->refname, b->refname) && 1006 1000 a->update_index == b->update_index && ··· 1054 1048 } 1055 1049 1056 1050 static int reftable_index_record_copy_from(void *rec, const void *src_rec, 1057 - int hash_size UNUSED) 1051 + uint32_t hash_size UNUSED) 1058 1052 { 1059 1053 struct reftable_index_record *dst = rec; 1060 1054 const struct reftable_index_record *src = src_rec; ··· 1081 1075 } 1082 1076 1083 1077 static int reftable_index_record_encode(const void *rec, struct string_view out, 1084 - int hash_size UNUSED) 1078 + uint32_t hash_size UNUSED) 1085 1079 { 1086 1080 const struct reftable_index_record *r = 1087 1081 (const struct reftable_index_record *)rec; ··· 1099 1093 static int reftable_index_record_decode(void *rec, struct reftable_buf key, 1100 1094 uint8_t val_type UNUSED, 1101 1095 struct string_view in, 1102 - int hash_size UNUSED, 1096 + uint32_t hash_size UNUSED, 1103 1097 struct reftable_buf *scratch UNUSED) 1104 1098 { 1105 1099 struct string_view start = in; ··· 1120 1114 } 1121 1115 1122 1116 static int reftable_index_record_equal(const void *a, const void *b, 1123 - int hash_size UNUSED) 1117 + uint32_t hash_size UNUSED) 1124 1118 { 1125 1119 struct reftable_index_record *ia = (struct reftable_index_record *) a; 1126 1120 struct reftable_index_record *ib = (struct reftable_index_record *) b; ··· 1154 1148 } 1155 1149 1156 1150 int reftable_record_encode(struct reftable_record *rec, struct string_view dest, 1157 - int hash_size) 1151 + uint32_t hash_size) 1158 1152 { 1159 1153 return reftable_record_vtable(rec)->encode(reftable_record_data(rec), 1160 1154 dest, hash_size); 1161 1155 } 1162 1156 1163 1157 int reftable_record_copy_from(struct reftable_record *rec, 1164 - struct reftable_record *src, int hash_size) 1158 + struct reftable_record *src, uint32_t hash_size) 1165 1159 { 1166 1160 assert(src->type == rec->type); 1167 1161 ··· 1176 1170 } 1177 1171 1178 1172 int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key, 1179 - uint8_t extra, struct string_view src, int hash_size, 1173 + uint8_t extra, struct string_view src, uint32_t hash_size, 1180 1174 struct reftable_buf *scratch) 1181 1175 { 1182 1176 return reftable_record_vtable(rec)->decode(reftable_record_data(rec), ··· 1203 1197 reftable_record_data(a), reftable_record_data(b)); 1204 1198 } 1205 1199 1206 - int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size) 1200 + int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size) 1207 1201 { 1208 1202 if (a->type != b->type) 1209 1203 return 0; ··· 1211 1205 reftable_record_data(a), reftable_record_data(b), hash_size); 1212 1206 } 1213 1207 1214 - static int hash_equal(const unsigned char *a, const unsigned char *b, int hash_size) 1208 + static int hash_equal(const unsigned char *a, const unsigned char *b, uint32_t hash_size) 1215 1209 { 1216 1210 if (a && b) 1217 1211 return !memcmp(a, b, hash_size); ··· 1220 1214 } 1221 1215 1222 1216 int reftable_ref_record_equal(const struct reftable_ref_record *a, 1223 - const struct reftable_ref_record *b, int hash_size) 1217 + const struct reftable_ref_record *b, uint32_t hash_size) 1224 1218 { 1225 - assert(hash_size > 0); 1226 1219 if (!null_streq(a->refname, b->refname)) 1227 1220 return 0; 1228 1221
+12 -13
reftable/record.h
··· 32 32 s->len -= n; 33 33 } 34 34 35 - /* utilities for de/encoding varints */ 36 - 35 + /* 36 + * Decode and encode a varint. Returns the number of bytes read/written, or a 37 + * negative value in case encoding/decoding the varint has failed. 38 + */ 37 39 int get_var_int(uint64_t *dest, struct string_view *in); 38 40 int put_var_int(struct string_view *dest, uint64_t val); 39 41 ··· 45 47 /* The record type of ('r' for ref). */ 46 48 uint8_t type; 47 49 48 - int (*copy_from)(void *dest, const void *src, int hash_size); 50 + int (*copy_from)(void *dest, const void *src, uint32_t hash_size); 49 51 50 52 /* a value of [0..7], indicating record subvariants (eg. ref vs. symref 51 53 * vs ref deletion) */ 52 54 uint8_t (*val_type)(const void *rec); 53 55 54 56 /* encodes rec into dest, returning how much space was used. */ 55 - int (*encode)(const void *rec, struct string_view dest, int hash_size); 57 + int (*encode)(const void *rec, struct string_view dest, uint32_t hash_size); 56 58 57 59 /* decode data from `src` into the record. */ 58 60 int (*decode)(void *rec, struct reftable_buf key, uint8_t extra, 59 - struct string_view src, int hash_size, 61 + struct string_view src, uint32_t hash_size, 60 62 struct reftable_buf *scratch); 61 63 62 64 /* deallocate and null the record. */ ··· 66 68 int (*is_deletion)(const void *rec); 67 69 68 70 /* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */ 69 - int (*equal)(const void *a, const void *b, int hash_size); 71 + int (*equal)(const void *a, const void *b, uint32_t hash_size); 70 72 71 73 /* 72 74 * Compare keys of two records with each other. The records must have 73 75 * the same type. 74 76 */ 75 77 int (*cmp)(const void *a, const void *b); 76 - 77 - /* Print on stdout, for debugging. */ 78 - void (*print)(const void *rec, int hash_size); 79 78 }; 80 79 81 80 /* returns true for recognized block types. Block start with the block type. */ ··· 136 135 137 136 /* see struct record_vtable */ 138 137 int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b); 139 - int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); 138 + int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size); 140 139 int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest); 141 140 int reftable_record_copy_from(struct reftable_record *rec, 142 - struct reftable_record *src, int hash_size); 141 + struct reftable_record *src, uint32_t hash_size); 143 142 uint8_t reftable_record_val_type(struct reftable_record *rec); 144 143 int reftable_record_encode(struct reftable_record *rec, struct string_view dest, 145 - int hash_size); 144 + uint32_t hash_size); 146 145 int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key, 147 146 uint8_t extra, struct string_view src, 148 - int hash_size, struct reftable_buf *scratch); 147 + uint32_t hash_size, struct reftable_buf *scratch); 149 148 int reftable_record_is_deletion(struct reftable_record *rec); 150 149 151 150 static inline uint8_t reftable_record_type(struct reftable_record *rec)
+8 -5
reftable/reftable-blocksource.h
··· 22 22 * so it can return itself into the pool. */ 23 23 struct reftable_block { 24 24 uint8_t *data; 25 - int len; 25 + size_t len; 26 26 struct reftable_block_source source; 27 27 }; 28 28 ··· 31 31 /* returns the size of a block source */ 32 32 uint64_t (*size)(void *source); 33 33 34 - /* reads a segment from the block source. It is an error to read 35 - beyond the end of the block */ 36 - int (*read_block)(void *source, struct reftable_block *dest, 37 - uint64_t off, uint32_t size); 34 + /* 35 + * Reads a segment from the block source. It is an error to read beyond 36 + * the end of the block. 37 + */ 38 + ssize_t (*read_block)(void *source, struct reftable_block *dest, 39 + uint64_t off, uint32_t size); 40 + 38 41 /* mark the block as read; may return the data back to malloc */ 39 42 void (*return_block)(void *source, struct reftable_block *blockp); 40 43
+2 -2
reftable/reftable-record.h
··· 65 65 66 66 /* returns whether two reftable_ref_records are the same. Useful for testing. */ 67 67 int reftable_ref_record_equal(const struct reftable_ref_record *a, 68 - const struct reftable_ref_record *b, int hash_size); 68 + const struct reftable_ref_record *b, uint32_t hash_size); 69 69 70 70 /* reftable_log_record holds a reflog entry */ 71 71 struct reftable_log_record { ··· 105 105 106 106 /* returns whether two records are equal. Useful for testing. */ 107 107 int reftable_log_record_equal(const struct reftable_log_record *a, 108 - const struct reftable_log_record *b, int hash_size); 108 + const struct reftable_log_record *b, uint32_t hash_size); 109 109 110 110 #endif
+1 -1
reftable/reftable-writer.h
··· 84 84 /* total number of entries written */ 85 85 int entries; 86 86 /* total number of key restarts */ 87 - int restarts; 87 + uint32_t restarts; 88 88 /* total number of blocks */ 89 89 int blocks; 90 90 /* total number of index blocks */
+5 -7
reftable/stack.c
··· 220 220 } 221 221 222 222 if (st->readers) { 223 - int i = 0; 224 223 struct reftable_buf filename = REFTABLE_BUF_INIT; 225 - for (i = 0; i < st->readers_len; i++) { 224 + 225 + for (size_t i = 0; i < st->readers_len; i++) { 226 226 const char *name = reader_name(st->readers[i]); 227 227 int try_unlinking = 1; 228 228 ··· 238 238 unlink(filename.buf); 239 239 } 240 240 } 241 + 241 242 reftable_buf_release(&filename); 242 243 st->readers_len = 0; 243 244 REFTABLE_FREE_AND_NULL(st->readers); ··· 568 569 { 569 570 char **names = NULL; 570 571 int err; 571 - int i = 0; 572 572 573 573 /* 574 574 * When we have cached stat information available then we use it to ··· 608 608 if (err < 0) 609 609 return err; 610 610 611 - for (i = 0; i < st->readers_len; i++) { 611 + for (size_t i = 0; i < st->readers_len; i++) { 612 612 if (!names[i]) { 613 613 err = 1; 614 614 goto done; ··· 1767 1767 } 1768 1768 1769 1769 while ((d = readdir(dir))) { 1770 - int i = 0; 1771 1770 int found = 0; 1772 1771 if (!is_table_name(d->d_name)) 1773 1772 continue; 1774 1773 1775 - for (i = 0; !found && i < st->readers_len; i++) { 1774 + for (size_t i = 0; !found && i < st->readers_len; i++) 1776 1775 found = !strcmp(reader_name(st->readers[i]), d->d_name); 1777 - } 1778 1776 if (found) 1779 1777 continue; 1780 1778
-2
reftable/system.h
··· 11 11 12 12 /* This header glues the reftable library to the rest of Git */ 13 13 14 - #define DISABLE_SIGN_COMPARE_WARNINGS 15 - 16 14 #include "git-compat-util.h" 17 15 18 16 /*
+3 -4
reftable/writer.c
··· 577 577 578 578 struct common_prefix_arg { 579 579 struct reftable_buf *last; 580 - int max; 580 + size_t max; 581 581 }; 582 582 583 583 static void update_common(void *void_arg, void *key) ··· 585 585 struct common_prefix_arg *arg = void_arg; 586 586 struct obj_index_tree_node *entry = key; 587 587 if (arg->last) { 588 - int n = common_prefix_size(&entry->hash, arg->last); 589 - if (n > arg->max) { 588 + size_t n = common_prefix_size(&entry->hash, arg->last); 589 + if (n > arg->max) 590 590 arg->max = n; 591 - } 592 591 } 593 592 arg->last = &entry->hash; 594 593 }
+1 -1
t/unit-tests/t-reftable-basics.c
··· 120 120 for (size_t i = 0; i < ARRAY_SIZE(cases); i++) { 121 121 check(!reftable_buf_addstr(&a, cases[i].a)); 122 122 check(!reftable_buf_addstr(&b, cases[i].b)); 123 - check_int(common_prefix_size(&a, &b), ==, cases[i].want); 123 + check_uint(common_prefix_size(&a, &b), ==, cases[i].want); 124 124 reftable_buf_reset(&a); 125 125 reftable_buf_reset(&b); 126 126 }
+1 -1
t/unit-tests/t-reftable-readwrite.c
··· 643 643 check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR); 644 644 reftable_writer_free(w); 645 645 646 - check_int(buf.len, ==, header_size(1) + footer_size(1)); 646 + check_uint(buf.len, ==, header_size(1) + footer_size(1)); 647 647 648 648 block_source_from_buf(&source, &buf); 649 649
+18 -1
t/unit-tests/t-reftable-record.c
··· 58 58 } 59 59 } 60 60 61 + static void t_varint_overflow(void) 62 + { 63 + unsigned char buf[] = { 64 + 0xFF, 0xFF, 0xFF, 0xFF, 65 + 0xFF, 0xFF, 0xFF, 0xFF, 66 + 0xFF, 0x00, 67 + }; 68 + struct string_view view = { 69 + .buf = buf, 70 + .len = sizeof(buf), 71 + }; 72 + uint64_t value; 73 + int err = get_var_int(&value, &view); 74 + check_int(err, ==, -1); 75 + } 76 + 61 77 static void set_hash(uint8_t *h, int j) 62 78 { 63 - for (int i = 0; i < hash_size(REFTABLE_HASH_SHA1); i++) 79 + for (size_t i = 0; i < hash_size(REFTABLE_HASH_SHA1); i++) 64 80 h[i] = (j >> i) & 0xff; 65 81 } 66 82 ··· 544 560 TEST(t_reftable_log_record_roundtrip(), "record operations work on log record"); 545 561 TEST(t_reftable_ref_record_roundtrip(), "record operations work on ref record"); 546 562 TEST(t_varint_roundtrip(), "put_var_int and get_var_int work"); 563 + TEST(t_varint_overflow(), "get_var_int notices an integer overflow"); 547 564 TEST(t_key_roundtrip(), "reftable_encode_key and reftable_decode_key work"); 548 565 TEST(t_reftable_obj_record_roundtrip(), "record operations work on obj record"); 549 566 TEST(t_reftable_index_record_roundtrip(), "record operations work on index record");