Skip to content

Commit 98b35e2

Browse files
committed
Merge branch 'ar/maint-mksnpath' into ar/mksnpath
* ar/maint-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 Conflicts: builtin-revert.c refs.c rerere.c
2 parents 058412d + a4f34cb commit 98b35e2

11 files changed

Lines changed: 60 additions & 15 deletions

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-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-
const char *defmsg = xstrdup(git_path("MERGE_MSG"));
254+
const 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ extern int check_repository_format(void);
497497

498498
extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
499499
__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)));
500504

501505
/* Return a statically allocated filename matching the sha1 signature */
502506
extern char *mkpath(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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,45 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
4747
return cleanup_path(buf);
4848
}
4949

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+
5089
char *mkpath(const char *fmt, ...)
5190
{
5291
va_list args;

refs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,15 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
413413
*flag = 0;
414414

415415
for (;;) {
416-
const char *path = git_path("%s", ref);
416+
char path[PATH_MAX];
417417
struct stat st;
418418
char *buf;
419419
int fd;
420420

421421
if (--depth < 0)
422422
return NULL;
423423

424+
git_snpath(path, sizeof(path), "%s", ref);
424425
/* Special case: non-existing file. */
425426
if (lstat(path, &st) < 0) {
426427
struct ref_list *list = get_packed_refs();
@@ -1130,13 +1131,14 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
11301131
int logfd, written, oflags = O_APPEND | O_WRONLY;
11311132
unsigned maxlen, len;
11321133
int msglen;
1133-
char *log_file, *logrec;
1134+
char log_file[PATH_MAX];
1135+
char *logrec;
11341136
const char *committer;
11351137

11361138
if (log_all_ref_updates < 0)
11371139
log_all_ref_updates = !is_bare_repository();
11381140

1139-
log_file = git_path("logs/%s", ref_name);
1141+
git_snpath(log_file, sizeof(log_file), "logs/%s", ref_name);
11401142

11411143
if (log_all_ref_updates &&
11421144
(!prefixcmp(ref_name, "refs/heads/") ||
@@ -1265,7 +1267,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
12651267
const char *lockpath;
12661268
char ref[1000];
12671269
int fd, len, written;
1268-
char *git_HEAD = xstrdup(git_path("%s", ref_target));
1270+
char *git_HEAD = git_pathdup("%s", ref_target);
12691271
unsigned char old_sha1[20], new_sha1[20];
12701272

12711273
if (logmsg && read_ref(ref_target, old_sha1))

rerere.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ int setup_rerere(struct string_list *merge_rr)
351351
if (!is_rerere_enabled())
352352
return -1;
353353

354-
merge_rr_path = xstrdup(git_path("MERGE_RR"));
354+
merge_rr_path = git_pathdup("MERGE_RR");
355355
fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
356356
LOCK_DIE_ON_ERROR);
357357
read_rr(merge_rr);

0 commit comments

Comments
 (0)