Skip to content

Commit 157aaea

Browse files
trastgitster
authored andcommitted
log_ref_setup: don't return stack-allocated array
859c301 (refs: split log_ref_write logic into log_ref_setup, 2010-05-21) refactors the stack allocation of the log_file array into the new log_ref_setup() function, but passes it back to the caller. Since the original intent seems to have been to split the work between log_ref_setup and log_ref_write, make it the caller's responsibility to allocate the buffer. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Reported-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 86e8e7a commit 157aaea

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,12 @@ static void update_refs_for_switch(struct checkout_opts *opts,
496496
if (opts->new_orphan_branch) {
497497
if (opts->new_branch_log && !log_all_ref_updates) {
498498
int temp;
499-
char *log_file;
499+
char log_file[PATH_MAX];
500500
char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
501501

502502
temp = log_all_ref_updates;
503503
log_all_ref_updates = 1;
504-
if (log_ref_setup(ref_name, &log_file)) {
504+
if (log_ref_setup(ref_name, log_file, sizeof(log_file))) {
505505
fprintf(stderr, "Can not do reflog for '%s'\n",
506506
opts->new_orphan_branch);
507507
log_all_ref_updates = temp;

refs.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,43 +1258,41 @@ static int copy_msg(char *buf, const char *msg)
12581258
return cp - buf;
12591259
}
12601260

1261-
int log_ref_setup(const char *ref_name, char **log_file)
1261+
int log_ref_setup(const char *ref_name, char *logfile, int bufsize)
12621262
{
12631263
int logfd, oflags = O_APPEND | O_WRONLY;
1264-
char logfile[PATH_MAX];
12651264

1266-
git_snpath(logfile, sizeof(logfile), "logs/%s", ref_name);
1267-
*log_file = logfile;
1265+
git_snpath(logfile, bufsize, "logs/%s", ref_name);
12681266
if (log_all_ref_updates &&
12691267
(!prefixcmp(ref_name, "refs/heads/") ||
12701268
!prefixcmp(ref_name, "refs/remotes/") ||
12711269
!prefixcmp(ref_name, "refs/notes/") ||
12721270
!strcmp(ref_name, "HEAD"))) {
1273-
if (safe_create_leading_directories(*log_file) < 0)
1271+
if (safe_create_leading_directories(logfile) < 0)
12741272
return error("unable to create directory for %s",
1275-
*log_file);
1273+
logfile);
12761274
oflags |= O_CREAT;
12771275
}
12781276

1279-
logfd = open(*log_file, oflags, 0666);
1277+
logfd = open(logfile, oflags, 0666);
12801278
if (logfd < 0) {
12811279
if (!(oflags & O_CREAT) && errno == ENOENT)
12821280
return 0;
12831281

12841282
if ((oflags & O_CREAT) && errno == EISDIR) {
1285-
if (remove_empty_directories(*log_file)) {
1283+
if (remove_empty_directories(logfile)) {
12861284
return error("There are still logs under '%s'",
1287-
*log_file);
1285+
logfile);
12881286
}
1289-
logfd = open(*log_file, oflags, 0666);
1287+
logfd = open(logfile, oflags, 0666);
12901288
}
12911289

12921290
if (logfd < 0)
12931291
return error("Unable to append to %s: %s",
1294-
*log_file, strerror(errno));
1292+
logfile, strerror(errno));
12951293
}
12961294

1297-
adjust_shared_perm(*log_file);
1295+
adjust_shared_perm(logfile);
12981296
close(logfd);
12991297
return 0;
13001298
}
@@ -1305,14 +1303,14 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
13051303
int logfd, result, written, oflags = O_APPEND | O_WRONLY;
13061304
unsigned maxlen, len;
13071305
int msglen;
1308-
char *log_file;
1306+
char log_file[PATH_MAX];
13091307
char *logrec;
13101308
const char *committer;
13111309

13121310
if (log_all_ref_updates < 0)
13131311
log_all_ref_updates = !is_bare_repository();
13141312

1315-
result = log_ref_setup(ref_name, &log_file);
1313+
result = log_ref_setup(ref_name, log_file, sizeof(log_file));
13161314
if (result)
13171315
return result;
13181316

refs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extern void unlock_ref(struct ref_lock *lock);
6969
extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
7070

7171
/** Setup reflog before using. **/
72-
int log_ref_setup(const char *ref_name, char **log_file);
72+
int log_ref_setup(const char *ref_name, char *logfile, int bufsize);
7373

7474
/** Reads log for the value of ref during at_time. **/
7575
extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);

0 commit comments

Comments
 (0)