Skip to content

Commit dcf6926

Browse files
pcloudsgitster
authored andcommitted
path.c: make get_pathname() call sites return const char *
Before the previous commit, get_pathname returns an array of PATH_MAX length. Even if git_path() and similar functions does not use the whole array, git_path() caller can, in theory. After the commit, get_pathname() may return a buffer that has just enough room for the returned string and git_path() caller should never write beyond that. Make git_path(), mkpath() and git_path_submodule() return a const buffer to make sure callers do not write in it at all. This could have been part of the previous commit, but the "const" conversion is too much distraction from the core changes in path.c. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4ef9caf commit dcf6926

File tree

15 files changed

+36
-32
lines changed

15 files changed

+36
-32
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
589589
if (opts->new_branch_log && !log_all_ref_updates) {
590590
int temp;
591591
char log_file[PATH_MAX];
592-
char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
592+
const char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
593593

594594
temp = log_all_ref_updates;
595595
log_all_ref_updates = 1;

builtin/clone.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,17 @@ static void copy_alternates(struct strbuf *src, struct strbuf *dst,
290290
struct strbuf line = STRBUF_INIT;
291291

292292
while (strbuf_getline(&line, in, '\n') != EOF) {
293-
char *abs_path, abs_buf[PATH_MAX];
293+
char *abs_path;
294294
if (!line.len || line.buf[0] == '#')
295295
continue;
296296
if (is_absolute_path(line.buf)) {
297297
add_to_alternates_file(line.buf);
298298
continue;
299299
}
300-
abs_path = mkpath("%s/objects/%s", src_repo, line.buf);
301-
normalize_path_copy(abs_buf, abs_path);
302-
add_to_alternates_file(abs_buf);
300+
abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
301+
normalize_path_copy(abs_path, abs_path);
302+
add_to_alternates_file(abs_path);
303+
free(abs_path);
303304
}
304305
strbuf_release(&line);
305306
fclose(in);

builtin/fetch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
587587
struct strbuf note = STRBUF_INIT;
588588
const char *what, *kind;
589589
struct ref *rm;
590-
char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
590+
char *url;
591+
const char *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
591592
int want_status;
592593

593594
fp = fopen(filename, "a");
@@ -821,7 +822,7 @@ static void check_not_current_branch(struct ref *ref_map)
821822

822823
static int truncate_fetch_head(void)
823824
{
824-
char *filename = git_path("FETCH_HEAD");
825+
const char *filename = git_path("FETCH_HEAD");
825826
FILE *fp = fopen(filename, "w");
826827

827828
if (!fp)

builtin/fsck.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ static void check_unreachable_object(struct object *obj)
225225
printf("dangling %s %s\n", typename(obj->type),
226226
sha1_to_hex(obj->sha1));
227227
if (write_lost_and_found) {
228-
char *filename = git_path("lost-found/%s/%s",
228+
const char *filename = git_path("lost-found/%s/%s",
229229
obj->type == OBJ_COMMIT ? "commit" : "other",
230230
sha1_to_hex(obj->sha1));
231231
FILE *f;
232232

233-
if (safe_create_leading_directories(filename)) {
233+
if (safe_create_leading_directories_const(filename)) {
234234
error("Could not create lost-found");
235235
return;
236236
}

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ static void run_update_post_hook(struct command *commands)
869869
int argc;
870870
const char **argv;
871871
struct child_process proc = CHILD_PROCESS_INIT;
872-
char *hook;
872+
const char *hook;
873873

874874
hook = find_hook("post-update");
875875
for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ static int migrate_file(struct remote *remote)
582582
{
583583
struct strbuf buf = STRBUF_INIT;
584584
int i;
585-
char *path = NULL;
585+
const char *path = NULL;
586586

587587
strbuf_addf(&buf, "remote.%s.url", remote->name);
588588
for (i = 0; i < remote->url_nr; i++)

builtin/repack.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
284284
failed = 0;
285285
for_each_string_list_item(item, &names) {
286286
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
287-
char *fname, *fname_old;
287+
const char *fname_old;
288+
char *fname;
288289
fname = mkpathdup("%s/pack-%s%s", packdir,
289290
item->string, exts[ext].name);
290291
if (!file_exists(fname)) {
@@ -312,7 +313,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
312313
if (failed) {
313314
struct string_list rollback_failure = STRING_LIST_INIT_DUP;
314315
for_each_string_list_item(item, &rollback) {
315-
char *fname, *fname_old;
316+
const char *fname_old;
317+
char *fname;
316318
fname = mkpathdup("%s/%s", packdir, item->string);
317319
fname_old = mkpath("%s/old-%s", packdir, item->string);
318320
if (rename(fname_old, fname))
@@ -365,7 +367,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
365367
/* Remove the "old-" files */
366368
for_each_string_list_item(item, &names) {
367369
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
368-
char *fname;
370+
const char *fname;
369371
fname = mkpath("%s/old-%s%s",
370372
packdir,
371373
item->string,

cache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,9 @@ extern char *mkpathdup(const char *fmt, ...)
687687
__attribute__((format (printf, 1, 2)));
688688

689689
/* Return a statically allocated filename matching the sha1 signature */
690-
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
691-
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
692-
extern char *git_path_submodule(const char *path, const char *fmt, ...)
690+
extern const char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
691+
extern const char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
692+
extern const char *git_path_submodule(const char *path, const char *fmt, ...)
693693
__attribute__((format (printf, 2, 3)));
694694

695695
/*

fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
405405

406406
static void write_crash_report(const char *err)
407407
{
408-
char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
408+
const char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
409409
FILE *rpt = fopen(loc, "w");
410410
struct branch *b;
411411
unsigned long lu;

notes-merge.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static void check_notes_merge_worktree(struct notes_merge_options *o)
280280
"(%s exists).", git_path("NOTES_MERGE_*"));
281281
}
282282

283-
if (safe_create_leading_directories(git_path(
283+
if (safe_create_leading_directories_const(git_path(
284284
NOTES_MERGE_WORKTREE "/.test")))
285285
die_errno("unable to create directory %s",
286286
git_path(NOTES_MERGE_WORKTREE));
@@ -295,8 +295,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
295295
const char *buf, unsigned long size)
296296
{
297297
int fd;
298-
char *path = git_path(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
299-
if (safe_create_leading_directories(path))
298+
const char *path = git_path(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
299+
if (safe_create_leading_directories_const(path))
300300
die_errno("unable to create directory for '%s'", path);
301301
if (file_exists(path))
302302
die("found existing file at '%s'", path);

0 commit comments

Comments
 (0)