Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/tabs tabs: fix lint warnings about ctype functions a...



details:   https://anonhg.NetBSD.org/src/rev/13f80b80fe69
branches:  trunk
changeset: 1023150:13f80b80fe69
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Aug 27 18:28:41 2021 +0000

description:
tabs: fix lint warnings about ctype functions and conversions

lines 100, 157, 206: conversion from 'long' to 'int' may lose accuracy
[132]

line 125: warning: argument to 'function from <ctype.h>' must be cast to
'unsigned char', not to 'int' [342]

While here, fix a typo in an error message.

diffstat:

 usr.bin/tabs/tabs.c |  28 +++++++++++++++++-----------
 1 files changed, 17 insertions(+), 11 deletions(-)

diffs (89 lines):

diff -r 4cf28198d814 -r 13f80b80fe69 usr.bin/tabs/tabs.c
--- a/usr.bin/tabs/tabs.c       Fri Aug 27 18:11:07 2021 +0000
+++ b/usr.bin/tabs/tabs.c       Fri Aug 27 18:28:41 2021 +0000
@@ -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 <sys/ioctl.h>
@@ -42,6 +42,7 @@
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -84,6 +85,7 @@
        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 @@
                                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 @@
                                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 @@
                        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 @@
                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)



Home | Main Index | Thread Index | Old Index