Skip to content

Commit b4600fb

Browse files
peffgitster
authored andcommitted
merge-recursive: convert malloc / strcpy to strbuf
This would be a fairly routine use of xstrfmt, except that we need to remember the length of the result to pass to cache_name_pos. So just use a strbuf, which makes this simple. As a bonus, this gets rid of confusing references to "pathlen+1". The "1" is for the trailing slash we added, but that is automatically accounted for in the strbuf's len parameter. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd22d4f commit b4600fb

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

merge-recursive.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,25 +630,24 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
630630

631631
static int dir_in_way(const char *path, int check_working_copy)
632632
{
633-
int pos, pathlen = strlen(path);
634-
char *dirpath = xmalloc(pathlen + 2);
633+
int pos;
634+
struct strbuf dirpath = STRBUF_INIT;
635635
struct stat st;
636636

637-
strcpy(dirpath, path);
638-
dirpath[pathlen] = '/';
639-
dirpath[pathlen+1] = '\0';
637+
strbuf_addstr(&dirpath, path);
638+
strbuf_addch(&dirpath, '/');
640639

641-
pos = cache_name_pos(dirpath, pathlen+1);
640+
pos = cache_name_pos(dirpath.buf, dirpath.len);
642641

643642
if (pos < 0)
644643
pos = -1 - pos;
645644
if (pos < active_nr &&
646-
!strncmp(dirpath, active_cache[pos]->name, pathlen+1)) {
647-
free(dirpath);
645+
!strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
646+
strbuf_release(&dirpath);
648647
return 1;
649648
}
650649

651-
free(dirpath);
650+
strbuf_release(&dirpath);
652651
return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
653652
}
654653

0 commit comments

Comments
 (0)