]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Convert dump_buf_hex() to use oc_text_buf instead of sprintf
authorDavid Woodhouse <dwmw2@infradead.org>
Fri, 7 Jun 2019 16:04:41 +0000 (17:04 +0100)
committerDavid Woodhouse <dwmw2@infradead.org>
Fri, 7 Jun 2019 19:29:37 +0000 (20:29 +0100)
I seem to recall the OpenBSD build will complain loudly about the use of
"bad" functions like sprintf. And even though this particular code does
seem to be perfectly correct, they do have a point in the general case.

Just use buf_append() for this, since that's what it was designed for.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
http.c

diff --git a/http.c b/http.c
index 243553e0cb56a5ae6f382e7cbdb60708d38cdcf3..434995052b9306f567c848ee8a9b14b8058b8982 100644 (file)
--- a/http.c
+++ b/http.c
@@ -797,23 +797,30 @@ void dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf)
 
 void dump_buf_hex(struct openconnect_info *vpninfo, int loglevel, char prefix, unsigned char *buf, int len)
 {
-       char linebuf[80];
+       struct oc_text_buf *line = buf_alloc();
        int i, j;
 
        for (i = 0; i < len; i+=16) {
-               sprintf(linebuf, "%04x:", i);
+               buf_truncate(line);
+               buf_append(line, "%04x:", i);
                for (j = i; j < i+16; j++) {
+                       if (!(j & 7))
+                               buf_append(line, " ");
+
                        if (j < len)
-                               sprintf(linebuf + strlen(linebuf), " %02x", buf[j]);
+                               buf_append(line, " %02x", buf[j]);
                        else
-                               sprintf(linebuf + strlen(linebuf), "   ");
+                               buf_append(line, "   ");
                }
-               sprintf(linebuf + strlen(linebuf), " |");
+               buf_append(line, "  |");
                for (j = i; j < i+16 && j < len; j++)
-                       sprintf(linebuf + strlen(linebuf), "%c", isprint(buf[j]) || buf[j]==' ' ? buf[j] : '.');
-               sprintf(linebuf + strlen(linebuf), "|");
-               vpn_progress(vpninfo, loglevel, "%c %s\n", prefix, linebuf);
+                       buf_append(line, "%c", isprint(buf[j])? buf[j] : '.');
+               buf_append(line, "|");
+               if (buf_error(line))
+                       break;
+               vpn_progress(vpninfo, loglevel, "%c %s\n", prefix, line->data);
        }
+       buf_free(line);
 }
 
 /* Inputs: