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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2015 at 17:07 UTC b4600fbe071898068ef30be2766ee75880ec308b
1 file changed +8 -9
merge-recursive.c
+8 -9
@@ -630,25 +630,24 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
630
631 static int dir_in_way(const char *path, int check_working_copy)
632 {
633 - int pos, pathlen = strlen(path);
634 - char *dirpath = xmalloc(pathlen + 2);
633 + int pos;
634 + struct strbuf dirpath = STRBUF_INIT;
635 struct stat st;
636
637 - strcpy(dirpath, path);
638 - dirpath[pathlen] = '/';
639 - dirpath[pathlen+1] = '\0';
637 + strbuf_addstr(&dirpath, path);
638 + strbuf_addch(&dirpath, '/');
639
641 - pos = cache_name_pos(dirpath, pathlen+1);
640 + pos = cache_name_pos(dirpath.buf, dirpath.len);
641
642 if (pos < 0)
643 pos = -1 - pos;
644 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);
647 return 1;
648 }
649
651 - free(dirpath);
650 + strbuf_release(&dirpath);
651 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
652 }
653