Merge branch 'ar/maint-mksnpath' into HEAD

* 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
This commit is contained in:
Junio C Hamano 2008-10-26 22:24:44 -07:00
commit 356af64d84
4 changed files with 24 additions and 4 deletions

View File

@ -2841,8 +2841,8 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
unsigned int nr = getpid(); unsigned int nr = getpid();
for (;;) { for (;;) {
const char *newpath; char newpath[PATH_MAX];
newpath = mkpath("%s~%u", path, nr); mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
if (!try_create_file(newpath, mode, buf, size)) { if (!try_create_file(newpath, mode, buf, size)) {
if (!rename(newpath, path)) if (!rename(newpath, path))
return; return;

View File

@ -495,6 +495,9 @@ extern int check_repository_format(void);
#define DATA_CHANGED 0x0020 #define DATA_CHANGED 0x0020
#define TYPE_CHANGED 0x0040 #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 */ /* Return a statically allocated filename matching the sha1 signature */
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2))); extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2))); extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));

15
path.c
View File

@ -32,6 +32,21 @@ static char *cleanup_path(char *path)
return 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, ...) char *mkpath(const char *fmt, ...)
{ {
va_list args; va_list args;

View File

@ -245,11 +245,13 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
*ref = NULL; *ref = NULL;
for (p = ref_rev_parse_rules; *p; p++) { for (p = ref_rev_parse_rules; *p; p++) {
char fullref[PATH_MAX];
unsigned char sha1_from_ref[20]; unsigned char sha1_from_ref[20];
unsigned char *this_result; unsigned char *this_result;
this_result = refs_found ? sha1_from_ref : sha1; 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 (r) {
if (!refs_found++) if (!refs_found++)
*ref = xstrdup(r); *ref = xstrdup(r);
@ -272,7 +274,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
char path[PATH_MAX]; char path[PATH_MAX];
const char *ref, *it; const char *ref, *it;
strcpy(path, mkpath(*p, len, str)); mksnpath(path, sizeof(path), *p, len, str);
ref = resolve_ref(path, hash, 1, NULL); ref = resolve_ref(path, hash, 1, NULL);
if (!ref) if (!ref)
continue; continue;