Skip to content

Commit

Permalink
Merge branch 'ar/maint-mksnpath' into HEAD
Browse files Browse the repository at this point in the history
* ar/maint-mksnpath:
  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
  • Loading branch information
Junio C Hamano committed Oct 27, 2008
2 parents 304d058 + 9fa03c1 commit 356af64
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2841,8 +2841,8 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
unsigned int nr = getpid();

for (;;) {
const char *newpath;
newpath = mkpath("%s~%u", path, nr);
char newpath[PATH_MAX];
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
if (!try_create_file(newpath, mode, buf, size)) {
if (!rename(newpath, path))
return;
Expand Down
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ extern int check_repository_format(void);
#define DATA_CHANGED 0x0020
#define TYPE_CHANGED 0x0040

extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
__attribute__((format (printf, 3, 4)));

/* Return a statically allocated filename matching the sha1 signature */
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
Expand Down
15 changes: 15 additions & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ 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);
}

char *mkpath(const char *fmt, ...)
{
va_list args;
Expand Down
6 changes: 4 additions & 2 deletions sha1_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)

*ref = NULL;
for (p = ref_rev_parse_rules; *p; p++) {
char fullref[PATH_MAX];
unsigned char sha1_from_ref[20];
unsigned char *this_result;

this_result = refs_found ? sha1_from_ref : sha1;
r = resolve_ref(mkpath(*p, len, str), this_result, 1, NULL);
mksnpath(fullref, sizeof(fullref), *p, len, str);
r = resolve_ref(fullref, this_result, 1, NULL);
if (r) {
if (!refs_found++)
*ref = xstrdup(r);
Expand All @@ -272,7 +274,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
char path[PATH_MAX];
const char *ref, *it;

strcpy(path, mkpath(*p, len, str));
mksnpath(path, sizeof(path), *p, len, str);
ref = resolve_ref(path, hash, 1, NULL);
if (!ref)
continue;
Expand Down

0 comments on commit 356af64

Please sign in to comment.