diff --git a/usr.bin/tabs/tabs.c b/usr.bin/tabs/tabs.c index 9e017a04f8cf..9956e14059a6 100644 --- a/usr.bin/tabs/tabs.c +++ b/usr.bin/tabs/tabs.c @@ -1,4 +1,4 @@ -/* $NetBSD: tabs.c,v 1.5 2019/02/01 08:29:04 mrg Exp $ */ +/* $NetBSD: tabs.c,v 1.6 2021/08/27 18:28:41 rillig Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -33,7 +33,7 @@ #ifndef lint __COPYRIGHT("@(#) Copyright (c) 2008 \ The NetBSD Foundation, inc. All rights reserved."); -__RCSID("$NetBSD: tabs.c,v 1.5 2019/02/01 08:29:04 mrg Exp $"); +__RCSID("$NetBSD: tabs.c,v 1.6 2021/08/27 18:28:41 rillig Exp $"); #endif /* not lint */ #include @@ -42,6 +42,7 @@ __RCSID("$NetBSD: tabs.c,v 1.5 2019/02/01 08:29:04 mrg Exp $"); #include #include #include +#include #include #include #include @@ -84,6 +85,7 @@ main(int argc, char **argv) char *term, *arg, *token, *end, *tabs = NULL, *p; const char *cr, *spec = NULL; int i, n, inc = 8, stops[NSTOPS], nstops, last, cols, margin = 0; + long num = 0; size_t j; struct winsize ws; @@ -97,10 +99,12 @@ main(int argc, char **argv) margin = 10; else { errno = 0; - margin = strtol(arg, &end, 10); - if (errno != 0 || *end != '\0' || margin < 0) + num = strtol(arg, &end, 10); + if (errno != 0 || *end != '\0' || + num < 0 || num > INT_MAX) errx(EXIT_FAILURE, "%s: invalid margin", arg); + margin = (int)num; } continue; } @@ -122,10 +126,10 @@ main(int argc, char **argv) usage(); continue; } - if (isdigit((int)arg[0])) { + if (isdigit((unsigned char)arg[0])) { if (arg[1] != '\0') errx(EXIT_FAILURE, - "%s: invalid increament", arg); + "%s: invalid increment", arg); inc = arg[0] - '0'; continue; } @@ -154,9 +158,10 @@ main(int argc, char **argv) errx(EXIT_FAILURE, "too many tab stops (max %d)", NSTOPS); errno = 0; - n = strtol(token, &end, 10); - if (errno != 0 || *end != '\0' || n <= 0) + num = strtol(token, &end, 10); + if (errno != 0 || *end != '\0' || num <= 0 || num > INT_MAX) errx(EXIT_FAILURE, "%s: invalid tab stop", token); + n = (int)num; if (*token == '+') { if (nstops == 0) errx(EXIT_FAILURE, @@ -203,9 +208,10 @@ main(int argc, char **argv) term = getenv("COLUMNS"); if (term != NULL) { errno = 0; - cols = strtol(term, &end, 10); - if (errno != 0 || *end != '\0' || cols < 0) - cols = 0; + num = strtol(term, &end, 10); + if (errno == 0 && *end == '\0' && + 0 <= cols && cols <= INT_MAX) + cols = (int)num; } if (cols == 0) { if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)