daemon: use cld->env_array when re-spawning
This avoids an ugly strcat into a fixed-size buffer. It's not wrong (the buffer is plenty large enough for an IPv6 address plus some minor formatting), but it takes some effort to verify that. Unfortunately we are still stuck with some fixed-size buffers to hold the output of inet_ntop. But at least we now pass very easy-to-verify parameters, rather than doing a manual computation to account for other data in the buffer. As a side effect, this also fixes the case where we might pass an uninitialized portbuf buffer through the environment. This probably couldn't happen in practice, as it would mean that addr->sa_family was neither AF_INET nor AF_INET6 (and that is all we are listening on). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
							parent
							
								
									0b282cc4b2
								
							
						
					
					
						commit
						f063d38b80
					
				
							
								
								
									
										26
									
								
								daemon.c
								
								
								
								
							
							
						
						
									
										26
									
								
								daemon.c
								
								
								
								
							|  | @ -811,8 +811,6 @@ static char **cld_argv; | ||||||
| static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen) | static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen) | ||||||
| { | { | ||||||
| 	struct child_process cld = CHILD_PROCESS_INIT; | 	struct child_process cld = CHILD_PROCESS_INIT; | ||||||
| 	char addrbuf[300] = "REMOTE_ADDR=", portbuf[300]; |  | ||||||
| 	char *env[] = { addrbuf, portbuf, NULL }; |  | ||||||
|  |  | ||||||
| 	if (max_connections && live_children >= max_connections) { | 	if (max_connections && live_children >= max_connections) { | ||||||
| 		kill_some_child(); | 		kill_some_child(); | ||||||
|  | @ -826,27 +824,23 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (addr->sa_family == AF_INET) { | 	if (addr->sa_family == AF_INET) { | ||||||
|  | 		char buf[128] = ""; | ||||||
| 		struct sockaddr_in *sin_addr = (void *) addr; | 		struct sockaddr_in *sin_addr = (void *) addr; | ||||||
| 		inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf + 12, | 		inet_ntop(addr->sa_family, &sin_addr->sin_addr, buf, sizeof(buf)); | ||||||
| 		    sizeof(addrbuf) - 12); | 		argv_array_pushf(&cld.env_array, "REMOTE_ADDR=%s", buf); | ||||||
| 		snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d", | 		argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d", | ||||||
| 		    ntohs(sin_addr->sin_port)); | 				 ntohs(sin_addr->sin_port)); | ||||||
| #ifndef NO_IPV6 | #ifndef NO_IPV6 | ||||||
| 	} else if (addr->sa_family == AF_INET6) { | 	} else if (addr->sa_family == AF_INET6) { | ||||||
|  | 		char buf[128] = ""; | ||||||
| 		struct sockaddr_in6 *sin6_addr = (void *) addr; | 		struct sockaddr_in6 *sin6_addr = (void *) addr; | ||||||
|  | 		inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(buf)); | ||||||
| 		char *buf = addrbuf + 12; | 		argv_array_pushf(&cld.env_array, "REMOTE_ADDR=[%s]", buf); | ||||||
| 		*buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ | 		argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d", | ||||||
| 		inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, | 				 ntohs(sin6_addr->sin6_port)); | ||||||
| 		    sizeof(addrbuf) - 13); |  | ||||||
| 		strcat(buf, "]"); |  | ||||||
|  |  | ||||||
| 		snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d", |  | ||||||
| 		    ntohs(sin6_addr->sin6_port)); |  | ||||||
| #endif | #endif | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	cld.env = (const char **)env; |  | ||||||
| 	cld.argv = (const char **)cld_argv; | 	cld.argv = (const char **)cld_argv; | ||||||
| 	cld.in = incoming; | 	cld.in = incoming; | ||||||
| 	cld.out = dup(incoming); | 	cld.out = dup(incoming); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Jeff King
						Jeff King