Project

General

Profile

Submit #2717 ยป patch-atoi.txt

dclink, 08/24/2014 12:27 AM

 
diff --git a/bin/ls/ls.c b/bin/ls/ls.c
index 604d4ad..3a758bd 100644
--- a/bin/ls/ls.c
+++ b/bin/ls/ls.c
@@ -139,6 +139,7 @@ main(int argc, char *argv[])
struct winsize win;
int ch, fts_options, notused;
char *p;
+ const char *errstr = NULL;
#ifdef COLORLS
char termcapbuf[1024]; /* termcap definition buffer */
char tcapbuf[512]; /* capability buffer */
@@ -150,9 +151,11 @@ main(int argc, char *argv[])
/* Terminal defaults to -Cq, non-terminal defaults to -1. */
if (isatty(STDOUT_FILENO)) {
termwidth = 80;
- if ((p = getenv("COLUMNS")) != NULL && *p != '\0')
- termwidth = atoi(p);
- else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
+ if ((p = getenv("COLUMNS")) != NULL && *p != '\0') {
+ termwidth = strtonum(p, 0, INT_MAX, &errstr);
+ if (errstr)
+ termwidth = 80;
+ } else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 &&
win.ws_col > 0)
termwidth = win.ws_col;
f_nonprint = 1;
@@ -160,8 +163,11 @@ main(int argc, char *argv[])
f_singlecol = 1;
/* retrieve environment variable, in case of explicit -C */
p = getenv("COLUMNS");
- if (p)
- termwidth = atoi(p);
+ if (p) {
+ termwidth = strtonum(p, 0, INT_MAX, &errstr);
+ if (errstr)
+ termwidth = 80;
+ }
}
/*
diff --git a/bin/pax/options.c b/bin/pax/options.c
index 4cf4ebf..cf29369 100644
--- a/bin/pax/options.c
+++ b/bin/pax/options.c
@@ -179,6 +179,7 @@ pax_options(int argc, char **argv)
unsigned int flg = 0;
unsigned int bflg = 0;
char *pt;
+ const char *errstr = NULL;
FSUB tmp;
/*
@@ -407,9 +408,12 @@ pax_options(int argc, char **argv)
flg |= CEF;
if (strcmp(NONE, optarg) == 0)
maxflt = -1;
- else if ((maxflt = atoi(optarg)) < 0) {
- paxwarn(1, "Error count value must be positive");
- pax_usage();
+ else {
+ maxflt = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr) {
+ paxwarn(1, "Error count value : %s", errstr);
+ pax_usage();
+ }
}
break;
case 'G':
@@ -1013,6 +1017,7 @@ cpio_options(int argc, char **argv)
{
int c, i;
char *str;
+ const char *errstr = NULL;
FSUB tmp;
FILE *fp;
@@ -1140,7 +1145,11 @@ cpio_options(int argc, char **argv)
/*
* set block size in bytes
*/
- wrblksz = atoi(optarg);
+ wrblksz = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr) {
+ paxwarn(1, "Error byte size : %s", errstr);
+ pax_usage();
+ }
break;
case 'E':
/*
diff --git a/contrib/nvi/cl/cl_term.c b/contrib/nvi/cl/cl_term.c
index d851930..9b489d2 100644
--- a/contrib/nvi/cl/cl_term.c
+++ b/contrib/nvi/cl/cl_term.c
@@ -356,6 +356,7 @@ cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
size_t col, row;
int rval;
char *p;
+ char *rowptr = NULL, *colptr = NULL;
/* Assume it's changed. */
if (changedp != NULL)
@@ -447,10 +448,15 @@ noterm: if (row == 0)
* deleting the LINES and COLUMNS environment variables from their
* dot-files.
*/
- if ((p = getenv("LINES")) != NULL)
- row = strtol(p, NULL, 10);
- if ((p = getenv("COLUMNS")) != NULL)
- col = strtol(p, NULL, 10);
+ if ((p = getenv("LINES")) != NULL) {
+ row = strtol(p, &rowptr, 10);
+ if (errno == ERANGE || *rowptr != '\0')
+ row = 24;
+ } if ((p = getenv("COLUMNS")) != NULL) {
+ col = strtol(p, &colptr, 10);
+ if (errno == ERANGE || *colptr != '\0')
+ col = 80;
+ }
if (rowp != NULL)
*rowp = row;
diff --git a/sbin/camcontrol/camcontrol.c b/sbin/camcontrol/camcontrol.c
index 19268c6..0759e24 100644
--- a/sbin/camcontrol/camcontrol.c
+++ b/sbin/camcontrol/camcontrol.c
@@ -2995,14 +2995,20 @@ scsiformat(struct cam_device *device, int argc, char **argv,
&& (timeout == 0)) {
char str[1024];
int new_timeout = 0;
+ const char *errstr = NULL;
fprintf(stdout, "Enter new timeout in seconds or press\n"
"return to keep the current timeout [%d] ",
use_timeout / 1000);
if (fgets(str, sizeof(str), stdin) != NULL) {
- if (str[0] != '\0')
- new_timeout = atoi(str);
+ if (str[0] != '\0') {
+ new_timeout = strtonum(str, 0, INT_MAX, &errstr);
+ if (errstr) {
+ fprintf(stderr, "Invalid new timeout %s\n", errstr);
+ goto scsiformat_bailout;
+ }
+ }
}
if (new_timeout != 0) {
diff --git a/sbin/comcontrol/comcontrol.c b/sbin/comcontrol/comcontrol.c
index 561131f..644e9b6 100644
--- a/sbin/comcontrol/comcontrol.c
+++ b/sbin/comcontrol/comcontrol.c
@@ -96,14 +96,18 @@ main(int argc, char *argv[])
usage();
if (argv[3] == NULL || !isdigit(argv[3][0]))
usage();
- dtrwait = atoi(argv[3]);
+ dtrwait = strtol(argv[3], NULL, 10);
+ if (errno == ERANGE)
+ usage();
argv += 2;
} else if (strcmp(argv[2],"drainwait") == 0) {
if (drainwait >= 0)
usage();
if (argv[3] == NULL || !isdigit(argv[3][0]))
usage();
- drainwait = atoi(argv[3]);
+ drainwait = strtol(argv[3], NULL, 10);
+ if (errno == ERANGE)
+ usage();
argv += 2;
} else {
usage();
diff --git a/sbin/fsdb/fsdbutil.c b/sbin/fsdb/fsdbutil.c
index 8ea6f83..6ec3595 100644
--- a/sbin/fsdb/fsdbutil.c
+++ b/sbin/fsdb/fsdbutil.c
@@ -37,6 +37,7 @@
#include <pwd.h>
#include <string.h>
#include <time.h>
+#include <errno.h>
#include <vfs/ufs/dinode.h>
#include <vfs/ufs/fs.h>
@@ -158,13 +159,17 @@ charsperline(void)
{
int columns;
char *cp;
+ char *p = NULL;
struct winsize ws;
columns = 0;
if (ioctl(0, TIOCGWINSZ, &ws) != -1)
columns = ws.ws_col;
- if (columns == 0 && (cp = getenv("COLUMNS")))
- columns = atoi(cp);
+ if (columns == 0 && (cp = getenv("COLUMNS"))) {
+ columns = strtol(cp, &p, 10);
+ if (errno == ERANGE || *p != '\0')
+ columns = 80;
+ }
if (columns == 0)
columns = 80; /* last resort */
return (columns);
diff --git a/sbin/growfs/growfs.c b/sbin/growfs/growfs.c
index bda65fe..7f31176 100644
--- a/sbin/growfs/growfs.c
+++ b/sbin/growfs/growfs.c
@@ -55,6 +55,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include <vfs/ufs/dinode.h>
#include <vfs/ufs/fs.h>
@@ -1869,6 +1870,7 @@ charsperline(void)
{
int columns;
char *cp;
+ char *p = NULL;
struct winsize ws;
DBG_ENTER;
@@ -1878,7 +1880,9 @@ charsperline(void)
columns = ws.ws_col;
}
if (columns == 0 && (cp = getenv("COLUMNS"))) {
- columns = atoi(cp);
+ columns = strtol(cp, &p, 10);
+ if (errno == ERANGE || *p != '\0')
+ columns = 80;
}
if (columns == 0) {
columns = 80; /* last resort */
diff --git a/sbin/ifconfig/ifcarp.c b/sbin/ifconfig/ifcarp.c
index abb2ae1..4046de4 100644
--- a/sbin/ifconfig/ifcarp.c
+++ b/sbin/ifconfig/ifcarp.c
@@ -119,11 +119,12 @@ setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
{
int vhid;
struct carpreq carpr;
+ const char *errstr = NULL;
- vhid = atoi(val);
+ vhid = strtonum(val, 1, INT_MAX, &errstr);
- if (vhid <= 0)
- errx(1, "vhid must be greater than 0");
+ if (errstr)
+ errx(1, "invalid vhid : %s", errstr);
memset((char *)&carpr, 0, sizeof(struct carpreq));
ifr.ifr_data = (caddr_t)&carpr;
@@ -142,8 +143,11 @@ setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
{
int advskew;
struct carpreq carpr;
+ const char *errstr = NULL;
- advskew = atoi(val);
+ advskew = strtonum(val, 1, 255, &errstr);
+ if (errstr)
+ errx(1, "invalid advskew value : %s", errstr);
memset((char *)&carpr, 0, sizeof(struct carpreq));
ifr.ifr_data = (caddr_t)&carpr;
@@ -162,8 +166,11 @@ setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
{
int advbase;
struct carpreq carpr;
+ const char *errstr = NULL;
- advbase = atoi(val);
+ advbase = strtonum(val, 1, 254, &errstr);
+ if (errstr)
+ errx(1, "invalid advbase value : %s", errstr);
memset((char *)&carpr, 0, sizeof(struct carpreq));
ifr.ifr_data = (caddr_t)&carpr;
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
index 0e9363d..c5d2bf8 100644
--- a/sbin/ifconfig/ifconfig.c
+++ b/sbin/ifconfig/ifconfig.c
@@ -779,9 +779,12 @@ static void
setifmetric(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
{
+ const char *errstr = NULL;
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
- ifr.ifr_metric = atoi(val);
- if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
+ ifr.ifr_metric = strtonum(val, 0, INT_MAX, &errstr);
+ if (errstr)
+ warn("invalid metric value %s", errstr);
+ else if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
warn("ioctl (set metric)");
}
@@ -789,9 +792,12 @@ static void
setifmtu(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
{
+ const char *errstr = NULL;
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
- ifr.ifr_mtu = atoi(val);
- if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
+ ifr.ifr_mtu = strtonum(val, 0, INT_MAX, &errstr);
+ if (errstr)
+ warn("invalid mtu value %s", errstr);
+ else if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
warn("ioctl (set mtu)");
}
@@ -799,9 +805,12 @@ static void
setiftsolen(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
{
+ const char *errstr = NULL;
strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
- ifr.ifr_tsolen = atoi(val);
- if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
+ ifr.ifr_tsolen = strtonum(val, 0, INT_MAX, &errstr);
+ if (errstr)
+ warn("invalid tsolen value %s", errstr);
+ else if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
warn("ioctl (set tsolen)");
}
diff --git a/sbin/ifconfig/ifieee80211.c b/sbin/ifconfig/ifieee80211.c
index e0e890b..1e37fe2 100644
--- a/sbin/ifconfig/ifieee80211.c
+++ b/sbin/ifconfig/ifieee80211.c
@@ -1848,13 +1848,13 @@ DECL_CMD_FUNC(set80211meshttl, val, d)
static
DECL_CMD_FUNC(set80211meshforward, val, d)
{
- set80211(s, IEEE80211_IOC_MESH_FWRD, atoi(val), 0, NULL);
+ set80211(s, IEEE80211_IOC_MESH_FWRD, d, 0, NULL);
}
static
DECL_CMD_FUNC(set80211meshpeering, val, d)
{
- set80211(s, IEEE80211_IOC_MESH_AP, atoi(val), 0, NULL);
+ set80211(s, IEEE80211_IOC_MESH_AP, d, 0, NULL);
}
static
diff --git a/sbin/route/route.c b/sbin/route/route.c
index 924c764..e289b92 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -543,6 +543,7 @@ set_metric(char *value, int key)
{
int flag = 0;
u_long noval, *valp = &noval;
+ char *p = NULL;
switch (key) {
#define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
@@ -563,7 +564,9 @@ set_metric(char *value, int key)
rt_metrics.rmx_locks |= flag;
if (locking)
locking = 0;
- *valp = atoi(value);
+ *valp = strtol(value, &p, 10);
+ if (errno == ERANGE || *p != '\0')
+ errx(EX_OSERR, "%s : bad metric", value);
}
static void
diff --git a/sbin/slattach/slattach.c b/sbin/slattach/slattach.c
index f6794b6..35978ef 100644
--- a/sbin/slattach/slattach.c
+++ b/sbin/slattach/slattach.c
@@ -45,6 +45,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <limits.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
@@ -105,6 +106,7 @@ int
main(int argc, char **argv)
{
int option;
+ const char *errstr = NULL;
while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) {
switch (option) {
@@ -135,7 +137,11 @@ main(int argc, char **argv)
redial_cmd = (char*) strdup (optarg);
break;
case 's':
- speed = atoi(optarg);
+ speed = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr) {
+ fprintf(stderr, "bad speed value : %s", errstr);
+ exit(1);
+ }
break;
case 'u':
config_cmd = (char*) strdup (optarg);
diff --git a/sbin/startslip/startslip.c b/sbin/startslip/startslip.c
index c035ec8..fb2fe94 100644
--- a/sbin/startslip/startslip.c
+++ b/sbin/startslip/startslip.c
@@ -42,6 +42,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <limits.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
@@ -115,6 +116,7 @@ main(int argc, char **argv)
pid_t pid;
struct termios t;
int result;
+ const char *errstr = NULL;
while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
switch (ch) {
@@ -122,7 +124,9 @@ main(int argc, char **argv)
debug = 1;
break;
case 'b':
- speed = atoi(optarg);
+ speed = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr)
+ errx(1, "bad speed value (%s)", errstr);
break;
case 's':
if (diali >= MAXDIALS)
@@ -130,10 +134,14 @@ main(int argc, char **argv)
dials[diali++] = strdup(optarg);
break;
case 't':
- script_timeout = atoi(optarg);
+ script_timeout = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr)
+ errx(1, "bad script_timeout value (%s)", errstr);
break;
case 'w':
- wait_time = atoi(optarg);
+ wait_time = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr)
+ errx(1, "bas wait_time value (%s)", errstr);
break;
case 'W':
MAXTRIES = atoi(optarg);
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c
index 14f9d89..f4acacb 100644
--- a/sbin/sysctl/sysctl.c
+++ b/sbin/sysctl/sysctl.c
@@ -208,21 +208,29 @@ parse(const char *string)
(char *)newval);
} else
intval = (int) strtol(newval, NULL, 0);
+ if (errno == ERANGE)
+ errx(1, "value %s out of range", (char *)newval);
newval = &intval;
newsize = sizeof(intval);
break;
case CTLTYPE_UINT:
uintval = (int) strtoul(newval, NULL, 0);
+ if (errno == ERANGE)
+ errx(1, "value %s out of range", (char *)newval);
newval = &uintval;
newsize = sizeof uintval;
break;
case CTLTYPE_LONG:
longval = strtol(newval, NULL, 0);
+ if (errno == ERANGE)
+ errx(1, "value %s out of range", (char *)newval);
newval = &longval;
newsize = sizeof longval;
break;
case CTLTYPE_ULONG:
ulongval = strtoul(newval, NULL, 0);
+ if (errno == ERANGE)
+ errx(1, "value %s out of range", (char *)newval);
newval = &ulongval;
newsize = sizeof ulongval;
break;
diff --git a/sbin/vinum/v.c b/sbin/vinum/v.c
index 13b8de4..e08f5e8 100644
--- a/sbin/vinum/v.c
+++ b/sbin/vinum/v.c
@@ -316,6 +316,7 @@ parseline(int args, char *argv[])
int j;
enum keyword command; /* command to execute */
+ const char *errstr = NULL;
if (hist != NULL) { /* save the command to history file */
timestamp();
for (i = 0; i < args; i++) /* all args */
@@ -360,10 +361,13 @@ parseline(int args, char *argv[])
case 'i': /* interval */
interval = 0;
if (argv[i][j + 1] != '\0') /* operand follows, */
- interval = atoi(&argv[i][j + 1]); /* use it */
+ interval = strtonum(&argv[i][j + 1], 0, INT_MAX, &errstr); /* use it */
else if (args > (i + 1)) /* another following, */
- interval = atoi(argv[++i]); /* use it */
- if (interval == 0) /* nothing valid, */
+ interval = strtonum(argv[++i], 0, INT_MAX, &errstr); /* use it */
+ if (errstr) {
+ fprintf(stderr, "-i: invalid interval specified: %s\n", errstr);
+ exit(1);
+ } else if (interval == 0) /* nothing valid, */
fprintf(stderr, "-i: no interval specified\n");
break;
@@ -387,10 +391,13 @@ parseline(int args, char *argv[])
case 'S':
SSize = 0;
if (argv[i][j + 1] != '\0') /* operand follows, */
- SSize = atoi(&argv[i][j + 1]); /* use it */
+ SSize = strtonum(&argv[i][j + 1], 0, INT_MAX, &errstr); /* use it */
else if (args > (i + 1)) /* another following, */
- SSize = atoi(argv[++i]); /* use it */
- if (SSize == 0) /* nothing valid, */
+ SSize = strtonum(argv[++i], 0, INT_MAX, &errstr); /* use it */
+ if (errstr) {
+ fprintf(stderr, "-S: invalid size specified %s", errstr);
+ exit(1);
+ } else if (SSize == 0) /* nothing valid, */
fprintf(stderr, "-S: no size specified\n");
break;
diff --git a/usr.bin/column/column.c b/usr.bin/column/column.c
index fc1345d..bad2b54 100644
--- a/usr.bin/column/column.c
+++ b/usr.bin/column/column.c
@@ -71,15 +71,18 @@ main(int argc, char **argv)
FILE *fp;
int ch, tflag, xflag;
char *p;
- const char *src;
+ const char *src, *errstr = NULL;;
wchar_t *newsep;
size_t seplen;
setlocale(LC_ALL, "");
if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
- if ((p = getenv("COLUMNS")))
- termwidth = atoi(p);
+ if ((p = getenv("COLUMNS"))) {
+ termwidth = strtonum(p, 0, INT_MAX, &errstr);
+ if (errstr)
+ termwidth = 80;
+ }
} else
termwidth = win.ws_col;
@@ -87,7 +90,9 @@ main(int argc, char **argv)
while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
switch(ch) {
case 'c':
- termwidth = atoi(optarg);
+ termwidth = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr)
+ termwidth = 80;
break;
case 's':
src = optarg;
    (1-1/1)