Skip to content

Commit da3f78a

Browse files
Miklos Vajnagitster
authored andcommitted
builtin-branch: use strbuf in rename_branch()
In case the length of branch name is greather then PATH_MAX-11, we write to unallocated memory otherwise. Signed-off-by: Miklos Vajna <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3f9f9a commit da3f78a

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

builtin-branch.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -424,42 +424,45 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
424424

425425
static void rename_branch(const char *oldname, const char *newname, int force)
426426
{
427-
char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
427+
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
428428
unsigned char sha1[20];
429-
char oldsection[PATH_MAX], newsection[PATH_MAX];
429+
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
430430

431431
if (!oldname)
432432
die("cannot rename the current branch while not on any.");
433433

434-
if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
435-
die("Old branchname too long");
434+
strbuf_addf(&oldref, "refs/heads/%s", oldname);
436435

437-
if (check_ref_format(oldref))
438-
die("Invalid branch name: %s", oldref);
436+
if (check_ref_format(oldref.buf))
437+
die("Invalid branch name: %s", oldref.buf);
439438

440-
if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
441-
die("New branchname too long");
439+
strbuf_addf(&newref, "refs/heads/%s", newname);
442440

443-
if (check_ref_format(newref))
444-
die("Invalid branch name: %s", newref);
441+
if (check_ref_format(newref.buf))
442+
die("Invalid branch name: %s", newref.buf);
445443

446-
if (resolve_ref(newref, sha1, 1, NULL) && !force)
444+
if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
447445
die("A branch named '%s' already exists.", newname);
448446

449-
snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
450-
oldref, newref);
447+
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
448+
oldref.buf, newref.buf);
451449

452-
if (rename_ref(oldref, newref, logmsg))
450+
if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
453451
die("Branch rename failed");
452+
strbuf_release(&logmsg);
454453

455454
/* no need to pass logmsg here as HEAD didn't really move */
456-
if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
455+
if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
457456
die("Branch renamed to %s, but HEAD is not updated!", newname);
458457

459-
snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
460-
snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
461-
if (git_config_rename_section(oldsection, newsection) < 0)
458+
strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
459+
strbuf_release(&oldref);
460+
strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
461+
strbuf_release(&newref);
462+
if (git_config_rename_section(oldsection.buf, newsection.buf) < 0)
462463
die("Branch is renamed, but update of config-file failed");
464+
strbuf_release(&oldsection);
465+
strbuf_release(&newsection);
463466
}
464467

465468
static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)

0 commit comments

Comments
 (0)