reftable/record: handle allocation failures on copy
Handle allocation failures when copying records. While at it, convert from `xstrdup()` to `reftable_strdup()`. Adapt callsites to check for error codes. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Oct 2, 2024 at 12:55 UTC
ea194f9c4690de89f9f776517b1c76d706ab0ae8
2 files changed
+63
-27
reftable/record.c
+60
-24
@@ -215,13 +215,14 @@ static void reftable_ref_record_key(const void *r, struct strbuf *dest)
215
strbuf_addstr(dest, rec->refname);
216
}
217
218
-static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
219
- int hash_size)
218
+static int reftable_ref_record_copy_from(void *rec, const void *src_rec,
219
+ int hash_size)
220
{
221
struct reftable_ref_record *ref = rec;
222
const struct reftable_ref_record *src = src_rec;
223
char *refname = NULL;
224
size_t refname_cap = 0;
225
+ int err;
226
227
assert(hash_size > 0);
228
@@ -236,6 +237,11 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
237
238
REFTABLE_ALLOC_GROW(ref->refname, refname_len + 1,
239
ref->refname_cap);
240
+ if (!ref->refname) {
241
+ err = REFTABLE_OUT_OF_MEMORY_ERROR;
242
+ goto out;
243
+ }
244
+
245
memcpy(ref->refname, src->refname, refname_len);
246
ref->refname[refname_len] = 0;
247
}
@@ -254,9 +260,17 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
260
src->value.val2.target_value, hash_size);
261
break;
262
case REFTABLE_REF_SYMREF:
257
- ref->value.symref = xstrdup(src->value.symref);
263
+ ref->value.symref = reftable_strdup(src->value.symref);
264
+ if (!ref->value.symref) {
265
+ err = REFTABLE_OUT_OF_MEMORY_ERROR;
266
+ goto out;
267
+ }
268
break;
269
}
270
+
271
+ err = 0;
272
+out:
273
+ return err;
274
}
275
276
static void reftable_ref_record_release_void(void *rec)
@@ -457,23 +471,28 @@ static void reftable_obj_record_release(void *rec)
471
memset(obj, 0, sizeof(struct reftable_obj_record));
472
}
473
460
-static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
461
- int hash_size UNUSED)
474
+static int reftable_obj_record_copy_from(void *rec, const void *src_rec,
475
+ int hash_size UNUSED)
476
{
477
struct reftable_obj_record *obj = rec;
464
- const struct reftable_obj_record *src =
465
- (const struct reftable_obj_record *)src_rec;
478
+ const struct reftable_obj_record *src = src_rec;
479
480
reftable_obj_record_release(obj);
481
482
REFTABLE_ALLOC_ARRAY(obj->hash_prefix, src->hash_prefix_len);
483
+ if (!obj->hash_prefix)
484
+ return REFTABLE_OUT_OF_MEMORY_ERROR;
485
obj->hash_prefix_len = src->hash_prefix_len;
486
if (src->hash_prefix_len)
487
memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len);
488
489
REFTABLE_ALLOC_ARRAY(obj->offsets, src->offset_len);
490
+ if (!obj->offsets)
491
+ return REFTABLE_OUT_OF_MEMORY_ERROR;
492
obj->offset_len = src->offset_len;
493
COPY_ARRAY(obj->offsets, src->offsets, src->offset_len);
494
+
495
+ return 0;
496
}
497
498
static uint8_t reftable_obj_record_val_type(const void *rec)
@@ -646,33 +665,44 @@ static void reftable_log_record_key(const void *r, struct strbuf *dest)
665
strbuf_add(dest, i64, sizeof(i64));
666
}
667
649
-static void reftable_log_record_copy_from(void *rec, const void *src_rec,
650
- int hash_size)
668
+static int reftable_log_record_copy_from(void *rec, const void *src_rec,
669
+ int hash_size)
670
{
671
struct reftable_log_record *dst = rec;
672
const struct reftable_log_record *src =
673
(const struct reftable_log_record *)src_rec;
674
+ int ret;
675
676
reftable_log_record_release(dst);
677
*dst = *src;
678
+
679
if (dst->refname) {
659
- dst->refname = xstrdup(dst->refname);
680
+ dst->refname = reftable_strdup(dst->refname);
681
+ if (!dst->refname) {
682
+ ret = REFTABLE_OUT_OF_MEMORY_ERROR;
683
+ goto out;
684
+ }
685
}
686
+
687
switch (dst->value_type) {
688
case REFTABLE_LOG_DELETION:
689
break;
690
case REFTABLE_LOG_UPDATE:
665
- if (dst->value.update.email) {
691
+ if (dst->value.update.email)
692
dst->value.update.email =
667
- xstrdup(dst->value.update.email);
668
- }
669
- if (dst->value.update.name) {
693
+ reftable_strdup(dst->value.update.email);
694
+ if (dst->value.update.name)
695
dst->value.update.name =
671
- xstrdup(dst->value.update.name);
672
- }
673
- if (dst->value.update.message) {
696
+ reftable_strdup(dst->value.update.name);
697
+ if (dst->value.update.message)
698
dst->value.update.message =
675
- xstrdup(dst->value.update.message);
699
+ reftable_strdup(dst->value.update.message);
700
+
701
+ if (!dst->value.update.email ||
702
+ !dst->value.update.name ||
703
+ !dst->value.update.message) {
704
+ ret = REFTABLE_OUT_OF_MEMORY_ERROR;
705
+ goto out;
706
}
707
708
memcpy(dst->value.update.new_hash,
@@ -681,6 +711,10 @@ static void reftable_log_record_copy_from(void *rec, const void *src_rec,
711
src->value.update.old_hash, hash_size);
712
break;
713
}
714
+
715
+ ret = 0;
716
+out:
717
+ return ret;
718
}
719
720
static void reftable_log_record_release_void(void *rec)
@@ -954,8 +988,8 @@ static void reftable_index_record_key(const void *r, struct strbuf *dest)
988
strbuf_addbuf(dest, &rec->last_key);
989
}
990
957
-static void reftable_index_record_copy_from(void *rec, const void *src_rec,
958
- int hash_size UNUSED)
991
+static int reftable_index_record_copy_from(void *rec, const void *src_rec,
992
+ int hash_size UNUSED)
993
{
994
struct reftable_index_record *dst = rec;
995
const struct reftable_index_record *src = src_rec;
@@ -963,6 +997,8 @@ static void reftable_index_record_copy_from(void *rec, const void *src_rec,
997
strbuf_reset(&dst->last_key);
998
strbuf_addbuf(&dst->last_key, &src->last_key);
999
dst->offset = src->offset;
1000
+
1001
+ return 0;
1002
}
1003
1004
static void reftable_index_record_release(void *rec)
@@ -1054,14 +1090,14 @@ int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
1090
dest, hash_size);
1091
}
1092
1057
-void reftable_record_copy_from(struct reftable_record *rec,
1093
+int reftable_record_copy_from(struct reftable_record *rec,
1094
struct reftable_record *src, int hash_size)
1095
{
1096
assert(src->type == rec->type);
1097
1062
- reftable_record_vtable(rec)->copy_from(reftable_record_data(rec),
1063
- reftable_record_data(src),
1064
- hash_size);
1098
+ return reftable_record_vtable(rec)->copy_from(reftable_record_data(rec),
1099
+ reftable_record_data(src),
1100
+ hash_size);
1101
}
1102
1103
uint8_t reftable_record_val_type(struct reftable_record *rec)
reftable/record.h
+3
-3
@@ -44,7 +44,7 @@ struct reftable_record_vtable {
44
/* The record type of ('r' for ref). */
45
uint8_t type;
46
47
- void (*copy_from)(void *dest, const void *src, int hash_size);
47
+ int (*copy_from)(void *dest, const void *src, int hash_size);
48
49
/* a value of [0..7], indicating record subvariants (eg. ref vs. symref
50
* vs ref deletion) */
@@ -137,8 +137,8 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ);
137
int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
138
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
139
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
140
-void reftable_record_copy_from(struct reftable_record *rec,
141
- struct reftable_record *src, int hash_size);
140
+int reftable_record_copy_from(struct reftable_record *rec,
141
+ struct reftable_record *src, int hash_size);
142
uint8_t reftable_record_val_type(struct reftable_record *rec);
143
int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
144
int hash_size);