daemon: use strbuf for hostname info

Convert hostname, canon_hostname, ip_address and tcp_port to strbuf. This allows to get rid of the helpers strbuf_addstr_or_null() and STRARG because a strbuf always represents a valid (initially empty) string. sanitize_client() is not needed anymore and sanitize_client_strbuf() takes its place and name. Helped-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 Mar 7, 2015 at 11:50 UTC 7a646cec5bbcebad31a6e99f0279782b322ecd36
1 file changed +41 -57
daemon.c
+41 -57
@@ -56,10 +56,10 @@ static const char *user_path;
56 static unsigned int timeout;
57 static unsigned int init_timeout;
58
59 -static char *hostname;
60 -static char *canon_hostname;
61 -static char *ip_address;
62 -static char *tcp_port;
59 +static struct strbuf hostname = STRBUF_INIT;
60 +static struct strbuf canon_hostname = STRBUF_INIT;
61 +static struct strbuf ip_address = STRBUF_INIT;
62 +static struct strbuf tcp_port = STRBUF_INIT;
63
64 static int hostname_lookup_done;
65
@@ -68,13 +68,13 @@ static void lookup_hostname(void);
68 static const char *get_canon_hostname(void)
69 {
70 lookup_hostname();
71 - return canon_hostname;
71 + return canon_hostname.buf;
72 }
73
74 static const char *get_ip_address(void)
75 {
76 lookup_hostname();
77 - return ip_address;
77 + return ip_address.buf;
78 }
79
80 static void logreport(int priority, const char *err, va_list params)
@@ -122,12 +122,6 @@ static void NORETURN daemon_die(const char *err, va_list params)
122 exit(1);
123 }
124
125 -static void strbuf_addstr_or_null(struct strbuf *sb, const char *s)
126 -{
127 - if (s)
128 - strbuf_addstr(sb, s);
129 -}
130 -
125 struct expand_path_context {
126 const char *directory;
127 };
@@ -138,22 +132,22 @@ static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
132
133 switch (placeholder[0]) {
134 case 'H':
141 - strbuf_addstr_or_null(sb, hostname);
135 + strbuf_addbuf(sb, &hostname);
136 return 1;
137 case 'C':
138 if (placeholder[1] == 'H') {
145 - strbuf_addstr_or_null(sb, get_canon_hostname());
139 + strbuf_addstr(sb, get_canon_hostname());
140 return 2;
141 }
142 break;
143 case 'I':
144 if (placeholder[1] == 'P') {
151 - strbuf_addstr_or_null(sb, get_ip_address());
145 + strbuf_addstr(sb, get_ip_address());
146 return 2;
147 }
148 break;
149 case 'P':
156 - strbuf_addstr_or_null(sb, tcp_port);
150 + strbuf_addbuf(sb, &tcp_port);
151 return 1;
152 case 'D':
153 strbuf_addstr(sb, context->directory);
@@ -301,16 +295,14 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
295 char *eol;
296 int seen_errors = 0;
297
304 -#define STRARG(x) ((x) ? (x) : "")
298 *arg++ = access_hook;
299 *arg++ = service->name;
300 *arg++ = path;
308 - *arg++ = STRARG(hostname);
309 - *arg++ = STRARG(get_canon_hostname());
310 - *arg++ = STRARG(get_ip_address());
311 - *arg++ = STRARG(tcp_port);
301 + *arg++ = hostname.buf;
302 + *arg++ = get_canon_hostname();
303 + *arg++ = get_ip_address();
304 + *arg++ = tcp_port.buf;
305 *arg = NULL;
313 -#undef STRARG
306
307 child.use_shell = 1;
308 child.argv = argv;
@@ -542,7 +534,7 @@ static void parse_host_and_port(char *hostport, char **host,
534 * trailing and leading dots, which means that the client cannot escape
535 * our base path via ".." traversal.
536 */
545 -static void sanitize_client_strbuf(struct strbuf *out, const char *in)
537 +static void sanitize_client(struct strbuf *out, const char *in)
538 {
539 for (; *in; in++) {
540 if (*in == '/')
@@ -556,23 +548,14 @@ static void sanitize_client_strbuf(struct strbuf *out, const char *in)
548 strbuf_setlen(out, out->len - 1);
549 }
550
559 -static char *sanitize_client(const char *in)
560 -{
561 - struct strbuf out = STRBUF_INIT;
562 - sanitize_client_strbuf(&out, in);
563 - return strbuf_detach(&out, NULL);
564 -}
565 -
551 /*
552 * Like sanitize_client, but we also perform any canonicalization
553 * to make life easier on the admin.
554 */
570 -static char *canonicalize_client(const char *in)
555 +static void canonicalize_client(struct strbuf *out, const char *in)
556 {
572 - struct strbuf out = STRBUF_INIT;
573 - sanitize_client_strbuf(&out, in);
574 - strbuf_tolower(&out);
575 - return strbuf_detach(&out, NULL);
557 + sanitize_client(out, in);
558 + strbuf_tolower(out);
559 }
560
561 /*
@@ -595,11 +578,11 @@ static void parse_host_arg(char *extra_args, int buflen)
578 char *port;
579 parse_host_and_port(val, &host, &port);
580 if (port) {
598 - free(tcp_port);
599 - tcp_port = sanitize_client(port);
581 + strbuf_reset(&tcp_port);
582 + sanitize_client(&tcp_port, port);
583 }
601 - free(hostname);
602 - hostname = canonicalize_client(host);
584 + strbuf_reset(&hostname);
585 + canonicalize_client(&hostname, host);
586 hostname_lookup_done = 0;
587 }
588
@@ -616,7 +599,7 @@ static void parse_host_arg(char *extra_args, int buflen)
599 */
600 static void lookup_hostname(void)
601 {
619 - if (!hostname_lookup_done && hostname) {
602 + if (!hostname_lookup_done && hostname.len) {
603 #ifndef NO_IPV6
604 struct addrinfo hints;
605 struct addrinfo *ai;
@@ -626,19 +609,21 @@ static void lookup_hostname(void)
609 memset(&hints, 0, sizeof(hints));
610 hints.ai_flags = AI_CANONNAME;
611
629 - gai = getaddrinfo(hostname, NULL, &hints, &ai);
612 + gai = getaddrinfo(hostname.buf, NULL, &hints, &ai);
613 if (!gai) {
614 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
615
616 inet_ntop(AF_INET, &sin_addr->sin_addr,
617 addrbuf, sizeof(addrbuf));
635 - free(ip_address);
636 - ip_address = xstrdup(addrbuf);
618 + strbuf_reset(&ip_address);
619 + strbuf_addstr(&ip_address, addrbuf);
620
638 - free(canon_hostname);
639 - canon_hostname = ai->ai_canonname ?
640 - sanitize_client(ai->ai_canonname) :
641 - xstrdup(ip_address);
621 + strbuf_reset(&canon_hostname);
622 + if (ai->ai_canonname)
623 + sanitize_client(&canon_hostname,
624 + ai->ai_canonname);
625 + else
626 + strbuf_addbuf(&canon_hostname, &ip_address);
627
628 freeaddrinfo(ai);
629 }
@@ -648,7 +633,7 @@ static void lookup_hostname(void)
633 char **ap;
634 static char addrbuf[HOST_NAME_MAX + 1];
635
651 - hent = gethostbyname(hostname);
636 + hent = gethostbyname(hostname.buf);
637 if (hent) {
638 ap = hent->h_addr_list;
639 memset(&sa, 0, sizeof sa);
@@ -659,10 +644,10 @@ static void lookup_hostname(void)
644 inet_ntop(hent->h_addrtype, &sa.sin_addr,
645 addrbuf, sizeof(addrbuf));
646
662 - free(canon_hostname);
663 - canon_hostname = sanitize_client(hent->h_name);
664 - free(ip_address);
665 - ip_address = xstrdup(addrbuf);
647 + strbuf_reset(&canon_hostname);
648 + sanitize_client(&canon_hostname, hent->h_name);
649 + strbuf_reset(&ip_address);
650 + strbuf_addstr(&ip_address, addrbuf);
651 }
652 #endif
653 hostname_lookup_done = 1;
@@ -693,11 +678,10 @@ static int execute(void)
678 pktlen--;
679 }
680
696 - free(hostname);
697 - free(canon_hostname);
698 - free(ip_address);
699 - free(tcp_port);
700 - hostname = canon_hostname = ip_address = tcp_port = NULL;
681 + strbuf_release(&hostname);
682 + strbuf_release(&canon_hostname);
683 + strbuf_release(&ip_address);
684 + strbuf_release(&tcp_port);
685
686 if (len != pktlen)
687 parse_host_arg(line + len + 1, pktlen - len - 1);