Skip to content

Commit cd6a471

Browse files
pks-tgitster
authored andcommitted
reftable/blocksource: handle allocation failures
Handle allocation failures in the blocksource code. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc6a9af commit cd6a471

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

reftable/blocksource.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,
3030
struct strbuf *b = v;
3131
assert(off + size <= b->len);
3232
REFTABLE_CALLOC_ARRAY(dest->data, size);
33+
if (!dest->data)
34+
return -1;
3335
memcpy(dest->data, b->buf + off, size);
3436
dest->len = size;
3537
return size;
@@ -98,27 +100,40 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,
98100
{
99101
struct file_block_source *p;
100102
struct stat st;
101-
int fd;
103+
int fd, err;
102104

103105
fd = open(name, O_RDONLY);
104106
if (fd < 0) {
105107
if (errno == ENOENT)
106108
return REFTABLE_NOT_EXIST_ERROR;
107-
return -1;
109+
err = -1;
110+
goto out;
108111
}
109112

110113
if (fstat(fd, &st) < 0) {
111-
close(fd);
112-
return REFTABLE_IO_ERROR;
114+
err = REFTABLE_IO_ERROR;
115+
goto out;
113116
}
114117

115118
REFTABLE_CALLOC_ARRAY(p, 1);
119+
if (!p) {
120+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
121+
goto out;
122+
}
123+
116124
p->size = st.st_size;
117125
p->data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
118-
close(fd);
119126

120127
assert(!bs->ops);
121128
bs->ops = &file_vtable;
122129
bs->arg = p;
130+
131+
err = 0;
132+
133+
out:
134+
if (fd >= 0)
135+
close(fd);
136+
if (err < 0)
137+
reftable_free(p);
123138
return 0;
124139
}

0 commit comments

Comments
 (0)