@matheusbsbs / matheus-bagliano / commits / da3f78a29c
Fork 0

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 <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Miklos Vajna committed Nov 17, 2008 at 21:48 UTC da3f78a29cafee487c107a81c442213fa41463f7
1 file changed +21 -18
builtin-branch.c
+21 -18
@@ -424,42 +424,45 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
424
425 static void rename_branch(const char *oldname, const char *newname, int force)
426 {
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;
428 unsigned char sha1[20];
429 - char oldsection[PATH_MAX], newsection[PATH_MAX];
429 + struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
430
431 if (!oldname)
432 die("cannot rename the current branch while not on any.");
433
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);
435
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);
438
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);
440
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);
443
446 - if (resolve_ref(newref, sha1, 1, NULL) && !force)
444 + if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
445 die("A branch named '%s' already exists.", newname);
446
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);
449
452 - if (rename_ref(oldref, newref, logmsg))
450 + if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
451 die("Branch rename failed");
452 + strbuf_release(&logmsg);
453
454 /* 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))
456 die("Branch renamed to %s, but HEAD is not updated!", newname);
457
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)
463 die("Branch is renamed, but update of config-file failed");
464 + strbuf_release(&oldsection);
465 + strbuf_release(&newsection);
466 }
467
468 static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)