diff --git a/cpio/cmdline.c b/cpio/cmdline.c index db06c03c..a3d029c6 100644 --- a/cpio/cmdline.c +++ b/cpio/cmdline.c @@ -11,6 +11,9 @@ #ifdef HAVE_ERRNO_H #include #endif +#ifdef HAVE_LIMITS_H +#include +#endif #ifdef HAVE_GRP_H #include #endif @@ -347,9 +350,10 @@ owner_parse(const char *spec, struct cpio_owner *owner, const char **errmsg) owner->gid = pwent->pw_gid; } else { char *end; + unsigned long val; errno = 0; - owner->uid = (int)strtoul(user, &end, 10); - if (errno || *end != '\0') { + val = strtoul(user, &end, 10); + if (errno || *end != '\0' || val > (unsigned)INT_MAX) { snprintf(errbuff, sizeof(errbuff), "Couldn't lookup user ``%s''", user); errbuff[sizeof(errbuff) - 1] = '\0'; @@ -357,6 +361,7 @@ owner_parse(const char *spec, struct cpio_owner *owner, const char **errmsg) *errmsg = errbuff; return (-1); } + owner->uid = (int)val; } free(user); } @@ -373,15 +378,17 @@ owner_parse(const char *spec, struct cpio_owner *owner, const char **errmsg) } } else { char *end; + unsigned long val; errno = 0; - owner->gid = (int)strtoul(g, &end, 10); - if (errno || *end != '\0') { + val = strtoul(g, &end, 10); + if (errno || *end != '\0' || val > (unsigned)INT_MAX) { snprintf(errbuff, sizeof(errbuff), "Couldn't lookup group ``%s''", g); errbuff[sizeof(errbuff) - 1] = '\0'; *errmsg = errbuff; return (-1); } + owner->gid = (int)val; } } return (0); diff --git a/cpio/cpio.c b/cpio/cpio.c index dd5b65ef..b687f3b8 100644 --- a/cpio/cpio.c +++ b/cpio/cpio.c @@ -33,6 +33,9 @@ #ifdef HAVE_LOCALE_H #include #endif +#ifdef HAVE_LIMITS_H +#include +#endif #ifdef HAVE_PWD_H #include #endif @@ -116,7 +119,8 @@ main(int argc, char *argv[]) struct cpio_owner owner; const char *errmsg; char *tptr; - int opt, t; + int opt; + long t; cpio = &_cpio; memset(cpio, 0, sizeof(*cpio)); @@ -204,13 +208,13 @@ main(int argc, char *argv[]) case 'C': /* NetBSD/OpenBSD */ errno = 0; tptr = NULL; - t = (int)strtol(cpio->argument, &tptr, 10); - if (errno || t <= 0 || *(cpio->argument) == '\0' || + t = strtol(cpio->argument, &tptr, 10); + if (errno || t <= 0 || t > INT_MAX || *(cpio->argument) == '\0' || tptr == NULL || *tptr != '\0') { lafe_errc(1, 0, "Invalid blocksize: %s", cpio->argument); } - cpio->bytes_per_block = t; + cpio->bytes_per_block = (int)t; break; case 'c': /* POSIX 1997 */ cpio->format = "odc";