diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1999-09-03 16:23:20 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1999-09-03 16:23:20 +0000 |
commit | 49aebb598146d4d57b680055b575b4923212427c (patch) | |
tree | 5585ce86b34fdabf330c018ebbede316ae795f7a /lib/libc | |
parent | 30af2905c3d217ed35bfdf507d777f1695b39a87 (diff) |
Use strtol() and strtoul() instead of atoi(). This allows us to catch
errors reasonably and deal correctly with unsigned quantities.
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/gen/fstab.c | 33 | ||||
-rw-r--r-- | lib/libc/gen/getgrent.c | 26 | ||||
-rw-r--r-- | lib/libc/gen/getpwent.c | 27 | ||||
-rw-r--r-- | lib/libc/net/getprotoent.c | 10 | ||||
-rw-r--r-- | lib/libc/net/getservent.c | 10 | ||||
-rw-r--r-- | lib/libc/net/res_init.c | 25 |
6 files changed, 95 insertions, 36 deletions
diff --git a/lib/libc/gen/fstab.c b/lib/libc/gen/fstab.c index 3c316644fa8..bf6da99b94f 100644 --- a/lib/libc/gen/fstab.c +++ b/lib/libc/gen/fstab.c @@ -32,13 +32,15 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: fstab.c,v 1.7 1999/08/03 09:18:30 downsj Exp $"; +static char rcsid[] = "$OpenBSD: fstab.c,v 1.8 1999/09/03 16:23:18 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> + #include <errno.h> +#include <limits.h> #include <fstab.h> #include <stdio.h> #include <stdlib.h> @@ -58,7 +60,9 @@ fstabscan() #define MAXLINELENGTH 1024 static char line[MAXLINELENGTH]; char subline[MAXLINELENGTH]; + char *endp; int typexx; + long l; for (;;) { if (!(cp = fgets(line, sizeof(line), _fs_fp))) @@ -78,9 +82,17 @@ fstabscan() strcmp(_fs_fstab.fs_type, FSTAB_SW) ? "ufs" : "swap"; if ((cp = strtok((char *)NULL, ":\n"))) { - _fs_fstab.fs_freq = atoi(cp); + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' || + l < 0 || l >= INT_MAX) + goto bad; + _fs_fstab.fs_freq = l; if ((cp = strtok((char *)NULL, ":\n"))) { - _fs_fstab.fs_passno = atoi(cp); + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' + || l < 0 || l >= INT_MAX) + goto bad; + _fs_fstab.fs_passno = l; return(1); } } @@ -99,9 +111,18 @@ fstabscan() _fs_fstab.fs_freq = 0; _fs_fstab.fs_passno = 0; if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { - _fs_fstab.fs_freq = atoi(cp); - if ((cp = strtok((char *)NULL, " \t\n")) != NULL) - _fs_fstab.fs_passno = atoi(cp); + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' || l < 0 || + l >= INT_MAX) + goto bad; + _fs_fstab.fs_freq = l; + if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' || l < 0 || + l >= INT_MAX) + goto bad; + _fs_fstab.fs_passno = l; + } } strncpy(subline, _fs_fstab.fs_mntops, sizeof subline-1); subline[sizeof subline-1] = '\0'; diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c index 3660df72953..bf33339e90e 100644 --- a/lib/libc/gen/getgrent.c +++ b/lib/libc/gen/getgrent.c @@ -33,7 +33,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getgrent.c,v 1.9 1998/11/20 11:18:37 d Exp $"; +static char rcsid[] = "$OpenBSD: getgrent.c,v 1.10 1999/09/03 16:23:18 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -73,7 +73,7 @@ static int __ypcurrentlen; struct group * getgrent_r(p_gr) -struct group * p_gr; +struct group *p_gr; { _THREAD_PRIVATE_MUTEX_LOCK(gr); if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL, p_gr)) @@ -121,7 +121,7 @@ getgrnam(name) struct group * getgrgid_r(gid, p_gr) gid_t gid; - struct group * p_gr; + struct group *p_gr; { int rval; @@ -214,10 +214,11 @@ grscan(search, gid, name, p_gr) register int search; register gid_t gid; register const char *name; - struct group * p_gr; + struct group *p_gr; { register char *cp, **m; - char *bp; + char *bp, *endp; + u_long ul; #ifdef YP char *key, *data; int keylen, datalen; @@ -343,8 +344,14 @@ grscan(search, gid, name, p_gr) strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) continue; - p_gr->gr_gid = - name ? atoi(cp) : gid; + if (name) { + ul = strtoul(cp, &endp, 10); + if (*endp != '\0' || + endp == cp || ul >= GID_MAX) + continue; + p_gr->gr_gid = ul; + } else + p_gr->gr_gid = gid; goto found_it; } break; @@ -370,7 +377,10 @@ parse: p_gr->gr_passwd = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) continue; - p_gr->gr_gid = atoi(cp); + ul = strtoul(cp, &endp, 10); + if (endp == cp || *endp != '\0' || ul >= GID_MAX) + continue; + p_gr->gr_gid = ul; if (search && name == NULL && p_gr->gr_gid != gid) continue; found_it: diff --git a/lib/libc/gen/getpwent.c b/lib/libc/gen/getpwent.c index 2b1b0d324e7..217352b90ea 100644 --- a/lib/libc/gen/getpwent.c +++ b/lib/libc/gen/getpwent.c @@ -33,7 +33,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getpwent.c,v 1.14 1998/08/14 21:39:29 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: getpwent.c,v 1.15 1999/09/03 16:23:18 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> @@ -235,7 +235,8 @@ __ypparse(pw, s) struct passwd *pw; char *s; { - char *bp, *cp; + char *bp, *cp, *endp; + u_long ul; int count = 0; /* count the colons. */ @@ -251,19 +252,33 @@ char *s; pw->pw_passwd = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) return 1; - pw->pw_uid = atoi(cp); + ul = strtoul(cp, &endp, 10); + if (endp == cp || *endp != '\0' || ul >= UID_MAX) + return 1; + pw->pw_uid = (uid_t)ul; if (!(cp = strsep(&bp, ":\n"))) return 1; - pw->pw_gid = atoi(cp); + ul = strtoul(cp, &endp, 10); + if (endp == cp || *endp != '\0' || ul >= GID_MAX) + return 1; + pw->pw_gid = (gid_t)ul; if (count == 9) { + long l; + /* If the ypserv gave us all the fields, use them. */ pw->pw_class = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) return 1; - pw->pw_change = atoi(cp); + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' || l >= INT_MAX || l <= INT_MIN) + return 1; + pw->pw_change = (time_t)l; if (!(cp = strsep(&bp, ":\n"))) return 1; - pw->pw_expire = atoi(cp); + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' || l >= INT_MAX || l <= INT_MIN) + return 1; + pw->pw_expire = (time_t)l; } else { /* ..else it is a normal ypserv. */ pw->pw_class = ""; diff --git a/lib/libc/net/getprotoent.c b/lib/libc/net/getprotoent.c index 2bef526e7ad..2f8b2676114 100644 --- a/lib/libc/net/getprotoent.c +++ b/lib/libc/net/getprotoent.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.3 1998/03/16 05:06:59 millert Exp $"; +static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.4 1999/09/03 16:23:18 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -74,7 +74,8 @@ endprotoent() struct protoent * getprotoent() { - char *p, *cp, **q; + char *p, *cp, **q, *endp; + long l; size_t len; if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) @@ -102,7 +103,10 @@ again: p = strpbrk(cp, " \t"); if (p != NULL) *p++ = '\0'; - proto.p_proto = atoi(cp); + l = strtol(cp, &endp, 10); + if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX) + goto again; + proto.p_proto = l; q = proto.p_aliases = proto_aliases; if (p != NULL) { cp = p; diff --git a/lib/libc/net/getservent.c b/lib/libc/net/getservent.c index 7d8cb6d8cad..ff6bf1e57f8 100644 --- a/lib/libc/net/getservent.c +++ b/lib/libc/net/getservent.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getservent.c,v 1.4 1998/03/16 05:07:00 millert Exp $"; +static char rcsid[] = "$OpenBSD: getservent.c,v 1.5 1999/09/03 16:23:19 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -74,7 +74,8 @@ endservent() struct servent * getservent() { - char *p, *cp, **q; + char *p, *cp, **q, *endp; + long l; size_t len; if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) @@ -103,7 +104,10 @@ again: if (cp == NULL) goto again; *cp++ = '\0'; - serv.s_port = htons((in_port_t)atoi(p)); + l = strtol(p, &endp, 10); + if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) + goto again; + serv.s_port = htons((in_port_t)l); serv.s_proto = cp; q = serv.s_aliases = serv_aliases; cp = strpbrk(cp, " \t"); diff --git a/lib/libc/net/res_init.c b/lib/libc/net/res_init.c index df176b7fa1d..2e8023ad310 100644 --- a/lib/libc/net/res_init.c +++ b/lib/libc/net/res_init.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $ */ +/* $OpenBSD: res_init.c,v 1.17 1999/09/03 16:23:19 millert Exp $ */ /* * ++Copyright++ 1985, 1989, 1993 @@ -60,7 +60,7 @@ static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $"; #else -static char rcsid[] = "$OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $"; +static char rcsid[] = "$OpenBSD: res_init.c,v 1.17 1999/09/03 16:23:19 millert Exp $"; #endif #endif /* LIBC_SCCS and not lint */ @@ -459,7 +459,8 @@ res_setoptions(options, source) char *options, *source; { char *cp = options; - int i; + char *endp; + long l; #ifdef DEBUG if (_res.options & RES_DEBUG) @@ -472,15 +473,19 @@ res_setoptions(options, source) cp++; /* search for and process individual options */ if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { - i = atoi(cp + sizeof("ndots:") - 1); - if (i <= RES_MAXNDOTS) - _res.ndots = i; - else - _res.ndots = RES_MAXNDOTS; + char *p = cp + sizeof("ndots:") - 1; + l = strtol(p, &endp, 10); + if (l >= 0 && endp != p && + (*endp = '\0' || issapce(*endp))) { + if (l <= RES_MAXNDOTS) + _res.ndots = l; + else + _res.ndots = RES_MAXNDOTS; #ifdef DEBUG - if (_res.options & RES_DEBUG) - printf(";;\tndots=%d\n", _res.ndots); + if (_res.options & RES_DEBUG) + printf(";;\tndots=%d\n", _res.ndots); #endif + } } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { #ifdef DEBUG if (!(_res.options & RES_DEBUG)) { |