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/gen | |
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/gen')
-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 |
3 files changed, 66 insertions, 20 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 = ""; |