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 <peff@peff.net>
Jeff King committed
Mar 28, 2017 at 15:46 UTC
3818b258dc145aae25c48cf4fca830be0cb69c6e
1 file changed
+9
-8
branch.c
+9
-8
@@ -234,7 +234,7 @@ void create_branch(const char *name, const char *start_name,
234
{
235
struct commit *commit;
236
unsigned char sha1[20];
237
- char *real_ref, msg[PATH_MAX + 20];
237
+ char *real_ref;
238
struct strbuf ref = STRBUF_INIT;
239
int forcing = 0;
240
int dont_change_ref = 0;
@@ -290,19 +290,20 @@ void create_branch(const char *name, const char *start_name,
290
die(_("Not a valid branch point: '%s'."), start_name);
291
hashcpy(sha1, commit->object.oid.hash);
292
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
-
293
if (reflog)
294
log_all_ref_updates = LOG_REFS_NORMAL;
295
296
if (!dont_change_ref) {
297
struct ref_transaction *transaction;
298
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);
307
308
transaction = ref_transaction_begin(&err);
309
if (!transaction ||