@matheusbsbs / matheus-bagliano / commits / 2c71810b90
Fork 0

git-apply: applying a patch to make a symlink shorter.

The internal representation of the result is counted string (i.e. char *buf and ulong size), which is fine for writing out to regular file, but throwing the buf at symlink(2) was a no-no. Reported by Willy Tarreau. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Aug 9, 2006 at 22:47 UTC 2c71810b90d122abdcc57fc3cb62174d16e77b58
2 files changed +60
builtin-apply.c
+11
@@ -1698,6 +1698,14 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1698 desc.buffer = buf;
1699 if (apply_fragments(&desc, patch) < 0)
1700 return -1;
1701 +
1702 + /* NUL terminate the result */
1703 + if (desc.alloc <= desc.size) {
1704 + desc.buffer = xrealloc(desc.buffer, desc.size + 1);
1705 + desc.alloc++;
1706 + }
1707 + desc.buffer[desc.size] = 0;
1708 +
1709 patch->result = desc.buffer;
1710 patch->resultsize = desc.size;
1711
@@ -2040,6 +2048,9 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2048 int fd;
2049
2050 if (S_ISLNK(mode))
2051 + /* Although buf:size is counted string, it also is NUL
2052 + * terminated.
2053 + */
2054 return symlink(buf, path);
2055 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
2056 if (fd < 0)
t/t4115-apply-symlink.sh new
+49
@@ -0,0 +1,49 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2005 Junio C Hamano
4 +#
5 +
6 +test_description='git-apply symlinks and partial files
7 +
8 +'
9 +
10 +. ./test-lib.sh
11 +
12 +test_expect_success setup '
13 +
14 + ln -s path1/path2/path3/path4/path5 link1 &&
15 + git add link? &&
16 + git commit -m initial &&
17 +
18 + git branch side &&
19 +
20 + rm -f link? &&
21 +
22 + ln -s htap6 link1 &&
23 + git update-index link? &&
24 + git commit -m second &&
25 +
26 + git diff-tree -p HEAD^ HEAD >patch &&
27 + git apply --stat --summary patch
28 +
29 +'
30 +
31 +test_expect_success 'apply symlink patch' '
32 +
33 + git checkout side &&
34 + git apply patch &&
35 + git diff-files -p >patched &&
36 + diff -u patch patched
37 +
38 +'
39 +
40 +test_expect_success 'apply --index symlink patch' '
41 +
42 + git checkout -f side &&
43 + git apply --index patch &&
44 + git diff-index --cached -p HEAD >patched &&
45 + diff -u patch patched
46 +
47 +'
48 +
49 +test_done