Use libc snprintf and nstr{cpy,cat}.

We don't need async-safe snprintf anymore, and nstr{cpy,cat} are
more ergonomic and efficient where they can used.
This commit is contained in:
Nicholas J. Kain 2022-08-09 14:17:31 -04:00
parent 898765e0a8
commit 9338aa37c2
11 changed files with 637 additions and 2566 deletions

1070
cfg.c

File diff suppressed because it is too large Load Diff

12
cfg.rl
View File

@ -13,16 +13,15 @@
#include "ndhc.h" #include "ndhc.h"
#include "ifchd.h" #include "ifchd.h"
#include "sockd.h" #include "sockd.h"
#include "nk/stb_sprintf.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/nstrcpy.h"
#include "nk/privs.h" #include "nk/privs.h"
#include "nk/io.h" #include "nk/io.h"
static void copy_cmdarg(char *dest, const char *src, static void copy_cmdarg(char *dest, const char *src,
size_t destlen, const char *argname) size_t destlen, const char *argname)
{ {
ssize_t olen = stbsp_snprintf(dest, destlen, "%s", src); if (!nstrcpy(dest, destlen, src))
if (olen < 0 || (size_t)olen > destlen)
suicide("snprintf failed on %s", argname); suicide("snprintf failed on %s", argname);
} }
@ -331,11 +330,8 @@ void parse_cmdline(int argc, char *argv[])
size_t argbl = 0; size_t argbl = 0;
for (size_t i = 1; i < (size_t)argc; ++i) { for (size_t i = 1; i < (size_t)argc; ++i) {
ssize_t snl; ssize_t snl;
if (i > 1) if (i > 1) snl = snprintf(argb + argbl, sizeof argb - argbl, "%c%s", 0, argv[i]);
snl = stbsp_snprintf(argb + argbl, sizeof argb - argbl, "%c%s", else snl = snprintf(argb + argbl, sizeof argb - argbl, "%s", argv[i]);
0, argv[i]);
else
snl = stbsp_snprintf(argb + argbl, sizeof argb - argbl, "%s", argv[i]);
if (snl < 0 || (size_t)snl > sizeof argb) if (snl < 0 || (size_t)snl > sizeof argb)
suicide("error parsing command line option: option too long"); suicide("error parsing command line option: option too long");
argbl += (size_t)snl; argbl += (size_t)snl;

View File

@ -10,7 +10,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <errno.h> #include <errno.h>
#include "nk/stb_sprintf.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/random.h" #include "nk/random.h"
#include "nk/io.h" #include "nk/io.h"
@ -19,7 +18,7 @@
static void get_duid_path(char *duidfile, size_t dlen) static void get_duid_path(char *duidfile, size_t dlen)
{ {
int splen = stbsp_snprintf(duidfile, dlen, "%s/DUID", state_dir); int splen = snprintf(duidfile, dlen, "%s/DUID", state_dir);
if (splen < 0 || (size_t)splen > dlen) if (splen < 0 || (size_t)splen > dlen)
suicide("%s: snprintf failed; return=%d", __func__, splen); suicide("%s: snprintf failed; return=%d", __func__, splen);
} }
@ -30,7 +29,7 @@ static void get_iaid_path(char *iaidfile, size_t ilen,
if (hwaddrlen != 6) if (hwaddrlen != 6)
suicide("%s: Hardware address length=%zu != 6 bytes", suicide("%s: Hardware address length=%zu != 6 bytes",
__func__, hwaddrlen); __func__, hwaddrlen);
int splen = stbsp_snprintf int splen = snprintf
(iaidfile, ilen, (iaidfile, ilen,
"%s/IAID-%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", "%s/IAID-%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
state_dir, hwaddr[0], hwaddr[1], hwaddr[2], state_dir, hwaddr[0], hwaddr[1], hwaddr[2],

View File

@ -9,7 +9,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include "nk/stb_sprintf.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/io.h" #include "nk/io.h"
#include "options.h" #include "options.h"
@ -40,8 +39,7 @@ static int ifcmd_raw(char *buf, size_t buflen, const char *optname,
return -1; return -1;
} }
int ioptlen = (int)optlen; int ioptlen = (int)optlen;
ssize_t olen = stbsp_snprintf(buf, buflen, "%s:%.*s;", ssize_t olen = snprintf(buf, buflen, "%s:%.*s;", optname, ioptlen, optdata);
optname, ioptlen, optdata);
if (olen < 0 || (size_t)olen > buflen) { if (olen < 0 || (size_t)olen > buflen) {
log_line("%s: (%s) '%s' option would truncate, so it was dropped.", log_line("%s: (%s) '%s' option would truncate, so it was dropped.",
client_config.interface, __func__, optname); client_config.interface, __func__, optname);
@ -64,7 +62,7 @@ static int ifcmd_u8(char *buf, size_t buflen, const char *optname,
return -1; return -1;
char numbuf[16]; char numbuf[16];
uint8_t c = optdata[0]; uint8_t c = optdata[0];
ssize_t olen = stbsp_snprintf(numbuf, sizeof numbuf, "%c", c); ssize_t olen = snprintf(numbuf, sizeof numbuf, "%c", c);
if (olen < 0 || (size_t)olen > sizeof numbuf) if (olen < 0 || (size_t)olen > sizeof numbuf)
return -1; return -1;
return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf)); return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf));
@ -79,7 +77,7 @@ static int ifcmd_u16(char *buf, size_t buflen, const char *optname,
uint16_t v; uint16_t v;
memcpy(&v, optdata, 2); memcpy(&v, optdata, 2);
v = ntohs(v); v = ntohs(v);
ssize_t olen = stbsp_snprintf(numbuf, sizeof numbuf, "%hu", v); ssize_t olen = snprintf(numbuf, sizeof numbuf, "%hu", v);
if (olen < 0 || (size_t)olen > sizeof numbuf) if (olen < 0 || (size_t)olen > sizeof numbuf)
return -1; return -1;
return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf)); return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf));
@ -94,7 +92,7 @@ static int ifcmd_s32(char *buf, size_t buflen, const char *optname,
uint32_t v; uint32_t v;
memcpy(&v, optdata, 4); memcpy(&v, optdata, 4);
v = ntohl(v); v = ntohl(v);
ssize_t olen = stbsp_snprintf(numbuf, sizeof numbuf, "%d", v); ssize_t olen = snprintf(numbuf, sizeof numbuf, "%d", v);
if (olen < 0 || (size_t)olen > sizeof numbuf) if (olen < 0 || (size_t)olen > sizeof numbuf)
return -1; return -1;
return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf)); return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf));
@ -122,14 +120,14 @@ static int ifcmd_iplist(char *out, size_t outlen, const char *optname,
return -1; return -1;
inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf); inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf);
ssize_t wc = stbsp_snprintf(buf + bufoff, sizeof buf, "%s", ipbuf); ssize_t wc = snprintf(buf + bufoff, sizeof buf, "%s", ipbuf);
if (wc < 0 || (size_t)wc > sizeof buf) if (wc < 0 || (size_t)wc > sizeof buf)
return -1; return -1;
optoff += 4; optoff += 4;
bufoff += (size_t)wc; bufoff += (size_t)wc;
while (optlen >= 4 + optoff) { while (optlen >= 4 + optoff) {
inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf); inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf);
wc = stbsp_snprintf(buf + bufoff, sizeof buf, ",%s", ipbuf); wc = snprintf(buf + bufoff, sizeof buf, ",%s", ipbuf);
if (wc < 0 || (size_t)wc > sizeof buf) if (wc < 0 || (size_t)wc > sizeof buf)
return -1; return -1;
optoff += 4; optoff += 4;
@ -193,20 +191,18 @@ static int ifchwrite(const char *buf, size_t count)
bool ifchange_carrier_isup(void) bool ifchange_carrier_isup(void)
{ {
char buf[256]; const char buf[] = "carrier:;";
stbsp_snprintf(buf, sizeof buf, "carrier:;");
return ifchwrite(buf, strlen(buf)) == 0; return ifchwrite(buf, strlen(buf)) == 0;
} }
int ifchange_deconfig(struct client_state_t *cs) int ifchange_deconfig(struct client_state_t *cs)
{ {
char buf[256]; const char buf[] = "ip4:0.0.0.0,255.255.255.255;";
int ret = -1; int ret = -1;
if (cs->ifDeconfig) if (cs->ifDeconfig)
return 0; return 0;
stbsp_snprintf(buf, sizeof buf, "ip4:0.0.0.0,255.255.255.255;");
log_line("%s: Resetting IP configuration.", client_config.interface); log_line("%s: Resetting IP configuration.", client_config.interface);
ret = ifchwrite(buf, strlen(buf)); ret = ifchwrite(buf, strlen(buf));
@ -266,9 +262,9 @@ static size_t send_client_ip(char *out, size_t olen,
int snlen; int snlen;
if (have_bcast) { if (have_bcast) {
snlen = stbsp_snprintf(out, olen, "ip4:%s,%s,%s;", ip, sn, bc); snlen = snprintf(out, olen, "ip4:%s,%s,%s;", ip, sn, bc);
} else { } else {
snlen = stbsp_snprintf(out, olen, "ip4:%s,%s;", ip, sn); snlen = snprintf(out, olen, "ip4:%s,%s;", ip, sn);
} }
if (snlen < 0 || (size_t)snlen > olen) { if (snlen < 0 || (size_t)snlen > olen) {
log_line("%s: (%s) ip4 command would truncate so it was dropped.", log_line("%s: (%s) ip4 command would truncate so it was dropped.",

View File

@ -6,8 +6,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "nk/stb_sprintf.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/nstrcpy.h"
#include "ifchd-parse.h" #include "ifchd-parse.h"
#include "ifchd.h" #include "ifchd.h"
#include "ifset.h" #include "ifset.h"
@ -1187,10 +1187,14 @@ int execute_buffer(const char *newbuf)
char tb[MAX_BUF]; char tb[MAX_BUF];
int cmdf = 0; int cmdf = 0;
ssize_t buflen = stbsp_snprintf(buf, sizeof buf, "%s%s", cl.ibuf, newbuf); char *snp = nstrcpy(buf, sizeof buf, cl.ibuf);
memset(cl.ibuf, 0, sizeof cl.ibuf); memset(cl.ibuf, 0, sizeof cl.ibuf);
if (buflen < 0 || (size_t)buflen > sizeof buf) { if (!snp) {
log_line("%s: (%s) snprintf1 failed", client_config.interface, __func__); log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
return -99;
}
if (!nstrcat(buf, sizeof buf, newbuf)) {
log_line("%s: (%s) nstrcat failed", client_config.interface, __func__);
return -99; return -99;
} }
@ -1202,15 +1206,15 @@ int execute_buffer(const char *newbuf)
int cs = 0; int cs = 0;
#line 1206 "ifchd-parse.c" #line 1210 "ifchd-parse.c"
{ {
cs = (int)ifchd_parser_start; cs = (int)ifchd_parser_start;
} }
#line 188 "ifchd-parse.rl" #line 192 "ifchd-parse.rl"
#line 1214 "ifchd-parse.c" #line 1218 "ifchd-parse.c"
{ {
switch ( cs ) { switch ( cs ) {
case 1: case 1:
@ -1513,7 +1517,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 1517 "ifchd-parse.c" #line 1521 "ifchd-parse.c"
goto _st2; goto _st2;
_st2: _st2:
@ -1593,7 +1597,7 @@ int execute_buffer(const char *newbuf)
#line 155 "ifchd-parse.rl" #line 155 "ifchd-parse.rl"
cl.state = STATE_CARRIER; } cl.state = STATE_CARRIER; }
#line 1597 "ifchd-parse.c" #line 1601 "ifchd-parse.c"
{ {
#line 105 "ifchd-parse.rl" #line 105 "ifchd-parse.rl"
@ -1624,7 +1628,7 @@ int execute_buffer(const char *newbuf)
cmdf |= pr; cmdf |= pr;
} }
#line 1628 "ifchd-parse.c" #line 1632 "ifchd-parse.c"
goto _st126; goto _st126;
_ctr39: _ctr39:
@ -1641,7 +1645,7 @@ int execute_buffer(const char *newbuf)
tb[arg_len] = 0; tb[arg_len] = 0;
} }
#line 1645 "ifchd-parse.c" #line 1649 "ifchd-parse.c"
{ {
#line 105 "ifchd-parse.rl" #line 105 "ifchd-parse.rl"
@ -1672,7 +1676,7 @@ int execute_buffer(const char *newbuf)
cmdf |= pr; cmdf |= pr;
} }
#line 1676 "ifchd-parse.c" #line 1680 "ifchd-parse.c"
goto _st126; goto _st126;
_st126: _st126:
@ -1718,7 +1722,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 1722 "ifchd-parse.c" #line 1726 "ifchd-parse.c"
goto _st10; goto _st10;
_st10: _st10:
@ -1767,13 +1771,13 @@ int execute_buffer(const char *newbuf)
#line 144 "ifchd-parse.rl" #line 144 "ifchd-parse.rl"
cl.state = STATE_DNS; } cl.state = STATE_DNS; }
#line 1771 "ifchd-parse.c" #line 1775 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 1777 "ifchd-parse.c" #line 1781 "ifchd-parse.c"
goto _st14; goto _st14;
_ctr115: _ctr115:
@ -1781,13 +1785,13 @@ int execute_buffer(const char *newbuf)
#line 145 "ifchd-parse.rl" #line 145 "ifchd-parse.rl"
cl.state = STATE_LPRSVR; } cl.state = STATE_LPRSVR; }
#line 1785 "ifchd-parse.c" #line 1789 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 1791 "ifchd-parse.c" #line 1795 "ifchd-parse.c"
goto _st14; goto _st14;
_ctr126: _ctr126:
@ -1795,13 +1799,13 @@ int execute_buffer(const char *newbuf)
#line 146 "ifchd-parse.rl" #line 146 "ifchd-parse.rl"
cl.state = STATE_NTPSVR; } cl.state = STATE_NTPSVR; }
#line 1799 "ifchd-parse.c" #line 1803 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 1805 "ifchd-parse.c" #line 1809 "ifchd-parse.c"
goto _st14; goto _st14;
_ctr148: _ctr148:
@ -1809,13 +1813,13 @@ int execute_buffer(const char *newbuf)
#line 147 "ifchd-parse.rl" #line 147 "ifchd-parse.rl"
cl.state = STATE_WINS; } cl.state = STATE_WINS; }
#line 1813 "ifchd-parse.c" #line 1817 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 1819 "ifchd-parse.c" #line 1823 "ifchd-parse.c"
goto _st14; goto _st14;
_st14: _st14:
@ -2038,13 +2042,13 @@ int execute_buffer(const char *newbuf)
#line 150 "ifchd-parse.rl" #line 150 "ifchd-parse.rl"
cl.state = STATE_DOMAIN; } cl.state = STATE_DOMAIN; }
#line 2042 "ifchd-parse.c" #line 2046 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 2048 "ifchd-parse.c" #line 2052 "ifchd-parse.c"
goto _st33; goto _st33;
_ctr53: _ctr53:
@ -2052,13 +2056,13 @@ int execute_buffer(const char *newbuf)
#line 149 "ifchd-parse.rl" #line 149 "ifchd-parse.rl"
cl.state = STATE_HOSTNAME; } cl.state = STATE_HOSTNAME; }
#line 2056 "ifchd-parse.c" #line 2060 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 2062 "ifchd-parse.c" #line 2066 "ifchd-parse.c"
goto _st33; goto _st33;
_st33: _st33:
@ -2080,7 +2084,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2084 "ifchd-parse.c" #line 2088 "ifchd-parse.c"
goto _st34; goto _st34;
_st34: _st34:
@ -2138,7 +2142,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2142 "ifchd-parse.c" #line 2146 "ifchd-parse.c"
goto _st39; goto _st39;
_st39: _st39:
@ -2187,13 +2191,13 @@ int execute_buffer(const char *newbuf)
#line 143 "ifchd-parse.rl" #line 143 "ifchd-parse.rl"
cl.state = STATE_IP4SET; } cl.state = STATE_IP4SET; }
#line 2191 "ifchd-parse.c" #line 2195 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 2197 "ifchd-parse.c" #line 2201 "ifchd-parse.c"
goto _st43; goto _st43;
_st43: _st43:
@ -2374,13 +2378,13 @@ int execute_buffer(const char *newbuf)
#line 142 "ifchd-parse.rl" #line 142 "ifchd-parse.rl"
cl.state = STATE_ROUTER; } cl.state = STATE_ROUTER; }
#line 2378 "ifchd-parse.c" #line 2382 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 2384 "ifchd-parse.c" #line 2388 "ifchd-parse.c"
goto _st59; goto _st59;
_st59: _st59:
@ -2475,13 +2479,13 @@ int execute_buffer(const char *newbuf)
#line 154 "ifchd-parse.rl" #line 154 "ifchd-parse.rl"
cl.state = STATE_IPTTL; } cl.state = STATE_IPTTL; }
#line 2479 "ifchd-parse.c" #line 2483 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 2485 "ifchd-parse.c" #line 2489 "ifchd-parse.c"
goto _st67; goto _st67;
_st67: _st67:
@ -2772,7 +2776,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2776 "ifchd-parse.c" #line 2780 "ifchd-parse.c"
goto _st94; goto _st94;
_st94: _st94:
@ -2816,7 +2820,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2820 "ifchd-parse.c" #line 2824 "ifchd-parse.c"
goto _st98; goto _st98;
_st98: _st98:
@ -2857,13 +2861,13 @@ int execute_buffer(const char *newbuf)
#line 153 "ifchd-parse.rl" #line 153 "ifchd-parse.rl"
cl.state = STATE_MTU; } cl.state = STATE_MTU; }
#line 2861 "ifchd-parse.c" #line 2865 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 2867 "ifchd-parse.c" #line 2871 "ifchd-parse.c"
goto _st102; goto _st102;
_st102: _st102:
@ -2877,7 +2881,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2881 "ifchd-parse.c" #line 2885 "ifchd-parse.c"
goto _st103; goto _st103;
_st103: _st103:
@ -2921,7 +2925,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2925 "ifchd-parse.c" #line 2929 "ifchd-parse.c"
goto _st107; goto _st107;
_st107: _st107:
@ -2983,7 +2987,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 2987 "ifchd-parse.c" #line 2991 "ifchd-parse.c"
goto _st113; goto _st113;
_st113: _st113:
@ -3042,13 +3046,13 @@ int execute_buffer(const char *newbuf)
#line 152 "ifchd-parse.rl" #line 152 "ifchd-parse.rl"
cl.state = STATE_TIMEZONE; } cl.state = STATE_TIMEZONE; }
#line 3046 "ifchd-parse.c" #line 3050 "ifchd-parse.c"
{ {
#line 93 "ifchd-parse.rl" #line 93 "ifchd-parse.rl"
arg_start = p; } arg_start = p; }
#line 3052 "ifchd-parse.c" #line 3056 "ifchd-parse.c"
goto _st119; goto _st119;
_st119: _st119:
@ -3068,7 +3072,7 @@ int execute_buffer(const char *newbuf)
#line 92 "ifchd-parse.rl" #line 92 "ifchd-parse.rl"
cl.state = STATE_NOTHING; } cl.state = STATE_NOTHING; }
#line 3072 "ifchd-parse.c" #line 3076 "ifchd-parse.c"
goto _st121; goto _st121;
_st121: _st121:
@ -3246,7 +3250,7 @@ int execute_buffer(const char *newbuf)
_out: {} _out: {}
} }
#line 189 "ifchd-parse.rl" #line 193 "ifchd-parse.rl"
if (cs == ifchd_parser_error) { if (cs == ifchd_parser_error) {
@ -3256,9 +3260,8 @@ int execute_buffer(const char *newbuf)
} }
if (cmd_start != pe) { if (cmd_start != pe) {
ssize_t ilen = stbsp_snprintf(cl.ibuf, sizeof cl.ibuf, "%s", cmd_start); if (!nstrcpy(cl.ibuf, sizeof cl.ibuf, cmd_start)) {
if (ilen < 0 || (size_t)ilen > sizeof buf) { log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
log_line("%s: (%s) snprintf2 failed", client_config.interface, __func__);
return -99; return -99;
} }
} }

View File

@ -5,8 +5,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "nk/stb_sprintf.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/nstrcpy.h"
#include "ifchd-parse.h" #include "ifchd-parse.h"
#include "ifchd.h" #include "ifchd.h"
#include "ifset.h" #include "ifset.h"
@ -171,10 +171,14 @@ int execute_buffer(const char *newbuf)
char tb[MAX_BUF]; char tb[MAX_BUF];
int cmdf = 0; int cmdf = 0;
ssize_t buflen = stbsp_snprintf(buf, sizeof buf, "%s%s", cl.ibuf, newbuf); char *snp = nstrcpy(buf, sizeof buf, cl.ibuf);
memset(cl.ibuf, 0, sizeof cl.ibuf); memset(cl.ibuf, 0, sizeof cl.ibuf);
if (buflen < 0 || (size_t)buflen > sizeof buf) { if (!snp) {
log_line("%s: (%s) snprintf1 failed", client_config.interface, __func__); log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
return -99;
}
if (!nstrcat(buf, sizeof buf, newbuf)) {
log_line("%s: (%s) nstrcat failed", client_config.interface, __func__);
return -99; return -99;
} }
@ -195,9 +199,8 @@ int execute_buffer(const char *newbuf)
} }
if (cmd_start != pe) { if (cmd_start != pe) {
ssize_t ilen = stbsp_snprintf(cl.ibuf, sizeof cl.ibuf, "%s", cmd_start); if (!nstrcpy(cl.ibuf, sizeof cl.ibuf, cmd_start)) {
if (ilen < 0 || (size_t)ilen > sizeof buf) { log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
log_line("%s: (%s) snprintf2 failed", client_config.interface, __func__);
return -99; return -99;
} }
} }

28
ifchd.c
View File

@ -14,7 +14,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <errno.h> #include <errno.h>
#include "nk/stb_sprintf.h" #include "nk/nstrcpy.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/privs.h" #include "nk/privs.h"
#include "nk/io.h" #include "nk/io.h"
@ -103,9 +103,8 @@ static int write_resolve_conf(void)
q = strchr(p, '\0'); q = strchr(p, '\0');
else else
*q++ = '\0'; *q++ = '\0';
ssize_t sl = stbsp_snprintf(buf, sizeof buf, "%s", p); if (!nstrcpy(buf, sizeof buf, p)) {
if (sl < 0 || (size_t)sl > sizeof buf) { log_line("%s: (%s) nstrcpy failed appending nameservers",
log_line("%s: (%s) snprintf failed appending nameservers",
client_config.interface, __func__); client_config.interface, __func__);
} }
@ -124,9 +123,8 @@ static int write_resolve_conf(void)
q = strchr(p, '\0'); q = strchr(p, '\0');
else else
*q++ = '\0'; *q++ = '\0';
ssize_t sl = stbsp_snprintf(buf, sizeof buf, "%s", p); if (!nstrcpy(buf, sizeof buf, p)) {
if (sl < 0 || (size_t)sl > sizeof buf) { log_line("%s: (%s) nstrcpy failed appending domains",
log_line("%s: (%s) snprintf failed appending domains",
client_config.interface, __func__); client_config.interface, __func__);
} }
@ -190,10 +188,8 @@ int perform_dns(const char *str, size_t len)
log_line("DNS server list is too long: %zu > %zu", len, sizeof cl.namesvrs); log_line("DNS server list is too long: %zu > %zu", len, sizeof cl.namesvrs);
return ret; return ret;
} }
ssize_t sl = stbsp_snprintf(cl.namesvrs, sizeof cl.namesvrs, "%s", str); if (!nstrcpy(cl.namesvrs, sizeof cl.namesvrs, str))
if (sl < 0 || (size_t)sl > sizeof cl.namesvrs) { log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
log_line("%s: (%s) snprintf failed", client_config.interface, __func__);
}
ret = write_resolve_conf(); ret = write_resolve_conf();
if (ret >= 0) if (ret >= 0)
log_line("Added DNS server: '%s'", str); log_line("Added DNS server: '%s'", str);
@ -231,10 +227,8 @@ int perform_domain(const char *str, size_t len)
log_line("DNS domain list is too long: %zu > %zu", len, sizeof cl.namesvrs); log_line("DNS domain list is too long: %zu > %zu", len, sizeof cl.namesvrs);
return ret; return ret;
} }
ssize_t sl = stbsp_snprintf(cl.domains, sizeof cl.domains, "%s", str); if (!nstrcpy(cl.domains, sizeof cl.domains, str))
if (sl < 0 || (size_t)sl > sizeof cl.domains) { log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
log_line("%s: (%s) snprintf failed", client_config.interface, __func__);
}
ret = write_resolve_conf(); ret = write_resolve_conf();
if (ret == 0) if (ret == 0)
log_line("Added DNS domain: '%s'", str); log_line("Added DNS domain: '%s'", str);
@ -345,13 +339,13 @@ static void setup_resolv_conf(void)
} }
char buf[PATH_MAX]; char buf[PATH_MAX];
ssize_t sl = stbsp_snprintf(buf, sizeof buf, "%s.head", resolv_conf_d); ssize_t sl = snprintf(buf, sizeof buf, "%s.head", resolv_conf_d);
if (sl < 0 || (size_t)sl > sizeof buf) if (sl < 0 || (size_t)sl > sizeof buf)
log_line("snprintf failed appending resolv_conf_head; path too long?"); log_line("snprintf failed appending resolv_conf_head; path too long?");
else else
resolv_conf_head_fd = open(buf, O_RDONLY|O_CLOEXEC, 0); resolv_conf_head_fd = open(buf, O_RDONLY|O_CLOEXEC, 0);
sl = stbsp_snprintf(buf, sizeof buf, "%s.tail", resolv_conf_d); sl = snprintf(buf, sizeof buf, "%s.tail", resolv_conf_d);
if (sl < 0 || (size_t)sl > sizeof buf) if (sl < 0 || (size_t)sl > sizeof buf)
log_line("snprintf failed appending resolv_conf_tail; path too long?"); log_line("snprintf failed appending resolv_conf_tail; path too long?");
else else

View File

@ -1,4 +1,4 @@
// Copyright 2011-2018 Nicholas J. Kain <njkain at gmail dot com> // Copyright 2011-2022 Nicholas J. Kain <njkain at gmail dot com>
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -11,7 +11,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include "nk/stb_sprintf.h" #include "nk/nstrcpy.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/io.h" #include "nk/io.h"
#include "leasefile.h" #include "leasefile.h"
@ -22,11 +22,12 @@ static int leasefilefd = -1;
static void get_leasefile_path(char *leasefile, size_t dlen, char *ifname) static void get_leasefile_path(char *leasefile, size_t dlen, char *ifname)
{ {
int splen = stbsp_snprintf(leasefile, dlen, "%s/LEASE-%s", if (!nstrcpy(leasefile, dlen, state_dir))
state_dir, ifname); suicide("%s: (%s) nstrcpy failed", client_config.interface, __func__);
if (splen < 0 || (size_t)splen > dlen) if (!nstrcat(leasefile, dlen, "/LEASE-"))
suicide("%s: (%s) snprintf failed; return=%d", suicide("%s: (%s) nstrcat1 failed", client_config.interface, __func__);
client_config.interface, __func__, splen); if (!nstrcat(leasefile, dlen, ifname))
suicide("%s: (%s) nstrcat2 failed", client_config.interface, __func__);
} }
void open_leasefile(void) void open_leasefile(void)
@ -49,10 +50,12 @@ static void do_write_leasefile(struct in_addr ipnum)
return; return;
} }
inet_ntop(AF_INET, &ipnum, ip, sizeof ip); inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
ssize_t olen = stbsp_snprintf(out, sizeof out, "%s\n", ip); if (!nstrcpy(out, sizeof out, ip)) {
if (olen < 0 || (size_t)olen > sizeof ip) { log_line("%s: (%s) nstrcpy failed", client_config.interface, __func__);
log_line("%s: (%s) snprintf failed; return=%zd", return;
client_config.interface, __func__, olen); }
if (!nstrcat(out, sizeof out, "\n")) {
log_line("%s: (%s) nstrcat failed", client_config.interface, __func__);
return; return;
} }
if (safe_ftruncate(leasefilefd, 0)) { if (safe_ftruncate(leasefilefd, 0)) {

View File

@ -1,3 +0,0 @@
#define STB_SPRINTF_IMPLEMENTATION
#define STB_SPRINTF_NOFLOAT
#include "stb_sprintf.h"

File diff suppressed because it is too large Load Diff

View File

@ -23,10 +23,10 @@
#include <linux/filter.h> #include <linux/filter.h>
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
#include "nk/stb_sprintf.h"
#include "nk/log.h" #include "nk/log.h"
#include "nk/io.h" #include "nk/io.h"
#include "nk/privs.h" #include "nk/privs.h"
#include "nk/nstrcpy.h"
#include "sockd.h" #include "sockd.h"
#include "ndhc-defines.h" #include "ndhc-defines.h"
#include "ndhc.h" #include "ndhc.h"
@ -142,8 +142,7 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
} }
struct ifreq ifr; struct ifreq ifr;
memset(&ifr, 0, sizeof ifr); memset(&ifr, 0, sizeof ifr);
ssize_t sl = stbsp_snprintf(ifr.ifr_name, sizeof ifr.ifr_name, "%s", iface); if (!nstrcpy(ifr.ifr_name, sizeof ifr.ifr_name, iface)) {
if (sl < 0 || (size_t)sl > sizeof ifr.ifr_name) {
log_line("%s: (%s) Set interface name failed.", log_line("%s: (%s) Set interface name failed.",
client_config.interface, __func__); client_config.interface, __func__);
goto out_fd; goto out_fd;