]> granicus.if.org Git - pgbouncer/commitdiff
Make pga_ntop/pga_str non-failing and zero-terminating
authorMarko Kreen <markokr@gmail.com>
Tue, 25 Oct 2011 12:51:06 +0000 (15:51 +0300)
committerMarko Kreen <markokr@gmail.com>
Tue, 25 Oct 2011 12:51:06 +0000 (15:51 +0300)
This frees call sites from worring about it.

Also fix some buf sizes in admin.c

src/admin.c
src/pooler.c
src/util.c

index 4de06c8131301d97a1c1fc3bae5964ce3948ccd3..04dd66d7d97ff47d0b99b99c57dd99e660522140 100644 (file)
@@ -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) {
index 8513da08417e24b51c05c5958fa521d4f684c36e..4f766382e2a82638327cdf0a2cf1460a8803e046 100644 (file)
@@ -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;
index 025cc5a4570680c4e6beb64096ebd17dc1ec7964..285d34b00a6d83baadb7d22f3d0ccd3ad712dd59 100644 (file)
@@ -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;
 }