]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/ttyutils: more robust get_terminal_width()
authorKarel Zak <kzak@redhat.com>
Tue, 15 May 2012 09:58:20 +0000 (11:58 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 15 May 2012 09:58:20 +0000 (11:58 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/ttyutils.h
misc-utils/blkid.c

index 15809e85a4590a200347a96270c6bccc38b48a96..3c40d72a40de556398717e598111ffe994fff684 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdlib.h>
 #include <termios.h>
+#include <limits.h>
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
@@ -97,8 +98,17 @@ static inline int get_terminal_width(void)
                return w_win.ws_col;
 #endif
         cp = getenv("COLUMNS");
-       if (cp)
-               return strtol(cp, NULL, 10);
+       if (cp) {
+               char *end = NULL;
+               long c;
+
+               errno = 0;
+               c = strtol(cp, &end, 10);
+
+               if (errno == 0 && end && *end == '\0' && end > cp &&
+                   c > 0 && c <= INT_MAX)
+                       return c;
+       }
        return 0;
 }
 
index aec3528b47dea815e8e6eacea9e5612169754665..5268aba1e1f51a7db6bebbd9b2f20a48bec009c3 100644 (file)
@@ -148,9 +148,11 @@ static void pretty_print_line(const char *device, const char *fs_type,
        static int term_width = -1;
        int len, w;
 
-       if (term_width < 0)
+       if (term_width < 0) {
                term_width = get_terminal_width();
-
+               if (term_width <= 0)
+                       term_width = 80;
+       }
        if (term_width > 80) {
                term_width -= 80;
                w = term_width / 10;