Skip to content

Commit 3818b25

Browse files
peffgitster
authored andcommitted
create_branch: move msg setup closer to point of use
In create_branch() we write the reflog msg into a buffer in the main function, but then use it only inside a conditional. If you carefully follow the logic, you can confirm that we never use the buffer uninitialized nor write when it would not be used. But we can make this a lot more obvious by simply moving the write step inside the conditional. Signed-off-by: Jeff King <[email protected]>
1 parent 6cd4a89 commit 3818b25

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

branch.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ void create_branch(const char *name, const char *start_name,
234234
{
235235
struct commit *commit;
236236
unsigned char sha1[20];
237-
char *real_ref, msg[PATH_MAX + 20];
237+
char *real_ref;
238238
struct strbuf ref = STRBUF_INIT;
239239
int forcing = 0;
240240
int dont_change_ref = 0;
@@ -290,19 +290,20 @@ void create_branch(const char *name, const char *start_name,
290290
die(_("Not a valid branch point: '%s'."), start_name);
291291
hashcpy(sha1, commit->object.oid.hash);
292292

293-
if (forcing)
294-
snprintf(msg, sizeof msg, "branch: Reset to %s",
295-
start_name);
296-
else if (!dont_change_ref)
297-
snprintf(msg, sizeof msg, "branch: Created from %s",
298-
start_name);
299-
300293
if (reflog)
301294
log_all_ref_updates = LOG_REFS_NORMAL;
302295

303296
if (!dont_change_ref) {
304297
struct ref_transaction *transaction;
305298
struct strbuf err = STRBUF_INIT;
299+
char msg[PATH_MAX + 20];
300+
301+
if (forcing)
302+
snprintf(msg, sizeof msg, "branch: Reset to %s",
303+
start_name);
304+
else
305+
snprintf(msg, sizeof msg, "branch: Created from %s",
306+
start_name);
306307

307308
transaction = ref_transaction_begin(&err);
308309
if (!transaction ||

0 commit comments

Comments
 (0)