aboutsummaryrefslogtreecommitdiff
path: root/path.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2007-07-25 21:34:53 -0700
committerJunio C Hamano <gitster@pobox.com>2007-07-25 21:34:53 -0700
commite7a7be8831b159b9a7331b34c3ec6915d4a72190 (patch)
tree18f6cb3d3977ddc13a2e196eb1357885c6aa2079 /path.c
parentd58e8d34b019d435b424811c6f972910dfac6f55 (diff)
downloadgit-e7a7be8831b159b9a7331b34c3ec6915d4a72190.tar.gz
git-e7a7be8831b159b9a7331b34c3ec6915d4a72190.tar.xz
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>
Diffstat (limited to 'path.c')
-rw-r--r--path.c24
1 files changed, 10 insertions, 14 deletions
diff --git a/path.c b/path.c
index c4ce96236..dfff41f62 100644
--- a/path.c
+++ b/path.c
@@ -71,21 +71,17 @@ char *git_path(const char *fmt, ...)
/* git_mkstemp() - create tmp file honoring TMPDIR variable */
int git_mkstemp(char *path, size_t len, const char *template)
{
- char *env, *pch = path;
-
- if ((env = getenv("TMPDIR")) == NULL) {
- strcpy(pch, "/tmp/");
- len -= 5;
- pch += 5;
- } else {
- size_t n = snprintf(pch, len, "%s/", env);
-
- len -= n;
- pch += n;
+ const char *tmp;
+ size_t n;
+
+ tmp = getenv("TMPDIR");
+ if (!tmp)
+ tmp = "/tmp";
+ n = snprintf(path, len, "%s/%s", tmp, template);
+ if (len <= n) {
+ errno = ENAMETOOLONG;
+ return -1;
}
-
- strlcpy(pch, template, len);
-
return mkstemp(path);
}