Skip to content

Commit dff10c7

Browse files
author
itojun
committed
s/sprintf/snprintf/.
there seem to be couple of unsafe use of strcat and strcpy - we should bring in strl{cat,cpy}.
1 parent 92d3fd1 commit dff10c7

17 files changed

+89
-77
lines changed

addrtoname.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
#ifndef lint
2525
static const char rcsid[] =
26-
"@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.64 1999-11-21 09:36:44 fenner Exp $ (LBL)";
26+
"@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.65 2000-01-17 06:24:23 itojun Exp $ (LBL)";
2727
#endif
2828

2929
#ifdef HAVE_CONFIG_H
@@ -556,7 +556,7 @@ tcpport_string(u_short port)
556556
tp->addr = i;
557557
tp->nxt = newhnamemem();
558558

559-
(void)sprintf(buf, "%u", i);
559+
(void)snprintf(buf, sizeof(buf), "%u", i);
560560
tp->name = savestr(buf);
561561
return (tp->name);
562562
}
@@ -575,7 +575,7 @@ udpport_string(register u_short port)
575575
tp->addr = i;
576576
tp->nxt = newhnamemem();
577577

578-
(void)sprintf(buf, "%u", i);
578+
(void)snprintf(buf, sizeof(buf), "%u", i);
579579
tp->name = savestr(buf);
580580
return (tp->name);
581581
}
@@ -601,7 +601,7 @@ init_servarray(void)
601601
while (table->name)
602602
table = table->nxt;
603603
if (nflag) {
604-
(void)sprintf(buf, "%d", port);
604+
(void)snprintf(buf, sizeof(buf), "%d", port);
605605
table->name = savestr(buf);
606606
} else
607607
table->name = savestr(sv->s_name);

machdep.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#ifndef lint
2323
static const char rcsid[] =
24-
"@(#) $Header: /tcpdump/master/tcpdump/machdep.c,v 1.5 1999-11-21 09:36:47 fenner Exp $ (LBL)";
24+
"@(#) $Header: /tcpdump/master/tcpdump/machdep.c,v 1.6 2000-01-17 06:24:23 itojun Exp $ (LBL)";
2525
#endif
2626

2727
#ifdef HAVE_CONFIG_H
@@ -37,13 +37,13 @@ static const char rcsid[] =
3737
#include "machdep.h"
3838

3939
int
40-
abort_on_misalignment(char *ebuf)
40+
abort_on_misalignment(char *ebuf, size_t ebufsiz)
4141
{
4242
#ifdef __osf__
4343
static int buf[2] = { SSIN_UACPROC, UAC_SIGBUS };
4444

4545
if (setsysinfo(SSI_NVPAIRS, (caddr_t)buf, 1, 0, 0) < 0) {
46-
(void)sprintf(ebuf, "setsysinfo: errno %d", errno);
46+
(void)snprintf(ebuf, ebufsiz, "setsysinfo: errno %d", errno);
4747
return (-1);
4848
}
4949
#endif

machdep.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1919
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2020
*
21-
* @(#) $Header: /tcpdump/master/tcpdump/machdep.h,v 1.1 1999-10-07 23:47:10 mcr Exp $ (LBL)
21+
* @(#) $Header: /tcpdump/master/tcpdump/machdep.h,v 1.2 2000-01-17 06:24:24 itojun Exp $ (LBL)
2222
*/
2323
#ifndef tcpdump_machdep_h
2424
#define tcpdump_machdep_h
2525

26-
int abort_on_misalignment(char *);
26+
int abort_on_misalignment(char *, size_t);
2727
#endif

print-ascii.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
#ifndef lint
4444
static const char rcsid[] =
45-
"@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.4 2000-01-09 21:34:16 fenner Exp $";
45+
"@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.5 2000-01-17 06:24:24 itojun Exp $";
4646
#endif
4747
#include <stdio.h>
4848
#include <sys/types.h>
@@ -72,7 +72,7 @@ ascii_print_with_offset(register const u_char *cp, register u_int length,
7272
while (--nshorts >= 0) {
7373
s1 = *cp++;
7474
s2 = *cp++;
75-
(void)sprintf(hsp, " %02x%02x", s1, s2);
75+
(void)snprintf(hsp, sizeof(hsp), " %02x%02x", s1, s2);
7676
hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
7777
*(asp++) = (isgraph(s1) ? s1 : '.');
7878
*(asp++) = (isgraph(s2) ? s2 : '.');
@@ -87,7 +87,7 @@ ascii_print_with_offset(register const u_char *cp, register u_int length,
8787
}
8888
if (length & 1) {
8989
s1 = *cp++;
90-
(void)sprintf(hsp, " %02x", s1);
90+
(void)snprintf(hsp, sizeof(hsp), " %02x", s1);
9191
hsp += 3;
9292
*(asp++) = (isgraph(s1) ? s1 : '.');
9393
++i;

print-atalk.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#ifndef lint
2525
static const char rcsid[] =
26-
"@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.51 1999-11-21 09:36:48 fenner Exp $ (LBL)";
26+
"@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.52 2000-01-17 06:24:24 itojun Exp $ (LBL)";
2727
#endif
2828

2929
#ifdef HAVE_CONFIG_H
@@ -538,18 +538,20 @@ ataddr_string(u_short atnet, u_char athost)
538538
if (tp2->addr == i) {
539539
tp->addr = (atnet << 8) | athost;
540540
tp->nxt = newhnamemem();
541-
(void)sprintf(nambuf, "%s.%d", tp2->name, athost);
541+
(void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
542+
tp2->name, athost);
542543
tp->name = savestr(nambuf);
543544
return (tp->name);
544545
}
545546

546547
tp->addr = (atnet << 8) | athost;
547548
tp->nxt = newhnamemem();
548549
if (athost != 255)
549-
(void)sprintf(nambuf, "%d.%d.%d",
550+
(void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
550551
atnet >> 8, atnet & 0xff, athost);
551552
else
552-
(void)sprintf(nambuf, "%d.%d", atnet >> 8, atnet & 0xff);
553+
(void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
554+
atnet & 0xff);
553555
tp->name = savestr(nambuf);
554556

555557
return (tp->name);
@@ -569,7 +571,7 @@ ddpskt_string(register int skt)
569571
static char buf[8];
570572

571573
if (nflag) {
572-
(void)sprintf(buf, "%d", skt);
574+
(void)snprintf(buf, sizeof(buf), "%d", skt);
573575
return (buf);
574576
}
575577
return (tok2str(skt2str, "%d", skt));

print-bgp.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#ifndef lint
3535
static const char rcsid[] =
36-
"@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.10 2000-01-09 21:34:17 fenner Exp $";
36+
"@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.11 2000-01-17 06:24:24 itojun Exp $";
3737
#endif
3838

3939
#include <sys/param.h>
@@ -240,7 +240,7 @@ num_or_str(const char **table, size_t siz, int value)
240240
{
241241
static char buf[20];
242242
if (value < 0 || siz <= value || table[value] == NULL) {
243-
sprintf(buf, "#%d", value);
243+
snprintf(buf, sizeof(buf), "#%d", value);
244244
return buf;
245245
} else
246246
return table[value];
@@ -266,7 +266,7 @@ bgp_notify_minor(int major, int minor)
266266
} else
267267
p = NULL;
268268
if (p == NULL) {
269-
sprintf(buf, "#%d", minor);
269+
snprintf(buf, sizeof(buf), "#%d", minor);
270270
return buf;
271271
} else
272272
return p;
@@ -288,7 +288,7 @@ decode_prefix4(const u_char *pd, char *buf, int buflen)
288288
((u_char *)&addr)[(plen + 7) / 8 - 1] &=
289289
((0xff00 >> (plen % 8)) & 0xff);
290290
}
291-
sprintf(buf, "%s/%d", getname((char *)&addr), plen);
291+
snprintf(buf, buflen, "%s/%d", getname((char *)&addr), plen);
292292
return 1 + (plen + 7) / 8;
293293
}
294294

@@ -309,7 +309,7 @@ decode_prefix6(const u_char *pd, char *buf, int buflen)
309309
addr.s6_addr[(plen + 7) / 8 - 1] &=
310310
((0xff00 >> (plen % 8)) & 0xff);
311311
}
312-
sprintf(buf, "%s/%d", getname6((char *)&addr), plen);
312+
snprintf(buf, buflen, "%s/%d", getname6((char *)&addr), plen);
313313
return 1 + (plen + 7) / 8;
314314
}
315315
#endif

print-decnet.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#ifndef lint
2323
static const char rcsid[] =
24-
"@(#) $Header: /tcpdump/master/tcpdump/print-decnet.c,v 1.27 1999-11-21 09:36:50 fenner Exp $ (LBL)";
24+
"@(#) $Header: /tcpdump/master/tcpdump/print-decnet.c,v 1.28 2000-01-17 06:24:24 itojun Exp $ (LBL)";
2525
#endif
2626

2727
#ifdef HAVE_CONFIG_H
@@ -740,13 +740,14 @@ char *
740740
dnnum_string(u_short dnaddr)
741741
{
742742
char *str;
743+
size_t siz;
743744
int area = (u_short)(dnaddr & AREAMASK) >> AREASHIFT;
744745
int node = dnaddr & NODEMASK;
745746

746-
str = (char *)malloc(sizeof("00.0000"));
747+
str = (char *)malloc(siz = sizeof("00.0000"));
747748
if (str == NULL)
748749
error("dnnum_string: malloc");
749-
sprintf(str, "%d.%d", area, node);
750+
snprintf(str, siz, "%d.%d", area, node);
750751
return(str);
751752
}
752753

print-icmp.c

+29-24
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#ifndef lint
2323
static const char rcsid[] =
24-
"@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.43 1999-11-22 04:28:21 fenner Exp $ (LBL)";
24+
"@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.44 2000-01-17 06:24:25 itojun Exp $ (LBL)";
2525
#endif
2626

2727
#ifdef HAVE_CONFIG_H
@@ -196,9 +196,10 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
196196

197197
case ICMP_UNREACH_PROTOCOL:
198198
TCHECK(dp->icmp_ip.ip_p);
199-
(void)sprintf(buf, "%s protocol %d unreachable",
200-
ipaddr_string(&dp->icmp_ip.ip_dst),
201-
dp->icmp_ip.ip_p);
199+
(void)snprintf(buf, sizeof(buf),
200+
"%s protocol %d unreachable",
201+
ipaddr_string(&dp->icmp_ip.ip_dst),
202+
dp->icmp_ip.ip_p);
202203
break;
203204

204205
case ICMP_UNREACH_PORT:
@@ -210,21 +211,21 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
210211
switch (oip->ip_p) {
211212

212213
case IPPROTO_TCP:
213-
(void)sprintf(buf,
214+
(void)snprintf(buf, sizeof(buf),
214215
"%s tcp port %s unreachable",
215216
ipaddr_string(&oip->ip_dst),
216217
tcpport_string(dport));
217218
break;
218219

219220
case IPPROTO_UDP:
220-
(void)sprintf(buf,
221+
(void)snprintf(buf, sizeof(buf),
221222
"%s udp port %s unreachable",
222223
ipaddr_string(&oip->ip_dst),
223224
udpport_string(dport));
224225
break;
225226

226227
default:
227-
(void)sprintf(buf,
228+
(void)snprintf(buf, sizeof(buf),
228229
"%s protocol %d port %d unreachable",
229230
ipaddr_string(&oip->ip_dst),
230231
oip->ip_p, dport);
@@ -239,11 +240,11 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
239240
mp = (struct mtu_discovery *)&dp->icmp_void;
240241
mtu = EXTRACT_16BITS(&mp->nexthopmtu);
241242
if (mtu)
242-
(void)sprintf(buf,
243+
(void)snprintf(buf, sizeof(buf),
243244
"%s unreachable - need to frag (mtu %d)",
244245
ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
245246
else
246-
(void)sprintf(buf,
247+
(void)snprintf(buf, sizeof(buf),
247248
"%s unreachable - need to frag",
248249
ipaddr_string(&dp->icmp_ip.ip_dst));
249250
}
@@ -252,7 +253,7 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
252253
default:
253254
fmt = tok2str(unreach2str, "#%d %%s unreachable",
254255
dp->icmp_code);
255-
(void)sprintf(buf, fmt,
256+
(void)snprintf(buf, sizeof(buf), fmt,
256257
ipaddr_string(&dp->icmp_ip.ip_dst));
257258
break;
258259
}
@@ -262,7 +263,7 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
262263
TCHECK(dp->icmp_ip.ip_dst);
263264
fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
264265
dp->icmp_code);
265-
(void)sprintf(buf, fmt,
266+
(void)snprintf(buf, sizeof(buf), fmt,
266267
ipaddr_string(&dp->icmp_ip.ip_dst),
267268
ipaddr_string(&dp->icmp_gwaddr));
268269
break;
@@ -278,34 +279,37 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
278279

279280
ihp = (struct ih_rdiscovery *)&dp->icmp_void;
280281
TCHECK(*ihp);
281-
(void)strcpy(cp, " lifetime ");
282+
(void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
282283
cp = buf + strlen(buf);
283284
lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
284285
if (lifetime < 60)
285-
(void)sprintf(cp, "%u", lifetime);
286+
(void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
287+
lifetime);
286288
else if (lifetime < 60 * 60)
287-
(void)sprintf(cp, "%u:%02u",
289+
(void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
288290
lifetime / 60, lifetime % 60);
289291
else
290-
(void)sprintf(cp, "%u:%02u:%02u",
292+
(void)snprintf(cp, sizeof(buf) - (cp - buf),
293+
"%u:%02u:%02u",
291294
lifetime / 3600,
292295
(lifetime % 3600) / 60,
293296
lifetime % 60);
294297
cp = buf + strlen(buf);
295298

296299
num = ihp->ird_addrnum;
297-
(void)sprintf(cp, " %d:", num);
300+
(void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
298301
cp = buf + strlen(buf);
299302

300303
size = ihp->ird_addrsiz;
301304
if (size != 2) {
302-
(void)sprintf(cp, " [size %d]", size);
305+
(void)snprintf(cp, sizeof(buf) - (cp - buf),
306+
" [size %d]", size);
303307
break;
304308
}
305309
idp = (struct id_rdiscovery *)&dp->icmp_data;
306310
while (num-- > 0) {
307311
TCHECK(*idp);
308-
(void)sprintf(cp, " {%s %u}",
312+
(void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
309313
ipaddr_string(&idp->ird_addr),
310314
EXTRACT_32BITS(&idp->ird_pref));
311315
cp = buf + strlen(buf);
@@ -326,25 +330,26 @@ icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
326330
break;
327331

328332
default:
329-
(void)sprintf(buf, "time exceeded-#%d", dp->icmp_code);
333+
(void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
334+
dp->icmp_code);
330335
break;
331336
}
332337
break;
333338

334339
case ICMP_PARAMPROB:
335340
if (dp->icmp_code)
336-
(void)sprintf(buf, "parameter problem - code %d",
337-
dp->icmp_code);
341+
(void)snprintf(buf, sizeof(buf),
342+
"parameter problem - code %d", dp->icmp_code);
338343
else {
339344
TCHECK(dp->icmp_pptr);
340-
(void)sprintf(buf, "parameter problem - octet %d",
341-
dp->icmp_pptr);
345+
(void)snprintf(buf, sizeof(buf),
346+
"parameter problem - octet %d", dp->icmp_pptr);
342347
}
343348
break;
344349

345350
case ICMP_MASKREPLY:
346351
TCHECK(dp->icmp_mask);
347-
(void)sprintf(buf, "address mask is 0x%08x",
352+
(void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
348353
(u_int32_t)ntohl(dp->icmp_mask));
349354
break;
350355

print-ipx.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#ifndef lint
2626
static const char rcsid[] =
27-
"@(#) $Header: /tcpdump/master/tcpdump/print-ipx.c,v 1.22 1999-11-21 09:36:54 fenner Exp $";
27+
"@(#) $Header: /tcpdump/master/tcpdump/print-ipx.c,v 1.23 2000-01-17 06:24:25 itojun Exp $";
2828
#endif
2929

3030
#ifdef HAVE_CONFIG_H
@@ -92,7 +92,7 @@ ipxaddr_string(u_int32_t net, const u_char *node)
9292
{
9393
static char line[256];
9494

95-
sprintf(line, "%x.%02x:%02x:%02x:%02x:%02x:%02x",
95+
snprintf(line, sizeof(line), "%x.%02x:%02x:%02x:%02x:%02x:%02x",
9696
net, node[0], node[1], node[2], node[3], node[4], node[5]);
9797

9898
return line;

0 commit comments

Comments
 (0)