summaryrefslogtreecommitdiff
path: root/bin/ls
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2015-04-18 18:28:39 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2015-04-18 18:28:39 +0000
commit82e40d211902d486d2871a1bc691d1768927efd5 (patch)
tree73caeece4da00dad32b1e62383474772aae90893 /bin/ls
parenta15dfcc7862a97d34cf8fed2bb1292c14721e771 (diff)
Convert many atoi() calls to strtonum(), adding range checks and failure
handling along the way. Reviews by Brendan MacDonell, Jeremy Devenport, florian, doug, millert
Diffstat (limited to 'bin/ls')
-rw-r--r--bin/ls/ls.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/bin/ls/ls.c b/bin/ls/ls.c
index 075677bb23c..2621788eaee 100644
--- a/bin/ls/ls.c
+++ b/bin/ls/ls.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ls.c,v 1.39 2014/03/31 20:54:37 sobrado Exp $ */
+/* $OpenBSD: ls.c,v 1.40 2015/04/18 18:28:36 deraadt Exp $ */
/* $NetBSD: ls.c,v 1.18 1996/07/09 09:16:29 mycroft Exp $ */
/*
@@ -47,6 +47,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <limits.h>
#include <util.h>
#include "ls.h"
@@ -99,22 +100,27 @@ ls_main(int argc, char *argv[])
static char dot[] = ".", *dotav[] = { dot, NULL };
struct winsize win;
int ch, fts_options, notused;
- int kflag = 0;
+ int kflag = 0, width = 0;
char *p;
/* Terminal defaults to -Cq, non-terminal defaults to -1. */
if (isatty(STDOUT_FILENO)) {
if ((p = getenv("COLUMNS")) != NULL)
- termwidth = atoi(p);
- else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
+ width = strtonum(p, 1, INT_MAX, NULL);
+ if (width == 0 &&
+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
win.ws_col > 0)
- termwidth = win.ws_col;
+ width = win.ws_col;
+ if (width)
+ termwidth = width;
f_column = f_nonprint = 1;
} else {
f_singlecol = 1;
/* retrieve environment variable, in case of explicit -C */
if ((p = getenv("COLUMNS")) != NULL)
- termwidth = atoi(p);
+ width = strtonum(p, 0, INT_MAX, NULL);
+ if (width)
+ termwidth = width;
}
/* Root is -A automatically. */