Skip to content

Commit 2757168

Browse files
inosmeetgitster
authored andcommitted
reftable: propagate specific error codes in block_writer_add()
Previously, functions block_writer_add() and related functions returned -1 when the record did not fit, forcing the caller to assume that any failure meant the entry was too big. Replace these generic -1 returns with defined error codes. This prepares the codebase for finer-grained error handling so that callers can distinguish between a block-full condition and other errors. Signed-off-by: Meet Soni <[email protected]> Acked-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 683c54c commit 2757168

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

reftable/block.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,
4949
if (is_restart)
5050
rlen++;
5151
if (2 + 3 * rlen + n > w->block_size - w->next)
52-
return -1;
52+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
5353
if (is_restart) {
5454
REFTABLE_ALLOC_GROW_OR_NULL(w->restarts, w->restart_len + 1,
5555
w->restart_cap);
@@ -97,9 +97,10 @@ uint8_t block_writer_type(struct block_writer *bw)
9797
return bw->block[bw->header_off];
9898
}
9999

100-
/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on
101-
success. Returns REFTABLE_API_ERROR if attempting to write a record with
102-
empty key. */
100+
/*
101+
* Adds the reftable_record to the block. Returns 0 on success and
102+
* appropriate error codes on failure.
103+
*/
103104
int block_writer_add(struct block_writer *w, struct reftable_record *rec)
104105
{
105106
struct reftable_buf empty = REFTABLE_BUF_INIT;
@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
126127
n = reftable_encode_key(&is_restart, out, last, w->scratch,
127128
reftable_record_val_type(rec));
128129
if (n < 0) {
129-
err = -1;
130+
err = n;
130131
goto done;
131132
}
132133
string_view_consume(&out, n);
133134

134135
n = reftable_record_encode(rec, out, w->hash_size);
135136
if (n < 0) {
136-
err = -1;
137+
err = n;
137138
goto done;
138139
}
139140
string_view_consume(&out, n);

reftable/block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
5353
/* returns the block type (eg. 'r' for ref records. */
5454
uint8_t block_writer_type(struct block_writer *bw);
5555

56-
/* appends the record, or -1 if it doesn't fit. */
56+
/* Attempts to append the record. Returns 0 on success or error code on failure. */
5757
int block_writer_add(struct block_writer *w, struct reftable_record *rec);
5858

5959
/* appends the key restarts, and compress the block if necessary. */

reftable/record.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int put_var_int(struct string_view *dest, uint64_t value)
6161
while (value >>= 7)
6262
varint[--pos] = 0x80 | (--value & 0x7f);
6363
if (dest->len < sizeof(varint) - pos)
64-
return -1;
64+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
6565
memcpy(dest->buf, varint + pos, sizeof(varint) - pos);
6666
return sizeof(varint) - pos;
6767
}
@@ -129,10 +129,10 @@ static int encode_string(const char *str, struct string_view s)
129129
size_t l = strlen(str);
130130
int n = put_var_int(&s, l);
131131
if (n < 0)
132-
return -1;
132+
return n;
133133
string_view_consume(&s, n);
134134
if (s.len < l)
135-
return -1;
135+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
136136
memcpy(s.buf, str, l);
137137
string_view_consume(&s, l);
138138

@@ -148,18 +148,18 @@ int reftable_encode_key(int *restart, struct string_view dest,
148148
uint64_t suffix_len = key.len - prefix_len;
149149
int n = put_var_int(&dest, prefix_len);
150150
if (n < 0)
151-
return -1;
151+
return n;
152152
string_view_consume(&dest, n);
153153

154154
*restart = (prefix_len == 0);
155155

156156
n = put_var_int(&dest, suffix_len << 3 | (uint64_t)extra);
157157
if (n < 0)
158-
return -1;
158+
return n;
159159
string_view_consume(&dest, n);
160160

161161
if (dest.len < suffix_len)
162-
return -1;
162+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
163163
memcpy(dest.buf, key.buf + prefix_len, suffix_len);
164164
string_view_consume(&dest, suffix_len);
165165

@@ -324,30 +324,27 @@ static int reftable_ref_record_encode(const void *rec, struct string_view s,
324324
struct string_view start = s;
325325
int n = put_var_int(&s, r->update_index);
326326
if (n < 0)
327-
return -1;
327+
return n;
328328
string_view_consume(&s, n);
329329

330330
switch (r->value_type) {
331331
case REFTABLE_REF_SYMREF:
332332
n = encode_string(r->value.symref, s);
333-
if (n < 0) {
334-
return -1;
335-
}
333+
if (n < 0)
334+
return n;
336335
string_view_consume(&s, n);
337336
break;
338337
case REFTABLE_REF_VAL2:
339-
if (s.len < 2 * hash_size) {
340-
return -1;
341-
}
338+
if (s.len < 2 * hash_size)
339+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
342340
memcpy(s.buf, r->value.val2.value, hash_size);
343341
string_view_consume(&s, hash_size);
344342
memcpy(s.buf, r->value.val2.target_value, hash_size);
345343
string_view_consume(&s, hash_size);
346344
break;
347345
case REFTABLE_REF_VAL1:
348-
if (s.len < hash_size) {
349-
return -1;
350-
}
346+
if (s.len < hash_size)
347+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
351348
memcpy(s.buf, r->value.val1, hash_size);
352349
string_view_consume(&s, hash_size);
353350
break;
@@ -531,24 +528,22 @@ static int reftable_obj_record_encode(const void *rec, struct string_view s,
531528
uint64_t last = 0;
532529
if (r->offset_len == 0 || r->offset_len >= 8) {
533530
n = put_var_int(&s, r->offset_len);
534-
if (n < 0) {
535-
return -1;
536-
}
531+
if (n < 0)
532+
return n;
537533
string_view_consume(&s, n);
538534
}
539535
if (r->offset_len == 0)
540536
return start.len - s.len;
541537
n = put_var_int(&s, r->offsets[0]);
542538
if (n < 0)
543-
return -1;
539+
return n;
544540
string_view_consume(&s, n);
545541

546542
last = r->offsets[0];
547543
for (i = 1; i < r->offset_len; i++) {
548544
int n = put_var_int(&s, r->offsets[i] - last);
549-
if (n < 0) {
550-
return -1;
551-
}
545+
if (n < 0)
546+
return n;
552547
string_view_consume(&s, n);
553548
last = r->offsets[i];
554549
}
@@ -783,38 +778,38 @@ static int reftable_log_record_encode(const void *rec, struct string_view s,
783778
return 0;
784779

785780
if (s.len < 2 * hash_size)
786-
return -1;
781+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
787782

788783
memcpy(s.buf, r->value.update.old_hash, hash_size);
789784
memcpy(s.buf + hash_size, r->value.update.new_hash, hash_size);
790785
string_view_consume(&s, 2 * hash_size);
791786

792787
n = encode_string(r->value.update.name ? r->value.update.name : "", s);
793788
if (n < 0)
794-
return -1;
789+
return n;
795790
string_view_consume(&s, n);
796791

797792
n = encode_string(r->value.update.email ? r->value.update.email : "",
798793
s);
799794
if (n < 0)
800-
return -1;
795+
return n;
801796
string_view_consume(&s, n);
802797

803798
n = put_var_int(&s, r->value.update.time);
804799
if (n < 0)
805-
return -1;
800+
return n;
806801
string_view_consume(&s, n);
807802

808803
if (s.len < 2)
809-
return -1;
804+
return REFTABLE_ENTRY_TOO_BIG_ERROR;
810805

811806
put_be16(s.buf, r->value.update.tz_offset);
812807
string_view_consume(&s, 2);
813808

814809
n = encode_string(
815810
r->value.update.message ? r->value.update.message : "", s);
816811
if (n < 0)
817-
return -1;
812+
return n;
818813
string_view_consume(&s, n);
819814

820815
return start.len - s.len;

0 commit comments

Comments
 (0)