提交 cc96fd09 编写于 作者: J Junio C Hamano

git-apply: Do not free the wrong buffer when we convert the data for writeout

When we write out the result of patch application, we sometimes
need to munge the data (e.g. under core.autocrlf).  After doing
so, what we should free is the temporary buffer that holds the
converted data returned from convert_to_working_tree(), not the
original one.

This patch also moves the call to open() up in the function, as
the caller expects us to fail cheaply if leading directories
need to be created (and then the caller creates them and calls
us again).  For that calling pattern, attempting conversion
before opening the file adds unnecessary overhead.
Acked-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NJunio C Hamano <junkio@cox.net>
上级 00cec846
...@@ -2355,7 +2355,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned ...@@ -2355,7 +2355,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
{ {
int fd; int fd, converted;
char *nbuf; char *nbuf;
unsigned long nsize; unsigned long nsize;
...@@ -2364,17 +2364,18 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf, ...@@ -2364,17 +2364,18 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
* terminated. * terminated.
*/ */
return symlink(buf, path); return symlink(buf, path);
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
if (fd < 0)
return -1;
nsize = size; nsize = size;
nbuf = (char *) buf; nbuf = (char *) buf;
if (convert_to_working_tree(path, &nbuf, &nsize)) { converted = convert_to_working_tree(path, &nbuf, &nsize);
free((char *) buf); if (converted) {
buf = nbuf; buf = nbuf;
size = nsize; size = nsize;
} }
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
if (fd < 0)
return -1;
while (size) { while (size) {
int written = xwrite(fd, buf, size); int written = xwrite(fd, buf, size);
if (written < 0) if (written < 0)
...@@ -2386,6 +2387,8 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf, ...@@ -2386,6 +2387,8 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
} }
if (close(fd) < 0) if (close(fd) < 0)
die("closing file %s: %s", path, strerror(errno)); die("closing file %s: %s", path, strerror(errno));
if (converted)
free(nbuf);
return 0; return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册