git_mkstemp(): be careful not to overflow the path buffer.
If user's TMPDIR is insanely long, return negative after setting errno to ENAMETOOLONG, pretending that the underlying mkstemp() choked on a temporary file path that is too long. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
2
diff.c
2
diff.c
@ -1695,7 +1695,7 @@ static void prep_temp_blob(struct diff_tempfile *temp,
|
|||||||
|
|
||||||
fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
|
fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
die("unable to create temp-file");
|
die("unable to create temp-file: %s", strerror(errno));
|
||||||
if (write_in_full(fd, blob, size) != size)
|
if (write_in_full(fd, blob, size) != size)
|
||||||
die("unable to write temp-file");
|
die("unable to write temp-file");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
22
path.c
22
path.c
@ -71,21 +71,17 @@ char *git_path(const char *fmt, ...)
|
|||||||
/* git_mkstemp() - create tmp file honoring TMPDIR variable */
|
/* git_mkstemp() - create tmp file honoring TMPDIR variable */
|
||||||
int git_mkstemp(char *path, size_t len, const char *template)
|
int git_mkstemp(char *path, size_t len, const char *template)
|
||||||
{
|
{
|
||||||
char *env, *pch = path;
|
const char *tmp;
|
||||||
|
size_t n;
|
||||||
|
|
||||||
if ((env = getenv("TMPDIR")) == NULL) {
|
tmp = getenv("TMPDIR");
|
||||||
strcpy(pch, "/tmp/");
|
if (!tmp)
|
||||||
len -= 5;
|
tmp = "/tmp";
|
||||||
pch += 5;
|
n = snprintf(path, len, "%s/%s", tmp, template);
|
||||||
} else {
|
if (len <= n) {
|
||||||
size_t n = snprintf(pch, len, "%s/", env);
|
errno = ENAMETOOLONG;
|
||||||
|
return -1;
|
||||||
len -= n;
|
|
||||||
pch += n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
strlcpy(pch, template, len);
|
|
||||||
|
|
||||||
return mkstemp(path);
|
return mkstemp(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user