Skip to content

Commit a1a846a

Browse files
committed
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
2 parents d95f91d + 98b35e2 commit a1a846a

14 files changed

Lines changed: 88 additions & 21 deletions

builtin-apply.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,8 +2841,8 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
28412841
unsigned int nr = getpid();
28422842

28432843
for (;;) {
2844-
const char *newpath;
2845-
newpath = mkpath("%s~%u", path, nr);
2844+
char newpath[PATH_MAX];
2845+
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
28462846
if (!try_create_file(newpath, mode, buf, size)) {
28472847
if (!rename(newpath, path))
28482848
return;

builtin-config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static int get_value(const char* key_, const char* regex_)
8484
local = config_exclusive_filename;
8585
if (!local) {
8686
const char *home = getenv("HOME");
87-
local = repo_config = xstrdup(git_path("config"));
87+
local = repo_config = git_pathdup("config");
8888
if (git_config_global() && home)
8989
global = xstrdup(mkpath("%s/.gitconfig", home));
9090
if (git_config_system())

builtin-for-each-ref.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,16 @@ static char *get_short_ref(struct refinfo *ref)
620620
for (j = 0; j < i; j++) {
621621
const char *rule = ref_rev_parse_rules[j];
622622
unsigned char short_objectname[20];
623+
char refname[PATH_MAX];
623624

624625
/*
625626
* the short name is ambiguous, if it resolves
626627
* (with this previous rule) to a valid ref
627628
* read_ref() returns 0 on success
628629
*/
629-
if (!read_ref(mkpath(rule, short_name_len, short_name),
630-
short_objectname))
630+
mksnpath(refname, sizeof(refname),
631+
rule, short_name_len, short_name);
632+
if (!read_ref(refname, short_objectname))
631633
break;
632634
}
633635

builtin-reflog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
277277
lock = lock_any_ref_for_update(ref, sha1, 0);
278278
if (!lock)
279279
return error("cannot lock ref '%s'", ref);
280-
log_file = xstrdup(git_path("logs/%s", ref));
280+
log_file = git_pathdup("logs/%s", ref);
281281
if (!file_exists(log_file))
282282
goto finish;
283283
if (!cmd->dry_run) {
284-
newlog_path = xstrdup(git_path("logs/%s.lock", ref));
284+
newlog_path = git_pathdup("logs/%s.lock", ref);
285285
cb.newlog = fopen(newlog_path, "w");
286286
}
287287

builtin-revert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
251251
int i, index_fd, clean;
252252
char *oneline, *reencoded_message = NULL;
253253
const char *message, *encoding;
254-
char *defmsg = xstrdup(git_path("MERGE_MSG"));
254+
char *defmsg = git_pathdup("MERGE_MSG");
255255
struct merge_options o;
256256
struct tree *result, *next_tree, *base_tree, *head_tree;
257257
static struct lock_file index_lock;

builtin-tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static void create_tag(const unsigned char *object, const char *tag,
283283
int fd;
284284

285285
/* write the template message before editing: */
286-
path = xstrdup(git_path("TAG_EDITMSG"));
286+
path = git_pathdup("TAG_EDITMSG");
287287
fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
288288
if (fd < 0)
289289
die("could not create file '%s': %s",

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ extern int check_repository_format(void);
495495
#define DATA_CHANGED 0x0020
496496
#define TYPE_CHANGED 0x0040
497497

498+
extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
499+
__attribute__((format (printf, 3, 4)));
500+
extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
501+
__attribute__((format (printf, 3, 4)));
502+
extern char *git_pathdup(const char *fmt, ...)
503+
__attribute__((format (printf, 1, 2)));
504+
498505
/* Return a statically allocated filename matching the sha1 signature */
499506
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
500507
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));

config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int git_config(config_fn_t fn, void *data)
649649
free(user_config);
650650
}
651651

652-
repo_config = xstrdup(git_path("config"));
652+
repo_config = git_pathdup("config");
653653
ret += git_config_from_file(fn, repo_config, data);
654654
free(repo_config);
655655
return ret;
@@ -889,7 +889,7 @@ int git_config_set_multivar(const char* key, const char* value,
889889
if (config_exclusive_filename)
890890
config_filename = xstrdup(config_exclusive_filename);
891891
else
892-
config_filename = xstrdup(git_path("config"));
892+
config_filename = git_pathdup("config");
893893

894894
/*
895895
* Since "key" actually contains the section name and the real
@@ -1149,7 +1149,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
11491149
if (config_exclusive_filename)
11501150
config_filename = xstrdup(config_exclusive_filename);
11511151
else
1152-
config_filename = xstrdup(git_path("config"));
1152+
config_filename = git_pathdup("config");
11531153
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
11541154
if (out_fd < 0) {
11551155
ret = error("could not lock config file %s", config_filename);

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void setup_git_env(void)
7171
}
7272
git_graft_file = getenv(GRAFT_ENVIRONMENT);
7373
if (!git_graft_file)
74-
git_graft_file = xstrdup(git_path("info/grafts"));
74+
git_graft_file = git_pathdup("info/grafts");
7575
}
7676

7777
int is_bare_repository(void)

path.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,60 @@ static char *cleanup_path(char *path)
3232
return path;
3333
}
3434

35+
char *mksnpath(char *buf, size_t n, const char *fmt, ...)
36+
{
37+
va_list args;
38+
unsigned len;
39+
40+
va_start(args, fmt);
41+
len = vsnprintf(buf, n, fmt, args);
42+
va_end(args);
43+
if (len >= n) {
44+
snprintf(buf, n, bad_path);
45+
return buf;
46+
}
47+
return cleanup_path(buf);
48+
}
49+
50+
static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
51+
{
52+
const char *git_dir = get_git_dir();
53+
size_t len;
54+
55+
len = strlen(git_dir);
56+
if (n < len + 1)
57+
goto bad;
58+
memcpy(buf, git_dir, len);
59+
if (len && !is_dir_sep(git_dir[len-1]))
60+
buf[len++] = '/';
61+
len += vsnprintf(buf + len, n - len, fmt, args);
62+
if (len >= n)
63+
goto bad;
64+
return cleanup_path(buf);
65+
bad:
66+
snprintf(buf, n, bad_path);
67+
return buf;
68+
}
69+
70+
char *git_snpath(char *buf, size_t n, const char *fmt, ...)
71+
{
72+
va_list args;
73+
va_start(args, fmt);
74+
(void)git_vsnpath(buf, n, fmt, args);
75+
va_end(args);
76+
return buf;
77+
}
78+
79+
char *git_pathdup(const char *fmt, ...)
80+
{
81+
char path[PATH_MAX];
82+
va_list args;
83+
va_start(args, fmt);
84+
(void)git_vsnpath(path, sizeof(path), fmt, args);
85+
va_end(args);
86+
return xstrdup(path);
87+
}
88+
3589
char *mkpath(const char *fmt, ...)
3690
{
3791
va_list args;

0 commit comments

Comments
 (0)