From: Marko Kreen Date: Tue, 25 Oct 2011 12:51:06 +0000 (+0300) Subject: Make pga_ntop/pga_str non-failing and zero-terminating X-Git-Tag: pgbouncer_1_5_rc1~39 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e0b51e615f134baf999844adc5ee9607a4f826e0;p=pgbouncer Make pga_ntop/pga_str non-failing and zero-terminating This frees call sites from worring about it. Also fix some buf sizes in admin.c --- diff --git a/src/admin.c b/src/admin.c index 4de06c8..04dd66d 100644 --- a/src/admin.c +++ b/src/admin.c @@ -523,7 +523,7 @@ static void socket_row(PktBuf *buf, PgSocket *sk, const char *state, bool debug) { int pkt_avail = 0, send_avail = 0; char ptrbuf[128], linkbuf[128]; - char l_addr[32], r_addr[32]; + char l_addr[PGADDR_BUF], r_addr[PGADDR_BUF]; IOBuf *io = sk->sbuf.io; if (io) { diff --git a/src/pooler.c b/src/pooler.c index 8513da0..4f76638 100644 --- a/src/pooler.c +++ b/src/pooler.c @@ -244,18 +244,14 @@ static void err_wait_func(int sock, short flags, void *arg) static const char *addrpair(const PgAddr *src, const PgAddr *dst) { - static char ip1buf[INET6_ADDRSTRLEN], ip2buf[INET6_ADDRSTRLEN], - buf[INET6_ADDRSTRLEN+INET6_ADDRSTRLEN+128]; + static char ip1buf[PGADDR_BUF], ip2buf[PGADDR_BUF], + buf[2*PGADDR_BUF + 16]; const char *ip1, *ip2; if (pga_is_unix(src)) return "unix->unix"; ip1 = pga_ntop(src, ip1buf, sizeof(ip1buf)); - if (!ip1) - ip1 = strerror(errno); ip2 = pga_ntop(src, ip2buf, sizeof(ip2buf)); - if (!ip2) - ip2 = strerror(errno); snprintf(buf, sizeof(buf), "%s:%d -> %s:%d", ip1, pga_port(src), ip2, pga_port(dst)); return buf; diff --git a/src/util.c b/src/util.c index 025cc5a..285d34b 100644 --- a/src/util.c +++ b/src/util.c @@ -42,10 +42,7 @@ int log_socket_prefix(enum LogLevel lev, void *ctx, char *dst, unsigned int dstl if (pga_is_unix(&sock->remote_addr)) { host = "unix"; } else { - memset(host6, 0, sizeof(host6)); host = pga_ntop(&sock->remote_addr, host6, sizeof(host6)); - if (!host) - host = "(ntop-err)"; } port = pga_port(&sock->remote_addr); @@ -369,17 +366,29 @@ int pga_cmp_addr(const PgAddr *a, const PgAddr *b) /* convert pgaddr to string */ const char *pga_ntop(const PgAddr *a, char *dst, int dstlen) { + const char *res = NULL; + char buf[PGADDR_BUF]; + + memset(buf, 0, sizeof(buf)); + switch (pga_family(a)) { case AF_UNIX: - strlcpy(dst, "unix", dstlen); - return dst; + res = "unix"; + break; case AF_INET: - return inet_ntop(AF_INET, &a->sin.sin_addr, dst, dstlen); + res = inet_ntop(AF_INET, &a->sin.sin_addr, buf, sizeof(buf)); + break; case AF_INET6: - return inet_ntop(AF_INET6, &a->sin6.sin6_addr, dst, dstlen); + res = inet_ntop(AF_INET6, &a->sin6.sin6_addr, buf, sizeof(buf)); + break; default: - return NULL; + res = "(bad-af)"; } + if (res == NULL) + res = "(err-ntop)"; + + strlcpy(dst, res, dstlen); + return dst; } /* parse address from string */ @@ -405,8 +414,7 @@ bool pga_pton(PgAddr *a, const char *s, int port) const char *pga_str(const PgAddr *a, char *dst, int dstlen) { char buf[PGADDR_BUF]; - if (!pga_ntop(a, buf, sizeof(buf))) - return NULL; + pga_ntop(a, buf, sizeof(buf)); snprintf(dst, dstlen, "%s@%d", buf, pga_port(a)); return dst; }