]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/basics: adjust `hash_size()` to return `uint32_t`
authorPatrick Steinhardt <ps@pks.im>
Mon, 20 Jan 2025 16:17:23 +0000 (17:17 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Jan 2025 22:20:29 +0000 (14:20 -0800)
The `hash_size()` function returns the number of bytes used by the hash
function. Weirdly enough though, it returns a signed integer for its
size even though the size obviously cannot ever be negative. The only
case where it could be negative is if the function returned an error
when asked for an unknown hash, but we assert(3p) instead.

Adjust the type of `hash_size()` to be `uint32_t` and adapt all places
that use signed integers for the hash size to follow suit. This also
allows us to get rid of a couple asserts that we had which verified that
the size was indeed positive, which further stresses the point that this
refactoring makes sense.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
reftable/basics.c
reftable/basics.h
reftable/block.c
reftable/block.h
reftable/reader.c
reftable/record.c
reftable/record.h
reftable/reftable-record.h
t/unit-tests/t-reftable-record.c

index 10b234ea55f0162793617286373a8d7e8440802b..3b5ea27bbdc56ed9cc3d475a649a26d9f294e636 100644 (file)
@@ -272,7 +272,7 @@ size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b)
        return p;
 }
 
-int hash_size(enum reftable_hash id)
+uint32_t hash_size(enum reftable_hash id)
 {
        if (!id)
                return REFTABLE_HASH_SIZE_SHA1;
index 9ff81a68f8e1c6188397e3e20e4ea2bfceff8e14..a2a010a0e199ba713982c8da7c469cb7fee6a596 100644 (file)
@@ -171,7 +171,7 @@ static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,
 /* Find the longest shared prefix size of `a` and `b` */
 size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);
 
-int hash_size(enum reftable_hash id);
+uint32_t hash_size(enum reftable_hash id);
 
 /*
  * Format IDs that identify the hash function used by a reftable. Note that
index 9858bbc7c5f7aa63fbcaf1acf9f4b5738e0b6d27..2380aabb2f36a039fe629230309346c190fe5458 100644 (file)
@@ -72,7 +72,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,
 }
 
 int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
-                     uint32_t block_size, uint32_t header_off, int hash_size)
+                     uint32_t block_size, uint32_t header_off, uint32_t hash_size)
 {
        bw->block = block;
        bw->hash_size = hash_size;
@@ -214,7 +214,7 @@ int block_writer_finish(struct block_writer *w)
 
 int block_reader_init(struct block_reader *br, struct reftable_block *block,
                      uint32_t header_off, uint32_t table_block_size,
-                     int hash_size)
+                     uint32_t hash_size)
 {
        uint32_t full_block_size = table_block_size;
        uint8_t typ = block->data[header_off];
index 0431e8591f41dedfb96eef304ea63ef2e9e5f5dd..5f67ed74c5e9a82d286d7fc1c8f24ac1925a47bf 100644 (file)
@@ -30,7 +30,7 @@ struct block_writer {
 
        /* How often to restart keys. */
        uint16_t restart_interval;
-       int hash_size;
+       uint32_t hash_size;
 
        /* Offset of next uint8_t to write. */
        uint32_t next;
@@ -48,7 +48,7 @@ struct block_writer {
  * initializes the blockwriter to write `typ` entries, using `block` as temporary
  * storage. `block` is not owned by the block_writer. */
 int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
-                     uint32_t block_size, uint32_t header_off, int hash_size);
+                     uint32_t block_size, uint32_t header_off, uint32_t hash_size);
 
 /* returns the block type (eg. 'r' for ref records. */
 uint8_t block_writer_type(struct block_writer *bw);
@@ -72,7 +72,7 @@ struct block_reader {
 
        /* the memory block */
        struct reftable_block block;
-       int hash_size;
+       uint32_t hash_size;
 
        /* Uncompressed data for log entries. */
        z_stream *zstream;
@@ -92,7 +92,7 @@ struct block_reader {
 /* initializes a block reader. */
 int block_reader_init(struct block_reader *br, struct reftable_block *bl,
                      uint32_t header_off, uint32_t table_block_size,
-                     int hash_size);
+                     uint32_t hash_size);
 
 void block_reader_release(struct block_reader *br);
 
@@ -108,7 +108,7 @@ struct block_iter {
        uint32_t next_off;
        const unsigned char *block;
        size_t block_len;
-       int hash_size;
+       uint32_t hash_size;
 
        /* key for last entry we read. */
        struct reftable_buf last_key;
index ea82955c9bcf1d797aea497fd76e850f145d88ae..9df8a5ecb1ee5c8fe12c4faa0b4fdde52505da4a 100644 (file)
@@ -750,7 +750,7 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
        struct table_iter *ti;
        struct filtering_ref_iterator *filter = NULL;
        struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT;
-       int oid_len = hash_size(r->hash_id);
+       uint32_t oid_len = hash_size(r->hash_id);
        int err;
 
        REFTABLE_ALLOC_ARRAY(ti, 1);
index 4a3e019528981d6b038cc6ccec1c73290d614cd8..f7766a32efe02ffe5981247dd2f3806108c61965 100644 (file)
@@ -229,7 +229,7 @@ static int reftable_ref_record_key(const void *r, struct reftable_buf *dest)
 }
 
 static int reftable_ref_record_copy_from(void *rec, const void *src_rec,
-                                        int hash_size)
+                                        uint32_t hash_size)
 {
        struct reftable_ref_record *ref = rec;
        const struct reftable_ref_record *src = src_rec;
@@ -237,8 +237,6 @@ static int reftable_ref_record_copy_from(void *rec, const void *src_rec,
        size_t refname_cap = 0;
        int err;
 
-       assert(hash_size > 0);
-
        SWAP(refname, ref->refname);
        SWAP(refname_cap, ref->refname_cap);
        reftable_ref_record_release(ref);
@@ -319,13 +317,12 @@ static uint8_t reftable_ref_record_val_type(const void *rec)
 }
 
 static int reftable_ref_record_encode(const void *rec, struct string_view s,
-                                     int hash_size)
+                                     uint32_t hash_size)
 {
        const struct reftable_ref_record *r =
                (const struct reftable_ref_record *)rec;
        struct string_view start = s;
        int n = put_var_int(&s, r->update_index);
-       assert(hash_size > 0);
        if (n < 0)
                return -1;
        string_view_consume(&s, n);
@@ -365,7 +362,7 @@ static int reftable_ref_record_encode(const void *rec, struct string_view s,
 
 static int reftable_ref_record_decode(void *rec, struct reftable_buf key,
                                      uint8_t val_type, struct string_view in,
-                                     int hash_size, struct reftable_buf *scratch)
+                                     uint32_t hash_size, struct reftable_buf *scratch)
 {
        struct reftable_ref_record *r = rec;
        struct string_view start = in;
@@ -374,8 +371,6 @@ static int reftable_ref_record_decode(void *rec, struct reftable_buf key,
        size_t refname_cap = 0;
        int n, err;
 
-       assert(hash_size > 0);
-
        n = get_var_int(&update_index, &in);
        if (n < 0)
                return n;
@@ -451,7 +446,7 @@ static int reftable_ref_record_is_deletion_void(const void *p)
 }
 
 static int reftable_ref_record_equal_void(const void *a,
-                                         const void *b, int hash_size)
+                                         const void *b, uint32_t hash_size)
 {
        struct reftable_ref_record *ra = (struct reftable_ref_record *) a;
        struct reftable_ref_record *rb = (struct reftable_ref_record *) b;
@@ -495,7 +490,7 @@ static void reftable_obj_record_release(void *rec)
 }
 
 static int reftable_obj_record_copy_from(void *rec, const void *src_rec,
-                                        int hash_size UNUSED)
+                                        uint32_t hash_size UNUSED)
 {
        struct reftable_obj_record *obj = rec;
        const struct reftable_obj_record *src = src_rec;
@@ -527,7 +522,7 @@ static uint8_t reftable_obj_record_val_type(const void *rec)
 }
 
 static int reftable_obj_record_encode(const void *rec, struct string_view s,
-                                     int hash_size UNUSED)
+                                     uint32_t hash_size UNUSED)
 {
        const struct reftable_obj_record *r = rec;
        struct string_view start = s;
@@ -562,7 +557,7 @@ static int reftable_obj_record_encode(const void *rec, struct string_view s,
 
 static int reftable_obj_record_decode(void *rec, struct reftable_buf key,
                                      uint8_t val_type, struct string_view in,
-                                     int hash_size UNUSED,
+                                     uint32_t hash_size UNUSED,
                                      struct reftable_buf *scratch UNUSED)
 {
        struct string_view start = in;
@@ -626,7 +621,7 @@ static int not_a_deletion(const void *p UNUSED)
 }
 
 static int reftable_obj_record_equal_void(const void *a, const void *b,
-                                         int hash_size UNUSED)
+                                         uint32_t hash_size UNUSED)
 {
        struct reftable_obj_record *ra = (struct reftable_obj_record *) a;
        struct reftable_obj_record *rb = (struct reftable_obj_record *) b;
@@ -701,7 +696,7 @@ static int reftable_log_record_key(const void *r, struct reftable_buf *dest)
 }
 
 static int reftable_log_record_copy_from(void *rec, const void *src_rec,
-                                        int hash_size)
+                                        uint32_t hash_size)
 {
        struct reftable_log_record *dst = rec;
        const struct reftable_log_record *src =
@@ -782,7 +777,7 @@ static uint8_t reftable_log_record_val_type(const void *rec)
 }
 
 static int reftable_log_record_encode(const void *rec, struct string_view s,
-                                     int hash_size)
+                                     uint32_t hash_size)
 {
        const struct reftable_log_record *r = rec;
        struct string_view start = s;
@@ -830,7 +825,7 @@ static int reftable_log_record_encode(const void *rec, struct string_view s,
 
 static int reftable_log_record_decode(void *rec, struct reftable_buf key,
                                      uint8_t val_type, struct string_view in,
-                                     int hash_size, struct reftable_buf *scratch)
+                                     uint32_t hash_size, struct reftable_buf *scratch)
 {
        struct string_view start = in;
        struct reftable_log_record *r = rec;
@@ -978,7 +973,7 @@ static int null_streq(const char *a, const char *b)
 }
 
 static int reftable_log_record_equal_void(const void *a,
-                                         const void *b, int hash_size)
+                                         const void *b, uint32_t hash_size)
 {
        return reftable_log_record_equal((struct reftable_log_record *) a,
                                         (struct reftable_log_record *) b,
@@ -1002,7 +997,7 @@ static int reftable_log_record_cmp_void(const void *_a, const void *_b)
 }
 
 int reftable_log_record_equal(const struct reftable_log_record *a,
-                             const struct reftable_log_record *b, int hash_size)
+                             const struct reftable_log_record *b, uint32_t hash_size)
 {
        if (!(null_streq(a->refname, b->refname) &&
              a->update_index == b->update_index &&
@@ -1056,7 +1051,7 @@ static int reftable_index_record_key(const void *r, struct reftable_buf *dest)
 }
 
 static int reftable_index_record_copy_from(void *rec, const void *src_rec,
-                                          int hash_size UNUSED)
+                                          uint32_t hash_size UNUSED)
 {
        struct reftable_index_record *dst = rec;
        const struct reftable_index_record *src = src_rec;
@@ -1083,7 +1078,7 @@ static uint8_t reftable_index_record_val_type(const void *rec UNUSED)
 }
 
 static int reftable_index_record_encode(const void *rec, struct string_view out,
-                                       int hash_size UNUSED)
+                                       uint32_t hash_size UNUSED)
 {
        const struct reftable_index_record *r =
                (const struct reftable_index_record *)rec;
@@ -1101,7 +1096,7 @@ static int reftable_index_record_encode(const void *rec, struct string_view out,
 static int reftable_index_record_decode(void *rec, struct reftable_buf key,
                                        uint8_t val_type UNUSED,
                                        struct string_view in,
-                                       int hash_size UNUSED,
+                                       uint32_t hash_size UNUSED,
                                        struct reftable_buf *scratch UNUSED)
 {
        struct string_view start = in;
@@ -1122,7 +1117,7 @@ static int reftable_index_record_decode(void *rec, struct reftable_buf key,
 }
 
 static int reftable_index_record_equal(const void *a, const void *b,
-                                      int hash_size UNUSED)
+                                      uint32_t hash_size UNUSED)
 {
        struct reftable_index_record *ia = (struct reftable_index_record *) a;
        struct reftable_index_record *ib = (struct reftable_index_record *) b;
@@ -1156,14 +1151,14 @@ int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest)
 }
 
 int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
-                          int hash_size)
+                          uint32_t hash_size)
 {
        return reftable_record_vtable(rec)->encode(reftable_record_data(rec),
                                                   dest, hash_size);
 }
 
 int reftable_record_copy_from(struct reftable_record *rec,
-                              struct reftable_record *src, int hash_size)
+                              struct reftable_record *src, uint32_t hash_size)
 {
        assert(src->type == rec->type);
 
@@ -1178,7 +1173,7 @@ uint8_t reftable_record_val_type(struct reftable_record *rec)
 }
 
 int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key,
-                          uint8_t extra, struct string_view src, int hash_size,
+                          uint8_t extra, struct string_view src, uint32_t hash_size,
                           struct reftable_buf *scratch)
 {
        return reftable_record_vtable(rec)->decode(reftable_record_data(rec),
@@ -1205,7 +1200,7 @@ int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b)
                reftable_record_data(a), reftable_record_data(b));
 }
 
-int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size)
+int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size)
 {
        if (a->type != b->type)
                return 0;
@@ -1213,7 +1208,7 @@ int reftable_record_equal(struct reftable_record *a, struct reftable_record *b,
                reftable_record_data(a), reftable_record_data(b), hash_size);
 }
 
-static int hash_equal(const unsigned char *a, const unsigned char *b, int hash_size)
+static int hash_equal(const unsigned char *a, const unsigned char *b, uint32_t hash_size)
 {
        if (a && b)
                return !memcmp(a, b, hash_size);
@@ -1222,9 +1217,8 @@ static int hash_equal(const unsigned char *a, const unsigned char *b, int hash_s
 }
 
 int reftable_ref_record_equal(const struct reftable_ref_record *a,
-                             const struct reftable_ref_record *b, int hash_size)
+                             const struct reftable_ref_record *b, uint32_t hash_size)
 {
-       assert(hash_size > 0);
        if (!null_streq(a->refname, b->refname))
                return 0;
 
index 0df950f401d5f43b2ab8e6989177e43409046ec7..c7755a4d750339accf670c0a976c490a45d1ff14 100644 (file)
@@ -47,18 +47,18 @@ struct reftable_record_vtable {
        /* The record type of ('r' for ref). */
        uint8_t type;
 
-       int (*copy_from)(void *dest, const void *src, int hash_size);
+       int (*copy_from)(void *dest, const void *src, uint32_t hash_size);
 
        /* a value of [0..7], indicating record subvariants (eg. ref vs. symref
         * vs ref deletion) */
        uint8_t (*val_type)(const void *rec);
 
        /* encodes rec into dest, returning how much space was used. */
-       int (*encode)(const void *rec, struct string_view dest, int hash_size);
+       int (*encode)(const void *rec, struct string_view dest, uint32_t hash_size);
 
        /* decode data from `src` into the record. */
        int (*decode)(void *rec, struct reftable_buf key, uint8_t extra,
-                     struct string_view src, int hash_size,
+                     struct string_view src, uint32_t hash_size,
                      struct reftable_buf *scratch);
 
        /* deallocate and null the record. */
@@ -68,7 +68,7 @@ struct reftable_record_vtable {
        int (*is_deletion)(const void *rec);
 
        /* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */
-       int (*equal)(const void *a, const void *b, int hash_size);
+       int (*equal)(const void *a, const void *b, uint32_t hash_size);
 
        /*
         * Compare keys of two records with each other. The records must have
@@ -135,16 +135,16 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ);
 
 /* see struct record_vtable */
 int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
-int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
+int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size);
 int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest);
 int reftable_record_copy_from(struct reftable_record *rec,
-                             struct reftable_record *src, int hash_size);
+                             struct reftable_record *src, uint32_t hash_size);
 uint8_t reftable_record_val_type(struct reftable_record *rec);
 int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
-                          int hash_size);
+                          uint32_t hash_size);
 int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key,
                           uint8_t extra, struct string_view src,
-                          int hash_size, struct reftable_buf *scratch);
+                          uint32_t hash_size, struct reftable_buf *scratch);
 int reftable_record_is_deletion(struct reftable_record *rec);
 
 static inline uint8_t reftable_record_type(struct reftable_record *rec)
index ddd48eb5798577b745ccaed16ecf08f711c75498..931e59474416dd7806578c9f85ca22e40a01615b 100644 (file)
@@ -65,7 +65,7 @@ void reftable_ref_record_release(struct reftable_ref_record *ref);
 
 /* returns whether two reftable_ref_records are the same. Useful for testing. */
 int reftable_ref_record_equal(const struct reftable_ref_record *a,
-                             const struct reftable_ref_record *b, int hash_size);
+                             const struct reftable_ref_record *b, uint32_t hash_size);
 
 /* reftable_log_record holds a reflog entry */
 struct reftable_log_record {
@@ -105,6 +105,6 @@ void reftable_log_record_release(struct reftable_log_record *log);
 
 /* returns whether two records are equal. Useful for testing. */
 int reftable_log_record_equal(const struct reftable_log_record *a,
-                             const struct reftable_log_record *b, int hash_size);
+                             const struct reftable_log_record *b, uint32_t hash_size);
 
 #endif
index 6d912b9c8f0754be9ebf149b42d232223c90774f..d49d2a2729cb1769601367961d0759dc928172d1 100644 (file)
@@ -76,7 +76,7 @@ static void t_varint_overflow(void)
 
 static void set_hash(uint8_t *h, int j)
 {
-       for (int i = 0; i < hash_size(REFTABLE_HASH_SHA1); i++)
+       for (size_t i = 0; i < hash_size(REFTABLE_HASH_SHA1); i++)
                h[i] = (j >> i) & 0xff;
 }