Project

General

Profile

Submit #2717 ยป patch-atoi.txt

dclink, 08/24/2014 12:27 AM

 
1
diff --git a/bin/ls/ls.c b/bin/ls/ls.c
2
index 604d4ad..3a758bd 100644
3
--- a/bin/ls/ls.c
4
+++ b/bin/ls/ls.c
5
@@ -139,6 +139,7 @@ main(int argc, char *argv[])
6
 	struct winsize win;
7
 	int ch, fts_options, notused;
8
 	char *p;
9
+        const char *errstr = NULL;
10
 #ifdef COLORLS
11
 	char termcapbuf[1024];	/* termcap definition buffer */
12
 	char tcapbuf[512];	/* capability buffer */
13
@@ -150,9 +151,11 @@ main(int argc, char *argv[])
14
 	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
15
 	if (isatty(STDOUT_FILENO)) {
16
 		termwidth = 80;
17
-		if ((p = getenv("COLUMNS")) != NULL && *p != '\0')
18
-			termwidth = atoi(p);
19
-		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
20
+		if ((p = getenv("COLUMNS")) != NULL && *p != '\0') {
21
+			termwidth = strtonum(p, 0, INT_MAX, &errstr);
22
+			if (errstr)
23
+				termwidth = 80;
24
+		} else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
25
 		    win.ws_col > 0)
26
 			termwidth = win.ws_col;
27
 		f_nonprint = 1;
28
@@ -160,8 +163,11 @@ main(int argc, char *argv[])
29
 		f_singlecol = 1;
30
 		/* retrieve environment variable, in case of explicit -C */
31
 		p = getenv("COLUMNS");
32
-		if (p)
33
-			termwidth = atoi(p);
34
+		if (p) {
35
+			termwidth = strtonum(p, 0, INT_MAX, &errstr);
36
+			if (errstr)
37
+				termwidth = 80;
38
+                }
39
 	}
40
 
41
 	/*
42
diff --git a/bin/pax/options.c b/bin/pax/options.c
43
index 4cf4ebf..cf29369 100644
44
--- a/bin/pax/options.c
45
+++ b/bin/pax/options.c
46
@@ -179,6 +179,7 @@ pax_options(int argc, char **argv)
47
 	unsigned int flg = 0;
48
 	unsigned int bflg = 0;
49
 	char *pt;
50
+	const char *errstr = NULL;
51
 	FSUB tmp;
52
 
53
 	/*
54
@@ -407,9 +408,12 @@ pax_options(int argc, char **argv)
55
 			flg |= CEF;
56
 			if (strcmp(NONE, optarg) == 0)
57
 				maxflt = -1;
58
-			else if ((maxflt = atoi(optarg)) < 0) {
59
-				paxwarn(1, "Error count value must be positive");
60
-				pax_usage();
61
+			else {
62
+				maxflt = strtonum(optarg, 0, INT_MAX, &errstr);
63
+				if (errstr) {
64
+					paxwarn(1, "Error count value : %s", errstr);
65
+					pax_usage();
66
+				}
67
 			}
68
 			break;
69
 		case 'G':
70
@@ -1013,6 +1017,7 @@ cpio_options(int argc, char **argv)
71
 {
72
 	int c, i;
73
 	char *str;
74
+	const char *errstr = NULL;
75
 	FSUB tmp;
76
 	FILE *fp;
77
 
78
@@ -1140,7 +1145,11 @@ cpio_options(int argc, char **argv)
79
 				/*
80
 				 * set block size in bytes
81
 				 */
82
-				wrblksz = atoi(optarg);
83
+				wrblksz = strtonum(optarg, 0, INT_MAX, &errstr);
84
+				if (errstr) {
85
+					paxwarn(1, "Error byte size : %s", errstr);
86
+					pax_usage();
87
+				}
88
 				break;
89
 			case 'E':
90
 				/*
91
diff --git a/contrib/nvi/cl/cl_term.c b/contrib/nvi/cl/cl_term.c
92
index d851930..9b489d2 100644
93
--- a/contrib/nvi/cl/cl_term.c
94
+++ b/contrib/nvi/cl/cl_term.c
95
@@ -356,6 +356,7 @@ cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
96
 	size_t col, row;
97
 	int rval;
98
 	char *p;
99
+        char *rowptr = NULL, *colptr = NULL;
100
 
101
 	/* Assume it's changed. */
102
 	if (changedp != NULL)
103
@@ -447,10 +448,15 @@ noterm:	if (row == 0)
104
 	 * deleting the LINES and COLUMNS environment variables from their
105
 	 * dot-files.
106
 	 */
107
-	if ((p = getenv("LINES")) != NULL)
108
-		row = strtol(p, NULL, 10);
109
-	if ((p = getenv("COLUMNS")) != NULL)
110
-		col = strtol(p, NULL, 10);
111
+	if ((p = getenv("LINES")) != NULL) {
112
+		row = strtol(p, &rowptr, 10);
113
+		if (errno == ERANGE || *rowptr != '\0')
114
+ 			row = 24;
115
+	} if ((p = getenv("COLUMNS")) != NULL) {
116
+		col = strtol(p, &colptr, 10);
117
+		if (errno == ERANGE || *colptr != '\0')
118
+ 			col = 80;
119
+        }
120
 
121
 	if (rowp != NULL)
122
 		*rowp = row;
123
diff --git a/sbin/camcontrol/camcontrol.c b/sbin/camcontrol/camcontrol.c
124
index 19268c6..0759e24 100644
125
--- a/sbin/camcontrol/camcontrol.c
126
+++ b/sbin/camcontrol/camcontrol.c
127
@@ -2995,14 +2995,20 @@ scsiformat(struct cam_device *device, int argc, char **argv,
128
 	 && (timeout == 0)) {
129
 		char str[1024];
130
 		int new_timeout = 0;
131
+		const char *errstr = NULL;
132
 
133
 		fprintf(stdout, "Enter new timeout in seconds or press\n"
134
 			"return to keep the current timeout [%d] ",
135
 			use_timeout / 1000);
136
 
137
 		if (fgets(str, sizeof(str), stdin) != NULL) {
138
-			if (str[0] != '\0')
139
-				new_timeout = atoi(str);
140
+			if (str[0] != '\0') {
141
+				new_timeout = strtonum(str, 0, INT_MAX, &errstr);
142
+				if (errstr) {
143
+					fprintf(stderr, "Invalid new timeout %s\n", errstr);
144
+					goto scsiformat_bailout;
145
+				}
146
+			}
147
 		}
148
 
149
 		if (new_timeout != 0) {
150
diff --git a/sbin/comcontrol/comcontrol.c b/sbin/comcontrol/comcontrol.c
151
index 561131f..644e9b6 100644
152
--- a/sbin/comcontrol/comcontrol.c
153
+++ b/sbin/comcontrol/comcontrol.c
154
@@ -96,14 +96,18 @@ main(int argc, char *argv[])
155
 					usage();
156
 				if (argv[3] == NULL || !isdigit(argv[3][0]))
157
 					usage();
158
-				dtrwait = atoi(argv[3]);
159
+				dtrwait = strtol(argv[3], NULL, 10);
160
+				if (errno == ERANGE)
161
+					usage();
162
 				argv += 2;
163
 			} else if (strcmp(argv[2],"drainwait") == 0) {
164
 				if (drainwait >= 0)
165
 					usage();
166
 				if (argv[3] == NULL || !isdigit(argv[3][0]))
167
 					usage();
168
-				drainwait = atoi(argv[3]);
169
+				drainwait = strtol(argv[3], NULL, 10);
170
+				if (errno == ERANGE)
171
+					usage();
172
 				argv += 2;
173
 			} else {
174
 				usage();
175
diff --git a/sbin/fsdb/fsdbutil.c b/sbin/fsdb/fsdbutil.c
176
index 8ea6f83..6ec3595 100644
177
--- a/sbin/fsdb/fsdbutil.c
178
+++ b/sbin/fsdb/fsdbutil.c
179
@@ -37,6 +37,7 @@
180
 #include <pwd.h>
181
 #include <string.h>
182
 #include <time.h>
183
+#include <errno.h>
184
 
185
 #include <vfs/ufs/dinode.h>
186
 #include <vfs/ufs/fs.h>
187
@@ -158,13 +159,17 @@ charsperline(void)
188
 {
189
 	int columns;
190
 	char *cp;
191
+	char *p = NULL;
192
 	struct winsize ws;
193
 
194
 	columns = 0;
195
 	if (ioctl(0, TIOCGWINSZ, &ws) != -1)
196
 		columns = ws.ws_col;
197
-	if (columns == 0 && (cp = getenv("COLUMNS")))
198
-		columns = atoi(cp);
199
+	if (columns == 0 && (cp = getenv("COLUMNS"))) {
200
+		columns = strtol(cp, &p, 10);
201
+		if (errno == ERANGE || *p != '\0')
202
+			columns = 80;
203
+	}
204
 	if (columns == 0)
205
 		columns = 80;	/* last resort */
206
 	return (columns);
207
diff --git a/sbin/growfs/growfs.c b/sbin/growfs/growfs.c
208
index bda65fe..7f31176 100644
209
--- a/sbin/growfs/growfs.c
210
+++ b/sbin/growfs/growfs.c
211
@@ -55,6 +55,7 @@
212
 #include <stdlib.h>
213
 #include <string.h>
214
 #include <unistd.h>
215
+#include <errno.h>
216
 #include <vfs/ufs/dinode.h>
217
 #include <vfs/ufs/fs.h>
218
 
219
@@ -1869,6 +1870,7 @@ charsperline(void)
220
 {
221
 	int	columns;
222
 	char	*cp;
223
+	char	*p = NULL;
224
 	struct winsize	ws;
225
 
226
 	DBG_ENTER;
227
@@ -1878,7 +1880,9 @@ charsperline(void)
228
 		columns = ws.ws_col;
229
 	}
230
 	if (columns == 0 && (cp = getenv("COLUMNS"))) {
231
-		columns = atoi(cp);
232
+		columns = strtol(cp, &p, 10);
233
+		if (errno == ERANGE || *p != '\0')
234
+			columns = 80;
235
 	}
236
 	if (columns == 0) {
237
 		columns = 80;	/* last resort */
238
diff --git a/sbin/ifconfig/ifcarp.c b/sbin/ifconfig/ifcarp.c
239
index abb2ae1..4046de4 100644
240
--- a/sbin/ifconfig/ifcarp.c
241
+++ b/sbin/ifconfig/ifcarp.c
242
@@ -119,11 +119,12 @@ setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
243
 {
244
 	int vhid;
245
 	struct carpreq carpr;
246
+	const char *errstr = NULL;
247
 
248
-	vhid = atoi(val);
249
+	vhid = strtonum(val, 1, INT_MAX, &errstr);
250
 
251
-	if (vhid <= 0)
252
-		errx(1, "vhid must be greater than 0");
253
+	if (errstr)
254
+		errx(1, "invalid vhid : %s", errstr);
255
 
256
 	memset((char *)&carpr, 0, sizeof(struct carpreq));
257
 	ifr.ifr_data = (caddr_t)&carpr;
258
@@ -142,8 +143,11 @@ setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
259
 {
260
 	int advskew;
261
 	struct carpreq carpr;
262
+	const char *errstr = NULL;
263
 
264
-	advskew = atoi(val);
265
+	advskew = strtonum(val, 1, 255, &errstr);
266
+	if (errstr)
267
+		errx(1, "invalid advskew value : %s", errstr);
268
 
269
 	memset((char *)&carpr, 0, sizeof(struct carpreq));
270
 	ifr.ifr_data = (caddr_t)&carpr;
271
@@ -162,8 +166,11 @@ setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
272
 {
273
 	int advbase;
274
 	struct carpreq carpr;
275
+	const char *errstr = NULL;
276
 
277
-	advbase = atoi(val);
278
+	advbase = strtonum(val, 1, 254, &errstr);
279
+	if (errstr)
280
+		errx(1, "invalid advbase value : %s", errstr);
281
 
282
 	memset((char *)&carpr, 0, sizeof(struct carpreq));
283
 	ifr.ifr_data = (caddr_t)&carpr;
284
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
285
index 0e9363d..c5d2bf8 100644
286
--- a/sbin/ifconfig/ifconfig.c
287
+++ b/sbin/ifconfig/ifconfig.c
288
@@ -779,9 +779,12 @@ static void
289
 setifmetric(const char *val, int dummy __unused, int s, 
290
     const struct afswtch *afp)
291
 {
292
+	const char *errstr = NULL;
293
 	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
294
-	ifr.ifr_metric = atoi(val);
295
-	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
296
+	ifr.ifr_metric = strtonum(val, 0, INT_MAX, &errstr);
297
+	if (errstr)
298
+		warn("invalid metric value %s", errstr);
299
+	else if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
300
 		warn("ioctl (set metric)");
301
 }
302
 
303
@@ -789,9 +792,12 @@ static void
304
 setifmtu(const char *val, int dummy __unused, int s, 
305
     const struct afswtch *afp)
306
 {
307
+	const char *errstr = NULL;
308
 	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
309
-	ifr.ifr_mtu = atoi(val);
310
-	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
311
+	ifr.ifr_mtu = strtonum(val, 0, INT_MAX, &errstr);
312
+	if (errstr)
313
+		warn("invalid mtu value %s", errstr);
314
+	else if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
315
 		warn("ioctl (set mtu)");
316
 }
317
 
318
@@ -799,9 +805,12 @@ static void
319
 setiftsolen(const char *val, int dummy __unused, int s,
320
     const struct afswtch *afp)
321
 {
322
+	const char *errstr = NULL;
323
 	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
324
-	ifr.ifr_tsolen = atoi(val);
325
-	if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
326
+	ifr.ifr_tsolen = strtonum(val, 0, INT_MAX, &errstr);
327
+	if (errstr)
328
+		warn("invalid tsolen value %s", errstr);
329
+	else if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
330
 		warn("ioctl (set tsolen)");
331
 }
332
 
333
diff --git a/sbin/ifconfig/ifieee80211.c b/sbin/ifconfig/ifieee80211.c
334
index e0e890b..1e37fe2 100644
335
--- a/sbin/ifconfig/ifieee80211.c
336
+++ b/sbin/ifconfig/ifieee80211.c
337
@@ -1848,13 +1848,13 @@ DECL_CMD_FUNC(set80211meshttl, val, d)
338
 static
339
 DECL_CMD_FUNC(set80211meshforward, val, d)
340
 {
341
-	set80211(s, IEEE80211_IOC_MESH_FWRD, atoi(val), 0, NULL);
342
+	set80211(s, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
343
 }
344
 
345
 static
346
 DECL_CMD_FUNC(set80211meshpeering, val, d)
347
 {
348
-	set80211(s, IEEE80211_IOC_MESH_AP, atoi(val), 0, NULL);
349
+	set80211(s, IEEE80211_IOC_MESH_AP, d, 0, NULL);
350
 }
351
 
352
 static
353
diff --git a/sbin/route/route.c b/sbin/route/route.c
354
index 924c764..e289b92 100644
355
--- a/sbin/route/route.c
356
+++ b/sbin/route/route.c
357
@@ -543,6 +543,7 @@ set_metric(char *value, int key)
358
 {
359
 	int flag = 0;
360
 	u_long noval, *valp = &noval;
361
+	char *p = NULL;
362
 
363
 	switch (key) {
364
 #define caseof(x, y, z)	case x: valp = &rt_metrics.z; flag = y; break
365
@@ -563,7 +564,9 @@ set_metric(char *value, int key)
366
 		rt_metrics.rmx_locks |= flag;
367
 	if (locking)
368
 		locking = 0;
369
-	*valp = atoi(value);
370
+	*valp = strtol(value, &p, 10);
371
+	if (errno == ERANGE || *p != '\0')
372
+		errx(EX_OSERR, "%s : bad metric", value);
373
 }
374
 
375
 static void
376
diff --git a/sbin/slattach/slattach.c b/sbin/slattach/slattach.c
377
index f6794b6..35978ef 100644
378
--- a/sbin/slattach/slattach.c
379
+++ b/sbin/slattach/slattach.c
380
@@ -45,6 +45,7 @@
381
 #include <signal.h>
382
 #include <stdio.h>
383
 #include <stdlib.h>
384
+#include <limits.h>
385
 #include <string.h>
386
 #include <syslog.h>
387
 #include <termios.h>
388
@@ -105,6 +106,7 @@ int
389
 main(int argc, char **argv)
390
 {
391
 	int option;
392
+	const char *errstr = NULL;
393
 
394
 	while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) {
395
 		switch (option) {
396
@@ -135,7 +137,11 @@ main(int argc, char **argv)
397
 			redial_cmd = (char*) strdup (optarg);
398
 			break;
399
 		case 's':
400
-			speed = atoi(optarg);
401
+			speed = strtonum(optarg, 0, INT_MAX, &errstr);
402
+			if (errstr) {
403
+				fprintf(stderr, "bad speed value : %s", errstr);
404
+				exit(1);
405
+			}
406
 			break;
407
 		case 'u':
408
 			config_cmd = (char*) strdup (optarg);
409
diff --git a/sbin/startslip/startslip.c b/sbin/startslip/startslip.c
410
index c035ec8..fb2fe94 100644
411
--- a/sbin/startslip/startslip.c
412
+++ b/sbin/startslip/startslip.c
413
@@ -42,6 +42,7 @@
414
 #include <signal.h>
415
 #include <stdio.h>
416
 #include <stdlib.h>
417
+#include <limits.h>
418
 #include <string.h>
419
 #include <syslog.h>
420
 #include <termios.h>
421
@@ -115,6 +116,7 @@ main(int argc, char **argv)
422
 	pid_t pid;
423
 	struct termios t;
424
 	int result;
425
+	const char *errstr = NULL;
426
 
427
 	while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
428
 		switch (ch) {
429
@@ -122,7 +124,9 @@ main(int argc, char **argv)
430
 			debug = 1;
431
 			break;
432
 		case 'b':
433
-			speed = atoi(optarg);
434
+			speed = strtonum(optarg, 0, INT_MAX, &errstr);
435
+			if (errstr)
436
+				errx(1, "bad speed value (%s)", errstr);
437
 			break;
438
 		case 's':
439
 			if (diali >= MAXDIALS)
440
@@ -130,10 +134,14 @@ main(int argc, char **argv)
441
 			dials[diali++] = strdup(optarg);
442
 			break;
443
 		case 't':
444
-			script_timeout = atoi(optarg);
445
+			script_timeout = strtonum(optarg, 0, INT_MAX, &errstr);
446
+			if (errstr)
447
+				errx(1, "bad script_timeout value (%s)", errstr);
448
 			break;
449
 		case 'w':
450
-			wait_time = atoi(optarg);
451
+			wait_time = strtonum(optarg, 0, INT_MAX, &errstr);
452
+			if (errstr)
453
+				errx(1, "bas wait_time value (%s)", errstr);
454
 			break;
455
 		case 'W':
456
 			MAXTRIES = atoi(optarg);
457
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c
458
index 14f9d89..f4acacb 100644
459
--- a/sbin/sysctl/sysctl.c
460
+++ b/sbin/sysctl/sysctl.c
461
@@ -208,21 +208,29 @@ parse(const char *string)
462
 						    (char *)newval);
463
 				} else
464
 					intval = (int) strtol(newval, NULL, 0);
465
+				if (errno == ERANGE)
466
+					errx(1, "value %s out of range", (char *)newval);
467
 				newval = &intval;
468
 				newsize = sizeof(intval);
469
 				break;
470
 			case CTLTYPE_UINT:
471
 				uintval = (int) strtoul(newval, NULL, 0);
472
+				if (errno == ERANGE)
473
+					errx(1, "value %s out of range", (char *)newval);
474
 				newval = &uintval;
475
 				newsize = sizeof uintval;
476
 				break;
477
 			case CTLTYPE_LONG:
478
 				longval = strtol(newval, NULL, 0);
479
+				if (errno == ERANGE)
480
+					errx(1, "value %s out of range", (char *)newval);
481
 				newval = &longval;
482
 				newsize = sizeof longval;
483
 				break;
484
 			case CTLTYPE_ULONG:
485
 				ulongval = strtoul(newval, NULL, 0);
486
+				if (errno == ERANGE)
487
+					errx(1, "value %s out of range", (char *)newval);
488
 				newval = &ulongval;
489
 				newsize = sizeof ulongval;
490
 				break;
491
diff --git a/sbin/vinum/v.c b/sbin/vinum/v.c
492
index 13b8de4..e08f5e8 100644
493
--- a/sbin/vinum/v.c
494
+++ b/sbin/vinum/v.c
495
@@ -316,6 +316,7 @@ parseline(int args, char *argv[])
496
     int j;
497
     enum keyword command;				    /* command to execute */
498
 
499
+    const char *errstr = NULL;
500
     if (hist != NULL) {				    /* save the command to history file */
501
 	timestamp();
502
 	for (i = 0; i < args; i++)			    /* all args */
503
@@ -360,10 +361,13 @@ parseline(int args, char *argv[])
504
 	    case 'i':					    /* interval */
505
 		interval = 0;
506
 		if (argv[i][j + 1] != '\0')		    /* operand follows, */
507
-		    interval = atoi(&argv[i][j + 1]);	    /* use it */
508
+		    interval = strtonum(&argv[i][j + 1], 0, INT_MAX, &errstr);	    /* use it */
509
 		else if (args > (i + 1))		    /* another following, */
510
-		    interval = atoi(argv[++i]);		    /* use it */
511
-		if (interval == 0)			    /* nothing valid, */
512
+		    interval = strtonum(argv[++i], 0, INT_MAX, &errstr);		    /* use it */
513
+		if (errstr) {
514
+		    fprintf(stderr, "-i: invalid interval specified: %s\n", errstr);
515
+		    exit(1);
516
+		} else if (interval == 0)			    /* nothing valid, */
517
 		    fprintf(stderr, "-i: no interval specified\n");
518
 		break;
519
 
520
@@ -387,10 +391,13 @@ parseline(int args, char *argv[])
521
 	    case 'S':
522
 		SSize = 0;
523
 		if (argv[i][j + 1] != '\0')		    /* operand follows, */
524
-		    SSize = atoi(&argv[i][j + 1]);	    /* use it */
525
+		    SSize = strtonum(&argv[i][j + 1], 0, INT_MAX, &errstr);	    /* use it */
526
 		else if (args > (i + 1))		    /* another following, */
527
-		    SSize = atoi(argv[++i]);		    /* use it */
528
-		if (SSize == 0)				    /* nothing valid, */
529
+		    SSize = strtonum(argv[++i], 0, INT_MAX, &errstr);		    /* use it */
530
+		if (errstr) {
531
+		    fprintf(stderr, "-S: invalid size specified %s", errstr);
532
+		    exit(1);
533
+		} else if (SSize == 0)				    /* nothing valid, */
534
 		    fprintf(stderr, "-S: no size specified\n");
535
 		break;
536
 
537
diff --git a/usr.bin/column/column.c b/usr.bin/column/column.c
538
index fc1345d..bad2b54 100644
539
--- a/usr.bin/column/column.c
540
+++ b/usr.bin/column/column.c
541
@@ -71,15 +71,18 @@ main(int argc, char **argv)
542
 	FILE *fp;
543
 	int ch, tflag, xflag;
544
 	char *p;
545
-	const char *src;
546
+	const char *src, *errstr = NULL;;
547
 	wchar_t *newsep;
548
 	size_t seplen;
549
 
550
 	setlocale(LC_ALL, "");
551
 
552
 	if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
553
-		if ((p = getenv("COLUMNS")))
554
-			termwidth = atoi(p);
555
+		if ((p = getenv("COLUMNS"))) {
556
+			termwidth = strtonum(p, 0, INT_MAX, &errstr);
557
+			if (errstr)
558
+				termwidth = 80;
559
+		}
560
 	} else
561
 		termwidth = win.ws_col;
562
 
563
@@ -87,7 +90,9 @@ main(int argc, char **argv)
564
 	while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
565
 		switch(ch) {
566
 		case 'c':
567
-			termwidth = atoi(optarg);
568
+			termwidth = strtonum(optarg, 0, INT_MAX, &errstr);
569
+			if (errstr)
570
+				termwidth = 80;
571
 			break;
572
 		case 's':
573
 			src = optarg;
    (1-1/1)