Skip to content

Commit 64f8502

Browse files
peffgitster
authored andcommitted
mv: replace src_dir with a strvec
We manually manage the src_dir array with ALLOC_GROW. Using a strvec is a little more ergonomic, and makes the memory ownership more clear. It does mean that we copy the strings (which were otherwise just pointers into the "sources" strvec), but using the same rationale as 9fcd9e4 (builtin/mv duplicate string list memory, 2024-05-27), it's just not enough to be worth worrying about here. As a bonus, this gets rid of some "int"s used for allocation management (though in practice these were limited to command-line sizes and thus not overflowable). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d58a687 commit 64f8502

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

builtin/mv.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
197197
struct strvec submodule_gitfiles_to_free = STRVEC_INIT;
198198
const char **submodule_gitfiles;
199199
char *dst_w_slash = NULL;
200-
const char **src_dir = NULL;
201-
int src_dir_nr = 0, src_dir_alloc = 0;
200+
struct strvec src_dir = STRVEC_INIT;
202201
enum update_mode *modes, dst_mode = 0;
203202
struct stat st, dest_st;
204203
struct string_list src_for_dst = STRING_LIST_INIT_DUP;
@@ -344,8 +343,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
344343
/* last - first >= 1 */
345344
modes[i] |= WORKING_DIRECTORY;
346345

347-
ALLOC_GROW(src_dir, src_dir_nr + 1, src_dir_alloc);
348-
src_dir[src_dir_nr++] = src;
346+
strvec_push(&src_dir, src);
349347

350348
n = argc + last - first;
351349
REALLOC_ARRAY(modes, n);
@@ -559,7 +557,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
559557
}
560558
}
561559

562-
remove_empty_src_dirs(src_dir, src_dir_nr);
560+
remove_empty_src_dirs(src_dir.v, src_dir.nr);
563561

564562
if (dirty_paths.nr)
565563
advise_on_moving_dirty_path(&dirty_paths);
@@ -574,7 +572,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
574572
ret = 0;
575573

576574
out:
577-
free(src_dir);
575+
strvec_clear(&src_dir);
578576
free(dst_w_slash);
579577
string_list_clear(&src_for_dst, 0);
580578
string_list_clear(&dirty_paths, 0);

0 commit comments

Comments
 (0)