@@ -31,13 +31,16 @@ static void reftable_addition_close(struct reftable_addition *add);
31
31
static int reftable_stack_reload_maybe_reuse (struct reftable_stack * st ,
32
32
int reuse_open );
33
33
34
- static void stack_filename (struct reftable_buf * dest , struct reftable_stack * st ,
35
- const char * name )
34
+ static int stack_filename (struct reftable_buf * dest , struct reftable_stack * st ,
35
+ const char * name )
36
36
{
37
+ int err ;
37
38
reftable_buf_reset (dest );
38
- reftable_buf_addstr (dest , st -> reftable_dir );
39
- reftable_buf_addstr (dest , "/" );
40
- reftable_buf_addstr (dest , name );
39
+ if ((err = reftable_buf_addstr (dest , st -> reftable_dir )) < 0 ||
40
+ (err = reftable_buf_addstr (dest , "/" )) < 0 ||
41
+ (err = reftable_buf_addstr (dest , name )) < 0 )
42
+ return err ;
43
+ return 0 ;
41
44
}
42
45
43
46
static ssize_t reftable_fd_write (void * arg , const void * data , size_t sz )
@@ -211,13 +214,16 @@ void reftable_stack_destroy(struct reftable_stack *st)
211
214
struct reftable_buf filename = REFTABLE_BUF_INIT ;
212
215
for (i = 0 ; i < st -> readers_len ; i ++ ) {
213
216
const char * name = reader_name (st -> readers [i ]);
217
+ int try_unlinking = 1 ;
218
+
214
219
reftable_buf_reset (& filename );
215
220
if (names && !has_name (names , name )) {
216
- stack_filename (& filename , st , name );
221
+ if (stack_filename (& filename , st , name ) < 0 )
222
+ try_unlinking = 0 ;
217
223
}
218
224
reftable_reader_decref (st -> readers [i ]);
219
225
220
- if (filename .len ) {
226
+ if (try_unlinking && filename .len ) {
221
227
/* On Windows, can only unlink after closing. */
222
228
unlink (filename .buf );
223
229
}
@@ -310,7 +316,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
310
316
311
317
if (!rd ) {
312
318
struct reftable_block_source src = { NULL };
313
- stack_filename (& table_path , st , name );
319
+
320
+ err = stack_filename (& table_path , st , name );
321
+ if (err < 0 )
322
+ goto done ;
314
323
315
324
err = reftable_block_source_from_file (& src ,
316
325
table_path .buf );
@@ -341,7 +350,11 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
341
350
for (i = 0 ; i < cur_len ; i ++ ) {
342
351
if (cur [i ]) {
343
352
const char * name = reader_name (cur [i ]);
344
- stack_filename (& table_path , st , name );
353
+
354
+ err = stack_filename (& table_path , st , name );
355
+ if (err < 0 )
356
+ goto done ;
357
+
345
358
reftable_reader_decref (cur [i ]);
346
359
unlink (table_path .buf );
347
360
}
@@ -700,8 +713,8 @@ static void reftable_addition_close(struct reftable_addition *add)
700
713
size_t i ;
701
714
702
715
for (i = 0 ; i < add -> new_tables_len ; i ++ ) {
703
- stack_filename (& nm , add -> stack , add -> new_tables [i ]);
704
- unlink (nm .buf );
716
+ if (! stack_filename (& nm , add -> stack , add -> new_tables [i ]))
717
+ unlink (nm .buf );
705
718
reftable_free (add -> new_tables [i ]);
706
719
add -> new_tables [i ] = NULL ;
707
720
}
@@ -851,7 +864,9 @@ int reftable_addition_add(struct reftable_addition *add,
851
864
if (err < 0 )
852
865
goto done ;
853
866
854
- stack_filename (& temp_tab_file_name , add -> stack , next_name .buf );
867
+ err = stack_filename (& temp_tab_file_name , add -> stack , next_name .buf );
868
+ if (err < 0 )
869
+ goto done ;
855
870
reftable_buf_addstr (& temp_tab_file_name , ".temp.XXXXXX" );
856
871
857
872
tab_file = mks_tempfile (temp_tab_file_name .buf );
@@ -900,7 +915,10 @@ int reftable_addition_add(struct reftable_addition *add,
900
915
if (err < 0 )
901
916
goto done ;
902
917
reftable_buf_addstr (& next_name , ".ref" );
903
- stack_filename (& tab_file_name , add -> stack , next_name .buf );
918
+
919
+ err = stack_filename (& tab_file_name , add -> stack , next_name .buf );
920
+ if (err < 0 )
921
+ goto done ;
904
922
905
923
/*
906
924
On windows, this relies on rand() picking a unique destination name.
@@ -954,7 +972,9 @@ static int stack_compact_locked(struct reftable_stack *st,
954
972
if (err < 0 )
955
973
goto done ;
956
974
957
- stack_filename (& tab_file_path , st , next_name .buf );
975
+ err = stack_filename (& tab_file_path , st , next_name .buf );
976
+ if (err < 0 )
977
+ goto done ;
958
978
reftable_buf_addstr (& tab_file_path , ".temp.XXXXXX" );
959
979
960
980
tab_file = mks_tempfile (tab_file_path .buf );
@@ -1174,7 +1194,9 @@ static int stack_compact_range(struct reftable_stack *st,
1174
1194
}
1175
1195
1176
1196
for (i = last + 1 ; i > first ; i -- ) {
1177
- stack_filename (& table_name , st , reader_name (st -> readers [i - 1 ]));
1197
+ err = stack_filename (& table_name , st , reader_name (st -> readers [i - 1 ]));
1198
+ if (err < 0 )
1199
+ goto done ;
1178
1200
1179
1201
err = hold_lock_file_for_update (& table_locks [nlocks ],
1180
1202
table_name .buf , LOCK_NO_DEREF );
@@ -1383,7 +1405,10 @@ static int stack_compact_range(struct reftable_stack *st,
1383
1405
goto done ;
1384
1406
1385
1407
reftable_buf_addstr (& new_table_name , ".ref" );
1386
- stack_filename (& new_table_path , st , new_table_name .buf );
1408
+
1409
+ err = stack_filename (& new_table_path , st , new_table_name .buf );
1410
+ if (err < 0 )
1411
+ goto done ;
1387
1412
1388
1413
err = rename_tempfile (& new_table , new_table_path .buf );
1389
1414
if (err < 0 ) {
@@ -1677,7 +1702,10 @@ static void remove_maybe_stale_table(struct reftable_stack *st, uint64_t max,
1677
1702
struct reftable_block_source src = { NULL };
1678
1703
struct reftable_reader * rd = NULL ;
1679
1704
struct reftable_buf table_path = REFTABLE_BUF_INIT ;
1680
- stack_filename (& table_path , st , name );
1705
+
1706
+ err = stack_filename (& table_path , st , name );
1707
+ if (err < 0 )
1708
+ goto done ;
1681
1709
1682
1710
err = reftable_block_source_from_file (& src , table_path .buf );
1683
1711
if (err < 0 )
0 commit comments