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>
Junio C Hamano committed
Jul 25, 2007 at 21:34 UTC
e7a7be8831b159b9a7331b34c3ec6915d4a72190
2 files changed
+11
-15
diff.c
+1
-1
@@ -1695,7 +1695,7 @@ static void prep_temp_blob(struct diff_tempfile *temp,
1695
1696
fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1697
if (fd < 0)
1698
- die("unable to create temp-file");
1698
+ die("unable to create temp-file: %s", strerror(errno));
1699
if (write_in_full(fd, blob, size) != size)
1700
die("unable to write temp-file");
1701
close(fd);
path.c
+10
-14
@@ -71,21 +71,17 @@ char *git_path(const char *fmt, ...)
71
/* git_mkstemp() - create tmp file honoring TMPDIR variable */
72
int git_mkstemp(char *path, size_t len, const char *template)
73
{
74
- char *env, *pch = path;
75
-
76
- if ((env = getenv("TMPDIR")) == NULL) {
77
- strcpy(pch, "/tmp/");
78
- len -= 5;
79
- pch += 5;
80
- } else {
81
- size_t n = snprintf(pch, len, "%s/", env);
82
-
83
- len -= n;
84
- pch += n;
74
+ const char *tmp;
75
+ size_t n;
76
+
77
+ tmp = getenv("TMPDIR");
78
+ if (!tmp)
79
+ tmp = "/tmp";
80
+ n = snprintf(path, len, "%s/%s", tmp, template);
81
+ if (len <= n) {
82
+ errno = ENAMETOOLONG;
83
+ return -1;
84
}
86
-
87
- strlcpy(pch, template, len);
88
-
85
return mkstemp(path);
86
}
87