@matheusbsbs / matheus-bagliano / commits / 7a33bcbe80
Fork 0

sq_quote_argv and add_to_string rework with strbuf's.

* sq_quote_buf is made public, and works on a strbuf. * sq_quote_argv also works on a strbuf. * make sq_quote_argv take a "maxlen" argument to check the buffer won't grow too big. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 20, 2007 at 00:42 UTC 7a33bcbe802080f3a926e93d66b65ff7c5e8c5ed
6 files changed +86 -137
connect.c
+9 -12
@@ -577,16 +577,13 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
577 if (pid < 0)
578 die("unable to fork");
579 if (!pid) {
580 - char command[MAX_CMD_LEN];
581 - char *posn = command;
582 - int size = MAX_CMD_LEN;
583 - int of = 0;
580 + struct strbuf cmd;
581
585 - of |= add_to_string(&posn, &size, prog, 0);
586 - of |= add_to_string(&posn, &size, " ", 0);
587 - of |= add_to_string(&posn, &size, path, 1);
588 -
589 - if (of)
582 + strbuf_init(&cmd, MAX_CMD_LEN);
583 + strbuf_addstr(&cmd, prog);
584 + strbuf_addch(&cmd, ' ');
585 + sq_quote_buf(&cmd, path);
586 + if (cmd.len >= MAX_CMD_LEN)
587 die("command line too long");
588
589 dup2(pipefd[1][0], 0);
@@ -606,10 +603,10 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
603 ssh_basename++;
604
605 if (!port)
609 - execlp(ssh, ssh_basename, host, command, NULL);
606 + execlp(ssh, ssh_basename, host, cmd.buf, NULL);
607 else
608 execlp(ssh, ssh_basename, "-p", port, host,
612 - command, NULL);
609 + cmd.buf, NULL);
610 }
611 else {
612 unsetenv(ALTERNATE_DB_ENVIRONMENT);
@@ -618,7 +615,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
615 unsetenv(GIT_WORK_TREE_ENVIRONMENT);
616 unsetenv(GRAFT_ENVIRONMENT);
617 unsetenv(INDEX_ENVIRONMENT);
621 - execlp("sh", "sh", "-c", command, NULL);
618 + execlp("sh", "sh", "-c", cmd.buf, NULL);
619 }
620 die("exec failed");
621 }
git.c
+5 -11
@@ -187,19 +187,13 @@ static int handle_alias(int *argcp, const char ***argv)
187 if (alias_string) {
188 if (alias_string[0] == '!') {
189 if (*argcp > 1) {
190 - int i, sz = PATH_MAX;
191 - char *s = xmalloc(sz), *new_alias = s;
190 + struct strbuf buf;
191
193 - add_to_string(&s, &sz, alias_string, 0);
192 + strbuf_init(&buf, PATH_MAX);
193 + strbuf_addstr(&buf, alias_string);
194 + sq_quote_argv(&buf, (*argv) + 1, *argcp - 1, PATH_MAX);
195 free(alias_string);
195 - alias_string = new_alias;
196 - for (i = 1; i < *argcp &&
197 - !add_to_string(&s, &sz, " ", 0) &&
198 - !add_to_string(&s, &sz, (*argv)[i], 1)
199 - ; i++)
200 - ; /* do nothing */
201 - if (!sz)
202 - die("Too many or long arguments");
196 + alias_string = buf.buf;
197 }
198 trace_printf("trace: alias to shell cmd: %s => %s\n",
199 alias_command, alias_string + 1);
quote.c
+24 -70
@@ -12,37 +12,31 @@
12 * a'b ==> a'\''b ==> 'a'\''b'
13 * a!b ==> a'\!'b ==> 'a'\!'b'
14 */
15 -#undef EMIT
16 -#define EMIT(x) do { if (++len < n) *bp++ = (x); } while(0)
17 -
15 static inline int need_bs_quote(char c)
16 {
17 return (c == '\'' || c == '!');
18 }
19
23 -static size_t sq_quote_buf(char *dst, size_t n, const char *src)
20 +void sq_quote_buf(struct strbuf *dst, const char *src)
21 {
25 - char c;
26 - char *bp = dst;
27 - size_t len = 0;
28 -
29 - EMIT('\'');
30 - while ((c = *src++)) {
31 - if (need_bs_quote(c)) {
32 - EMIT('\'');
33 - EMIT('\\');
34 - EMIT(c);
35 - EMIT('\'');
36 - } else {
37 - EMIT(c);
22 + char *to_free = NULL;
23 +
24 + if (dst->buf == src)
25 + to_free = strbuf_detach(dst);
26 +
27 + strbuf_addch(dst, '\'');
28 + while (*src) {
29 + size_t len = strcspn(src, "'\\");
30 + strbuf_add(dst, src, len);
31 + src += len;
32 + while (need_bs_quote(*src)) {
33 + strbuf_addstr(dst, "'\\");
34 + strbuf_addch(dst, *src++);
35 + strbuf_addch(dst, '\'');
36 }
37 }
40 - EMIT('\'');
41 -
42 - if ( n )
43 - *bp = 0;
44 -
45 - return len;
38 + strbuf_addch(dst, '\'');
39 + free(to_free);
40 }
41
42 void sq_quote_print(FILE *stream, const char *src)
@@ -62,11 +56,10 @@ void sq_quote_print(FILE *stream, const char *src)
56 fputc('\'', stream);
57 }
58
65 -char *sq_quote_argv(const char** argv, int count)
59 +void sq_quote_argv(struct strbuf *dst, const char** argv, int count,
60 + size_t maxlen)
61 {
67 - char *buf, *to;
62 int i;
69 - size_t len = 0;
63
64 /* Count argv if needed. */
65 if (count < 0) {
@@ -74,53 +67,14 @@ char *sq_quote_argv(const char** argv, int count)
67 ; /* just counting */
68 }
69
77 - /* Special case: no argv. */
78 - if (!count)
79 - return xcalloc(1,1);
80 -
81 - /* Get destination buffer length. */
82 - for (i = 0; i < count; i++)
83 - len += sq_quote_buf(NULL, 0, argv[i]) + 1;
84 -
85 - /* Alloc destination buffer. */
86 - to = buf = xmalloc(len + 1);
87 -
70 /* Copy into destination buffer. */
71 + strbuf_grow(dst, 32 * count);
72 for (i = 0; i < count; ++i) {
90 - *to++ = ' ';
91 - to += sq_quote_buf(to, len, argv[i]);
73 + strbuf_addch(dst, ' ');
74 + sq_quote_buf(dst, argv[i]);
75 + if (maxlen && dst->len > maxlen)
76 + die("Too many or long arguments");
77 }
93 -
94 - return buf;
95 -}
96 -
97 -/*
98 - * Append a string to a string buffer, with or without shell quoting.
99 - * Return true if the buffer overflowed.
100 - */
101 -int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
102 -{
103 - char *p = *ptrp;
104 - int size = *sizep;
105 - int oc;
106 - int err = 0;
107 -
108 - if (quote)
109 - oc = sq_quote_buf(p, size, str);
110 - else {
111 - oc = strlen(str);
112 - memcpy(p, str, (size <= oc) ? size - 1 : oc);
113 - }
114 -
115 - if (size <= oc) {
116 - err = 1;
117 - oc = size - 1;
118 - }
119 -
120 - *ptrp += oc;
121 - **ptrp = '\0';
122 - *sizep -= oc;
123 - return err;
78 }
79
80 char *sq_dequote(char *arg)
quote.h
+3 -6
@@ -29,13 +29,10 @@
29 */
30
31 extern void sq_quote_print(FILE *stream, const char *src);
32 -extern char *sq_quote_argv(const char** argv, int count);
32
34 -/*
35 - * Append a string to a string buffer, with or without shell quoting.
36 - * Return true if the buffer overflowed.
37 - */
38 -extern int add_to_string(char **ptrp, int *sizep, const char *str, int quote);
33 +extern void sq_quote_buf(struct strbuf *, const char *src);
34 +extern void sq_quote_argv(struct strbuf *, const char **argv, int count,
35 + size_t maxlen);
36
37 /* This unwraps what sq_quote() produces in place, but returns
38 * NULL if the input does not look like what sq_quote would have
rsh.c
+14 -18
@@ -10,12 +10,9 @@ int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
10 char *host;
11 char *path;
12 int sv[2];
13 - char command[COMMAND_SIZE];
14 - char *posn;
15 - int sizen;
16 - int of;
13 int i;
14 pid_t pid;
15 + struct strbuf cmd;
16
17 if (!strcmp(url, "-")) {
18 *fd_in = 0;
@@ -36,24 +33,23 @@ int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
33 if (!path) {
34 return error("Bad URL: %s", url);
35 }
36 +
37 /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */
40 - sizen = COMMAND_SIZE;
41 - posn = command;
42 - of = 0;
43 - of |= add_to_string(&posn, &sizen, "env ", 0);
44 - of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT "=", 0);
45 - of |= add_to_string(&posn, &sizen, path, 1);
46 - of |= add_to_string(&posn, &sizen, " ", 0);
47 - of |= add_to_string(&posn, &sizen, remote_prog, 1);
38 + strbuf_init(&cmd, COMMAND_SIZE);
39 + strbuf_addstr(&cmd, "env ");
40 + strbuf_addstr(&cmd, GIT_DIR_ENVIRONMENT "=");
41 + sq_quote_buf(&cmd, path);
42 + strbuf_addch(&cmd, ' ');
43 + sq_quote_buf(&cmd, remote_prog);
44
49 - for ( i = 0 ; i < rmt_argc ; i++ ) {
50 - of |= add_to_string(&posn, &sizen, " ", 0);
51 - of |= add_to_string(&posn, &sizen, rmt_argv[i], 1);
45 + for (i = 0 ; i < rmt_argc ; i++) {
46 + strbuf_addch(&cmd, ' ');
47 + sq_quote_buf(&cmd, rmt_argv[i]);
48 }
49
54 - of |= add_to_string(&posn, &sizen, " -", 0);
50 + strbuf_addstr(&cmd, " -");
51
56 - if ( of )
52 + if (cmd.len >= COMMAND_SIZE)
53 return error("Command line too long");
54
55 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
@@ -74,7 +70,7 @@ int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
70 close(sv[1]);
71 dup2(sv[0], 0);
72 dup2(sv[0], 1);
77 - execlp(ssh, ssh_basename, host, command, NULL);
73 + execlp(ssh, ssh_basename, host, cmd.buf, NULL);
74 }
75 close(sv[0]);
76 *fd_in = sv[1];
trace.c
+31 -20
@@ -64,7 +64,7 @@ static const char err_msg[] = "Could not trace into fd given by "
64
65 void trace_printf(const char *fmt, ...)
66 {
67 - char buf[8192];
67 + struct strbuf buf;
68 va_list ap;
69 int fd, len, need_close = 0;
70
@@ -72,12 +72,22 @@ void trace_printf(const char *fmt, ...)
72 if (!fd)
73 return;
74
75 + strbuf_init(&buf, 0);
76 va_start(ap, fmt);
76 - len = vsnprintf(buf, sizeof(buf), fmt, ap);
77 + len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
78 va_end(ap);
78 - if (len >= sizeof(buf))
79 - die("unreasonnable trace length");
80 - write_or_whine_pipe(fd, buf, len, err_msg);
79 + if (len >= strbuf_avail(&buf)) {
80 + strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
81 + va_start(ap, fmt);
82 + len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
83 + va_end(ap);
84 + if (len >= strbuf_avail(&buf))
85 + die("broken vsnprintf");
86 + }
87 + strbuf_setlen(&buf, len);
88 +
89 + write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
90 + strbuf_release(&buf);
91
92 if (need_close)
93 close(fd);
@@ -85,31 +95,32 @@ void trace_printf(const char *fmt, ...)
95
96 void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
97 {
88 - char buf[8192];
98 + struct strbuf buf;
99 va_list ap;
90 - char *argv_str;
91 - size_t argv_len;
100 int fd, len, need_close = 0;
101
102 fd = get_trace_fd(&need_close);
103 if (!fd)
104 return;
105
106 + strbuf_init(&buf, 0);
107 va_start(ap, fmt);
99 - len = vsnprintf(buf, sizeof(buf), fmt, ap);
108 + len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
109 va_end(ap);
101 - if (len >= sizeof(buf))
102 - die("unreasonnable trace length");
103 -
104 - /* Get the argv string. */
105 - argv_str = sq_quote_argv(argv, count);
106 - argv_len = strlen(argv_str);
107 -
108 - write_or_whine_pipe(fd, buf, len, err_msg);
109 - write_or_whine_pipe(fd, argv_str, argv_len, err_msg);
110 - write_or_whine_pipe(fd, "\n", 1, err_msg);
110 + if (len >= strbuf_avail(&buf)) {
111 + strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
112 + va_start(ap, fmt);
113 + len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
114 + va_end(ap);
115 + if (len >= strbuf_avail(&buf))
116 + die("broken vsnprintf");
117 + }
118 + strbuf_setlen(&buf, len);
119
112 - free(argv_str);
120 + sq_quote_argv(&buf, argv, count, 0);
121 + strbuf_addch(&buf, '\n');
122 + write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
123 + strbuf_release(&buf);
124
125 if (need_close)
126 close(fd);