fsmonitor: use internal argv_array of struct child_process
Avoid magic array sizes and indexes by constructing the fsmonitor command line using the embedded argv_array of the child_process. The resulting code is shorter and easier to extend. Getting rid of the snprintf() calls is a bonus -- even though the buffers were big enough here to avoid truncation -- as it makes auditing the remaining callers easier. Inspired-by: Jeff King <peff@peff.net> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 19, 2018 at 10:27 UTC
735e4173b3feba0cc495bbb2dd85341b6378f628
1 file changed
+4
-10
fsmonitor.c
+4
-10
@@ -97,19 +97,13 @@ void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
97
static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
98
{
99
struct child_process cp = CHILD_PROCESS_INIT;
100
- char ver[64];
101
- char date[64];
102
- const char *argv[4];
100
104
- if (!(argv[0] = core_fsmonitor))
101
+ if (!core_fsmonitor)
102
return -1;
103
107
- snprintf(ver, sizeof(version), "%d", version);
108
- snprintf(date, sizeof(date), "%" PRIuMAX, (uintmax_t)last_update);
109
- argv[1] = ver;
110
- argv[2] = date;
111
- argv[3] = NULL;
112
- cp.argv = argv;
104
+ argv_array_push(&cp.args, core_fsmonitor);
105
+ argv_array_pushf(&cp.args, "%d", version);
106
+ argv_array_pushf(&cp.args, "%" PRIuMAX, (uintmax_t)last_update);
107
cp.use_shell = 1;
108
cp.dir = get_git_work_tree();
109