reftable/stack: handle allocation failures in `stack_compact_range()`

Handle allocation failures in `stack_compact_range()`. 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:56 UTC 694af039f514eeca632903e000acbb21ff27a53c
1 file changed +17 -2
reftable/stack.c
+17 -2
@@ -1131,6 +1131,11 @@ static int stack_compact_range(struct reftable_stack *st,
1131 * from the point of view of the newer process.
1132 */
1133 REFTABLE_CALLOC_ARRAY(table_locks, last - first + 1);
1134 + if (!table_locks) {
1135 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
1136 + goto done;
1137 + }
1138 +
1139 for (i = last + 1; i > first; i--) {
1140 stack_filename(&table_name, st, reader_name(st->readers[i - 1]));
1141
@@ -1312,8 +1317,18 @@ static int stack_compact_range(struct reftable_stack *st,
1317 * thus have to allocate `readers_len + 1` many entries.
1318 */
1319 REFTABLE_CALLOC_ARRAY(names, st->merged->readers_len + 1);
1315 - for (size_t i = 0; i < st->merged->readers_len; i++)
1316 - names[i] = xstrdup(st->readers[i]->name);
1320 + if (!names) {
1321 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
1322 + goto done;
1323 + }
1324 +
1325 + for (size_t i = 0; i < st->merged->readers_len; i++) {
1326 + names[i] = reftable_strdup(st->readers[i]->name);
1327 + if (!names[i]) {
1328 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
1329 + goto done;
1330 + }
1331 + }
1332 first_to_replace = first;
1333 last_to_replace = last;
1334 }