Skip to content

Commit a0355f6

Browse files
peffgitster
authored andcommitted
http-push: use an argv_array for setup_revisions
This drops the magic number for the fixed-size argv arrays, so we do not have to wonder if we are overflowing it. We can also drop some confusing sha1_to_hex memory allocation (which seems to predate the ring of buffers allowing multiple calls), and get rid of an unchecked sprintf call. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 984a43b commit a0355f6

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

http-push.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "remote.h"
1111
#include "list-objects.h"
1212
#include "sigchain.h"
13+
#include "argv-array.h"
1314

1415
#ifdef EXPAT_NEEDS_XMLPARSE_H
1516
#include <xmlparse.h>
@@ -1856,9 +1857,7 @@ int main(int argc, char **argv)
18561857
new_refs = 0;
18571858
for (ref = remote_refs; ref; ref = ref->next) {
18581859
char old_hex[60], *new_hex;
1859-
const char *commit_argv[5];
1860-
int commit_argc;
1861-
char *new_sha1_hex, *old_sha1_hex;
1860+
struct argv_array commit_argv = ARGV_ARRAY_INIT;
18621861

18631862
if (!ref->peer_ref)
18641863
continue;
@@ -1937,27 +1936,15 @@ int main(int argc, char **argv)
19371936
}
19381937

19391938
/* Set up revision info for this refspec */
1940-
commit_argc = 3;
1941-
new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
1942-
old_sha1_hex = NULL;
1943-
commit_argv[1] = "--objects";
1944-
commit_argv[2] = new_sha1_hex;
1945-
if (!push_all && !is_null_sha1(ref->old_sha1)) {
1946-
old_sha1_hex = xmalloc(42);
1947-
sprintf(old_sha1_hex, "^%s",
1948-
sha1_to_hex(ref->old_sha1));
1949-
commit_argv[3] = old_sha1_hex;
1950-
commit_argc++;
1951-
}
1952-
commit_argv[commit_argc] = NULL;
1939+
argv_array_push(&commit_argv, ""); /* ignored */
1940+
argv_array_push(&commit_argv, "--objects");
1941+
argv_array_push(&commit_argv, sha1_to_hex(ref->new_sha1));
1942+
if (!push_all && !is_null_sha1(ref->old_sha1))
1943+
argv_array_pushf(&commit_argv, "^%s",
1944+
sha1_to_hex(ref->old_sha1));
19531945
init_revisions(&revs, setup_git_directory());
1954-
setup_revisions(commit_argc, commit_argv, &revs, NULL);
1946+
setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
19551947
revs.edge_hint = 0; /* just in case */
1956-
free(new_sha1_hex);
1957-
if (old_sha1_hex) {
1958-
free(old_sha1_hex);
1959-
commit_argv[1] = NULL;
1960-
}
19611948

19621949
/* Generate a list of objects that need to be pushed */
19631950
pushing = 0;
@@ -1986,6 +1973,7 @@ int main(int argc, char **argv)
19861973
printf("%s %s\n", !rc ? "ok" : "error", ref->name);
19871974
unlock_remote(ref_lock);
19881975
check_locks();
1976+
argv_array_clear(&commit_argv);
19891977
}
19901978

19911979
/* Update remote server info if appropriate */

0 commit comments

Comments
 (0)