Skip to content

Commit d33e487

Browse files
committed
difftool: fix use-after-free
The left and right base directories were pointed to the buf field of two strbufs, which were subject to change. Let's just copy the strings and be done with it. This fixes #1124 Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4c8c909 commit d33e487

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

builtin/difftool.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
262262
struct strbuf rpath = STRBUF_INIT, buf = STRBUF_INIT;
263263
struct strbuf ldir = STRBUF_INIT, rdir = STRBUF_INIT;
264264
struct strbuf wtdir = STRBUF_INIT;
265+
char *lbase_dir, *rbase_dir;
265266
size_t ldir_len, rdir_len, wtdir_len;
266267
struct cache_entry *ce = xcalloc(1, sizeof(ce) + PATH_MAX + 1);
267268
const char *workdir, *tmp;
@@ -298,11 +299,11 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
298299
memset(&wtindex, 0, sizeof(wtindex));
299300

300301
memset(&lstate, 0, sizeof(lstate));
301-
lstate.base_dir = ldir.buf;
302+
lstate.base_dir = lbase_dir = xstrdup(ldir.buf);
302303
lstate.base_dir_len = ldir.len;
303304
lstate.force = 1;
304305
memset(&rstate, 0, sizeof(rstate));
305-
rstate.base_dir = rdir.buf;
306+
rstate.base_dir = rbase_dir = xstrdup(rdir.buf);
306307
rstate.base_dir_len = rdir.len;
307308
rstate.force = 1;
308309

@@ -585,6 +586,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
585586

586587
finish:
587588
free(ce);
589+
free(lbase_dir);
590+
free(rbase_dir);
588591
strbuf_release(&ldir);
589592
strbuf_release(&rdir);
590593
strbuf_release(&wtdir);

0 commit comments

Comments
 (0)