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>
Thomas Rast committed
Jun 10, 2010 at 14:54 UTC
157aaea5fff7dbf2fc39829d827b35df647562a4
3 files changed
+15
-17
builtin/checkout.c
+2
-2
@@ -496,12 +496,12 @@ static void update_refs_for_switch(struct checkout_opts *opts,
496
if (opts->new_orphan_branch) {
497
if (opts->new_branch_log && !log_all_ref_updates) {
498
int temp;
499
- char *log_file;
499
+ char log_file[PATH_MAX];
500
char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
501
502
temp = log_all_ref_updates;
503
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))) {
505
fprintf(stderr, "Can not do reflog for '%s'\n",
506
opts->new_orphan_branch);
507
log_all_ref_updates = temp;
refs.c
+12
-14
@@ -1258,43 +1258,41 @@ static int copy_msg(char *buf, const char *msg)
1258
return cp - buf;
1259
}
1260
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)
1262
{
1263
int logfd, oflags = O_APPEND | O_WRONLY;
1264
- char logfile[PATH_MAX];
1264
1266
- git_snpath(logfile, sizeof(logfile), "logs/%s", ref_name);
1267
- *log_file = logfile;
1265
+ git_snpath(logfile, bufsize, "logs/%s", ref_name);
1266
if (log_all_ref_updates &&
1267
(!prefixcmp(ref_name, "refs/heads/") ||
1268
!prefixcmp(ref_name, "refs/remotes/") ||
1269
!prefixcmp(ref_name, "refs/notes/") ||
1270
!strcmp(ref_name, "HEAD"))) {
1273
- if (safe_create_leading_directories(*log_file) < 0)
1271
+ if (safe_create_leading_directories(logfile) < 0)
1272
return error("unable to create directory for %s",
1275
- *log_file);
1273
+ logfile);
1274
oflags |= O_CREAT;
1275
}
1276
1279
- logfd = open(*log_file, oflags, 0666);
1277
+ logfd = open(logfile, oflags, 0666);
1278
if (logfd < 0) {
1279
if (!(oflags & O_CREAT) && errno == ENOENT)
1280
return 0;
1281
1282
if ((oflags & O_CREAT) && errno == EISDIR) {
1285
- if (remove_empty_directories(*log_file)) {
1283
+ if (remove_empty_directories(logfile)) {
1284
return error("There are still logs under '%s'",
1287
- *log_file);
1285
+ logfile);
1286
}
1289
- logfd = open(*log_file, oflags, 0666);
1287
+ logfd = open(logfile, oflags, 0666);
1288
}
1289
1290
if (logfd < 0)
1291
return error("Unable to append to %s: %s",
1294
- *log_file, strerror(errno));
1292
+ logfile, strerror(errno));
1293
}
1294
1297
- adjust_shared_perm(*log_file);
1295
+ adjust_shared_perm(logfile);
1296
close(logfd);
1297
return 0;
1298
}
@@ -1305,14 +1303,14 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1303
int logfd, result, written, oflags = O_APPEND | O_WRONLY;
1304
unsigned maxlen, len;
1305
int msglen;
1308
- char *log_file;
1306
+ char log_file[PATH_MAX];
1307
char *logrec;
1308
const char *committer;
1309
1310
if (log_all_ref_updates < 0)
1311
log_all_ref_updates = !is_bare_repository();
1312
1315
- result = log_ref_setup(ref_name, &log_file);
1313
+ result = log_ref_setup(ref_name, log_file, sizeof(log_file));
1314
if (result)
1315
return result;
1316
refs.h
+1
-1
@@ -69,7 +69,7 @@ extern void unlock_ref(struct ref_lock *lock);
69
extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
70
71
/** 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);
73
74
/** Reads log for the value of ref during at_time. **/
75
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);