Skip to content

Commit 418c9b1

Browse files
jrngitster
authored andcommitted
do not let git_path clobber errno when reporting errors
Because git_path() calls vsnprintf(), code like fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666); die_errno(_("Could not write to '%s'"), git_path("SQUASH_MSG")); can end up printing an error indicator from vsnprintf() instead of open() by mistake. Store the path we are trying to write to in a temporary variable and pass _that_ to die_errno(), so the messages written by git cherry-pick/revert and git merge can avoid this source of confusion. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d2440f commit 418c9b1

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

builtin/merge.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,15 @@ static void squash_message(struct commit *commit)
316316
struct rev_info rev;
317317
struct strbuf out = STRBUF_INIT;
318318
struct commit_list *j;
319+
const char *filename;
319320
int fd;
320321
struct pretty_print_context ctx = {0};
321322

322323
printf(_("Squash commit -- not updating HEAD\n"));
323-
fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666);
324+
filename = git_path("SQUASH_MSG");
325+
fd = open(filename, O_WRONLY | O_CREAT, 0666);
324326
if (fd < 0)
325-
die_errno(_("Could not write to '%s'"), git_path("SQUASH_MSG"));
327+
die_errno(_("Could not write to '%s'"), filename);
326328

327329
init_revisions(&rev, NULL);
328330
rev.ignore_merges = 1;
@@ -492,14 +494,16 @@ static void merge_name(const char *remote, struct strbuf *msg)
492494

493495
if (!strcmp(remote, "FETCH_HEAD") &&
494496
!access(git_path("FETCH_HEAD"), R_OK)) {
497+
const char *filename;
495498
FILE *fp;
496499
struct strbuf line = STRBUF_INIT;
497500
char *ptr;
498501

499-
fp = fopen(git_path("FETCH_HEAD"), "r");
502+
filename = git_path("FETCH_HEAD");
503+
fp = fopen(filename, "r");
500504
if (!fp)
501505
die_errno(_("could not open '%s' for reading"),
502-
git_path("FETCH_HEAD"));
506+
filename);
503507
strbuf_getline(&line, fp, '\n');
504508
fclose(fp);
505509
ptr = strstr(line.buf, "\tnot-for-merge\t");
@@ -847,20 +851,22 @@ static void add_strategies(const char *string, unsigned attr)
847851

848852
static void write_merge_msg(struct strbuf *msg)
849853
{
850-
int fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
854+
const char *filename = git_path("MERGE_MSG");
855+
int fd = open(filename, O_WRONLY | O_CREAT, 0666);
851856
if (fd < 0)
852857
die_errno(_("Could not open '%s' for writing"),
853-
git_path("MERGE_MSG"));
858+
filename);
854859
if (write_in_full(fd, msg->buf, msg->len) != msg->len)
855-
die_errno(_("Could not write to '%s'"), git_path("MERGE_MSG"));
860+
die_errno(_("Could not write to '%s'"), filename);
856861
close(fd);
857862
}
858863

859864
static void read_merge_msg(struct strbuf *msg)
860865
{
866+
const char *filename = git_path("MERGE_MSG");
861867
strbuf_reset(msg);
862-
if (strbuf_read_file(msg, git_path("MERGE_MSG"), 0) < 0)
863-
die_errno(_("Could not read from '%s'"), git_path("MERGE_MSG"));
868+
if (strbuf_read_file(msg, filename, 0) < 0)
869+
die_errno(_("Could not read from '%s'"), filename);
864870
}
865871

866872
static void write_merge_state(void);
@@ -948,13 +954,14 @@ static int finish_automerge(struct commit *head,
948954

949955
static int suggest_conflicts(int renormalizing)
950956
{
957+
const char *filename;
951958
FILE *fp;
952959
int pos;
953960

954-
fp = fopen(git_path("MERGE_MSG"), "a");
961+
filename = git_path("MERGE_MSG");
962+
fp = fopen(filename, "a");
955963
if (!fp)
956-
die_errno(_("Could not open '%s' for writing"),
957-
git_path("MERGE_MSG"));
964+
die_errno(_("Could not open '%s' for writing"), filename);
958965
fprintf(fp, "\nConflicts:\n");
959966
for (pos = 0; pos < active_nr; pos++) {
960967
struct cache_entry *ce = active_cache[pos];
@@ -1046,31 +1053,33 @@ static int setup_with_upstream(const char ***argv)
10461053

10471054
static void write_merge_state(void)
10481055
{
1056+
const char *filename;
10491057
int fd;
10501058
struct commit_list *j;
10511059
struct strbuf buf = STRBUF_INIT;
10521060

10531061
for (j = remoteheads; j; j = j->next)
10541062
strbuf_addf(&buf, "%s\n",
10551063
sha1_to_hex(j->item->object.sha1));
1056-
fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666);
1064+
filename = git_path("MERGE_HEAD");
1065+
fd = open(filename, O_WRONLY | O_CREAT, 0666);
10571066
if (fd < 0)
1058-
die_errno(_("Could not open '%s' for writing"),
1059-
git_path("MERGE_HEAD"));
1067+
die_errno(_("Could not open '%s' for writing"), filename);
10601068
if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1061-
die_errno(_("Could not write to '%s'"), git_path("MERGE_HEAD"));
1069+
die_errno(_("Could not write to '%s'"), filename);
10621070
close(fd);
10631071
strbuf_addch(&merge_msg, '\n');
10641072
write_merge_msg(&merge_msg);
1065-
fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
1073+
1074+
filename = git_path("MERGE_MODE");
1075+
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
10661076
if (fd < 0)
1067-
die_errno(_("Could not open '%s' for writing"),
1068-
git_path("MERGE_MODE"));
1077+
die_errno(_("Could not open '%s' for writing"), filename);
10691078
strbuf_reset(&buf);
10701079
if (!allow_fast_forward)
10711080
strbuf_addf(&buf, "no-ff");
10721081
if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1073-
die_errno(_("Could not write to '%s'"), git_path("MERGE_MODE"));
1082+
die_errno(_("Could not write to '%s'"), filename);
10741083
close(fd);
10751084
}
10761085

builtin/revert.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,18 @@ static char *get_encoding(const char *message)
288288

289289
static void write_cherry_pick_head(struct commit *commit)
290290
{
291+
const char *filename;
291292
int fd;
292293
struct strbuf buf = STRBUF_INIT;
293294

294295
strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1));
295296

296-
fd = open(git_path("CHERRY_PICK_HEAD"), O_WRONLY | O_CREAT, 0666);
297+
filename = git_path("CHERRY_PICK_HEAD");
298+
fd = open(filename, O_WRONLY | O_CREAT, 0666);
297299
if (fd < 0)
298-
die_errno(_("Could not open '%s' for writing"),
299-
git_path("CHERRY_PICK_HEAD"));
300+
die_errno(_("Could not open '%s' for writing"), filename);
300301
if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd))
301-
die_errno(_("Could not write to '%s'"), git_path("CHERRY_PICK_HEAD"));
302+
die_errno(_("Could not write to '%s'"), filename);
302303
strbuf_release(&buf);
303304
}
304305

0 commit comments

Comments
 (0)