diff options
author | Junio C Hamano <gitster@pobox.com> | 2008-11-05 11:35:53 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2008-11-05 11:35:53 -0800 |
commit | a1a846a19e854a3b3baff8f55c039846880a2635 (patch) | |
tree | 0d6aebb3626cbc4204a1ec455e3b2105b4733062 /path.c | |
parent | d95f91d9e48a88a39ac2de5bfddd9d33fd36435d (diff) | |
parent | 98b35e2c749614446b916230148d66857e8a09f3 (diff) | |
download | git-a1a846a19e854a3b3baff8f55c039846880a2635.tar.gz |
Merge branch 'ar/mksnpath'
* ar/mksnpath:
Use git_pathdup instead of xstrdup(git_path(...))
git_pathdup: returns xstrdup-ed copy of the formatted path
Fix potentially dangerous use of git_path in ref.c
Add git_snpath: a .git path formatting routine with output buffer
Fix potentially dangerous uses of mkpath and git_path
Fix potentially dangerous uses of mkpath and git_path
Fix mkpath abuse in dwim_ref and dwim_log of sha1_name.c
Add mksnpath which allows you to specify the output buffer
Conflicts:
builtin-revert.c
Diffstat (limited to 'path.c')
-rw-r--r-- | path.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -32,6 +32,60 @@ static char *cleanup_path(char *path) return path; } +char *mksnpath(char *buf, size_t n, const char *fmt, ...) +{ + va_list args; + unsigned len; + + va_start(args, fmt); + len = vsnprintf(buf, n, fmt, args); + va_end(args); + if (len >= n) { + snprintf(buf, n, bad_path); + return buf; + } + return cleanup_path(buf); +} + +static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args) +{ + const char *git_dir = get_git_dir(); + size_t len; + + len = strlen(git_dir); + if (n < len + 1) + goto bad; + memcpy(buf, git_dir, len); + if (len && !is_dir_sep(git_dir[len-1])) + buf[len++] = '/'; + len += vsnprintf(buf + len, n - len, fmt, args); + if (len >= n) + goto bad; + return cleanup_path(buf); +bad: + snprintf(buf, n, bad_path); + return buf; +} + +char *git_snpath(char *buf, size_t n, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + (void)git_vsnpath(buf, n, fmt, args); + va_end(args); + return buf; +} + +char *git_pathdup(const char *fmt, ...) +{ + char path[PATH_MAX]; + va_list args; + va_start(args, fmt); + (void)git_vsnpath(path, sizeof(path), fmt, args); + va_end(args); + return xstrdup(path); +} + char *mkpath(const char *fmt, ...) { va_list args; |