From: Fabian Keil <fk@fabiankeil.de>
Date: Sat, 5 Dec 2020 13:12:25 +0000 (+0100)
Subject: socks5_connect: Fix compiler warning from GCC9 with -D_FORTIFY_SOURCE=2
X-Git-Tag: v_3_0_30~271^2~64
X-Git-Url: http://www.privoxy.org/gitweb/%22https:/static/gitweb.js?a=commitdiff_plain;h=b402d02197854c83609bb59de1365b1136703d8a;p=privoxy.git

socks5_connect: Fix compiler warning from GCC9 with -D_FORTIFY_SOURCE=2

    gateway.c: In function 'socks5_connect':
    gateway.c:1249:4: warning: 'strncpy' specified bound 295 equals destination size [-Wstringop-truncation]
     1249 |    strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
          |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The warning is harmless because 'target_host' is checked to
be at most 255 bytes long.

Reported by Lee.
---

diff --git a/gateway.c b/gateway.c
index b341ae95..135b2173 100644
--- a/gateway.c
+++ b/gateway.c
@@ -1246,7 +1246,7 @@ static jb_socket socks5_connect(const struct forward_spec *fwd,
    cbuf[client_pos++] = (char)(hostlen & 0xffu);
    assert(sizeof(cbuf) - client_pos > (size_t)255);
    /* Using strncpy because we really want the nul byte padding. */
-   strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
+   strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos - 1);
    client_pos += (hostlen & 0xffu);
    cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
    cbuf[client_pos++] = (char)((target_port     ) & 0xff);