[hackers] [sbase] Adjust some limits to more flexibility for strtonum || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 24 Mar 2015 23:53:32 +0100 (CET)

commit 27b770c02c3e936d12d3e0606e6ebf6c68b145e6
Author: FRIGN <dev_AT_frign.de>
Date: Sun Feb 1 01:24:03 2015 +0100

    Adjust some limits to more flexibility for strtonum

diff --git a/cols.c b/cols.c
index ce0018c..e377371 100644
--- a/cols.c
+++ b/cols.c
_AT_@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <assert.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/date.c b/date.c
index db69091..467270e 100644
--- a/date.c
+++ b/date.c
_AT_@ -26,7 +26,7 @@ main(int argc, char *argv[])
         t = time(NULL);
         ARGBEGIN {
         case 'd':
- t = estrtonum(EARGF(usage()), 0, LLONG_MAX);
+ t = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, (time_t)-1));
                 break;
         case 'u':
                 tztime = gmtime;
diff --git a/du.c b/du.c
index b900622..6d96190 100644
--- a/du.c
+++ b/du.c
_AT_@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <dirent.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -10,10 +11,10 @@
 
 #include "util.h"
 
-static long blksize = 512;
-static char file[PATH_MAX];
-static long depth = -1;
-static long curdepth = 0;
+static size_t blksize = 512;
+static char file[PATH_MAX];
+static size_t depth = -1;
+static size_t curdepth = 0;
 
 static int aflag = 0;
 static int dflag = 0;
_AT_@ -21,15 +22,6 @@ static int sflag = 0;
 static int kflag = 0;
 static int hflag = 0;
 
-static long du(const char *);
-static void print(long n, char *path);
-
-static void
-usage(void)
-{
- eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [file...]\n", argv0);
-}
-
 static char *
 xrealpath(const char *pathname, char *resolved)
 {
_AT_@ -41,60 +33,8 @@ xrealpath(const char *pathname, char *resolved)
         return r;
 }
 
-int
-main(int argc, char *argv[])
-{
- char *bsize;
- long n;
-
- ARGBEGIN {
- case 'a':
- aflag = 1;
- break;
- case 'd':
- dflag = 1;
- depth = estrtonum(EARGF(usage()), 0, LONG_MAX);
- break;
- case 's':
- sflag = 1;
- break;
- case 'k':
- kflag = 1;
- break;
- case 'h':
- hflag = 1;
- break;
- default:
- usage();
- } ARGEND;
-
- if ((aflag && sflag) || (dflag && sflag))
- usage();
-
- bsize = getenv("BLOCKSIZE");
- if (bsize)
- blksize = estrtonum(bsize, 0, LONG_MAX);
-
- if (kflag)
- blksize = 1024;
-
- if (argc < 1) {
- n = du(".");
- if (sflag)
- print(n, xrealpath(".", file));
- } else {
- for (; argc > 0; argc--, argv++) {
- curdepth = 0;
- n = du(argv[0]);
- if (sflag)
- print(n, xrealpath(argv[0], file));
- }
- }
- return 0;
-}
-
 static void
-print(long n, char *path)
+print(size_t n, char *path)
 {
         if (hflag)
                 printf("%s\t%s\n", humansize(n * blksize), path);
_AT_@ -121,21 +61,21 @@ pop(char *path)
         free(path);
 }
 
-static long
+static size_t
 nblks(struct stat *st)
 {
         return (512 * st->st_blocks + blksize - 1) / blksize;
 }
 
-static long
+static size_t
 du(const char *path)
 {
- DIR *dp;
- char *cwd;
         struct dirent *dent;
         struct stat st;
- long n = 0, m, t;
+ DIR *dp;
+ size_t n = 0, m, t;
         int r;
+ char *cwd;
 
         if (lstat(path, &st) < 0)
                 eprintf("stat: %s:", path);
_AT_@ -187,3 +127,61 @@ done:
                 print(n, xrealpath(path, file));
         return n;
 }
+
+static void
+usage(void)
+{
+ eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [file ...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ size_t n;
+ char *bsize;
+
+ ARGBEGIN {
+ case 'a':
+ aflag = 1;
+ break;
+ case 'd':
+ dflag = 1;
+ depth = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
+ break;
+ case 's':
+ sflag = 1;
+ break;
+ case 'k':
+ kflag = 1;
+ break;
+ case 'h':
+ hflag = 1;
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if ((aflag && sflag) || (dflag && sflag))
+ usage();
+
+ bsize = getenv("BLOCKSIZE");
+ if (bsize)
+ blksize = estrtonum(bsize, 0, LONG_MAX);
+
+ if (kflag)
+ blksize = 1024;
+
+ if (argc < 1) {
+ n = du(".");
+ if (sflag)
+ print(n, xrealpath(".", file));
+ } else {
+ for (; argc > 0; argc--, argv++) {
+ curdepth = 0;
+ n = du(argv[0]);
+ if (sflag)
+ print(n, xrealpath(argv[0], file));
+ }
+ }
+ return 0;
+}
diff --git a/expand.c b/expand.c
index 5147f92..2d1cc84 100644
--- a/expand.c
+++ b/expand.c
_AT_@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -7,9 +8,9 @@
 #include "utf.h"
 #include "util.h"
 
-static int iflag = 0;
-static size_t *tablist = NULL;
-static size_t tablistlen = 0;
+static int iflag = 0;
+static size_t *tablist = NULL;
+static size_t tablistlen = 0;
 
 static size_t
 parselist(const char *s)
_AT_@ -22,7 +23,7 @@ parselist(const char *s)
                 if (*p == '\0')
                         eprintf("empty field in tablist\n");
                 tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
- tablist[i] = estrtonum(p, 1, LLONG_MAX);
+ tablist[i] = estrtonum(p, 1, MIN(LLONG_MAX, SIZE_MAX));
                 if (i > 0 && tablist[i - 1] >= tablist[i])
                         eprintf("tablist must be ascending\n");
         }
diff --git a/fold.c b/fold.c
index 95783cf..50da75d 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <ctype.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
_AT_@ -84,7 +85,7 @@ main(int argc, char *argv[])
                 sflag = 1;
                 break;
         case 'w':
- width = estrtonum(EARGF(usage()), 1, LLONG_MAX);
+ width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
                 break;
         ARGNUM:
                 width = ARGNUMF();
diff --git a/head.c b/head.c
index 31845d3..8fb0195 100644
--- a/head.c
+++ b/head.c
_AT_@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -34,14 +35,14 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
- long n = 10;
+ size_t n = 10;
         FILE *fp;
         int ret = 0;
         int newline, many;
 
         ARGBEGIN {
         case 'n':
- n = estrtonum(EARGF(usage()), 0, LONG_MAX);
+ n = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
                 break;
         ARGNUM:
                 n = ARGNUMF();
diff --git a/nl.c b/nl.c
index 9d9bef8..be904ba 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -2,6 +2,7 @@
 #include <limits.h>
 #include <regex.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
_AT_@ -9,12 +10,29 @@
 #include "text.h"
 #include "util.h"
 
-static void nl(const char *, FILE *);
-
-static char mode = 't';
+static char mode = 't';
 static const char *sep = "\t";
-static long incr = 1;
-static regex_t preg;
+static size_t incr = 1;
+static regex_t preg;
+
+void
+nl(const char *name, FILE *fp)
+{
+ char *buf = NULL;
+ size_t n = 0, size = 0;
+
+ while (getline(&buf, &size, fp) != -1) {
+ if ((mode == 'a')
+ || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
+ || (mode == 't' && buf[0] != '\n'))
+ printf("%6ld%s%s", n += incr, sep, buf);
+ else
+ printf(" %s", buf);
+ }
+ free(buf);
+ if (ferror(fp))
+ eprintf("%s: read error:", name);
+}
 
 static void
 usage(void)
_AT_@ -38,7 +56,7 @@ main(int argc, char *argv[])
                         usage();
                 break;
         case 'i':
- incr = estrtonum(EARGF(usage()), 0, LONG_MAX);
+ incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
                 break;
         case 's':
                 sep = EARGF(usage());
_AT_@ -60,23 +78,3 @@ main(int argc, char *argv[])
         }
         return 0;
 }
-
-void
-nl(const char *name, FILE *fp)
-{
- char *buf = NULL;
- long n = 0;
- size_t size = 0;
-
- while (getline(&buf, &size, fp) != -1) {
- if ((mode == 'a')
- || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
- || (mode == 't' && buf[0] != '\n'))
- printf("%6ld%s%s", n += incr, sep, buf);
- else
- printf(" %s", buf);
- }
- free(buf);
- if (ferror(fp))
- eprintf("%s: read error:", name);
-}
diff --git a/seq.c b/seq.c
index 1b17a58..e352ab2 100644
--- a/seq.c
+++ b/seq.c
_AT_@ -110,7 +110,7 @@ digitsright(const char *d)
         int shift, after;
 
         exp = strpbrk(d, "eE");
- shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0;
+ shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0;
         after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
 
         return MAX(0, after - shift);
diff --git a/split.c b/split.c
index b71c795..6151ef4 100644
--- a/split.c
+++ b/split.c
_AT_@ -28,7 +28,7 @@ main(int argc, char *argv[])
         char *prefix = "x";
         char *file = NULL;
         char *tmp, *end;
- uint64_t size = 1000, scale = 1, n;
+ size_t size = 1000, scale = 1, n;
         int always = 0;
         FILE *in = stdin, *out = NULL;
 
_AT_@ -55,7 +55,7 @@ main(int argc, char *argv[])
                 default:
                         usage();
                 }
- if (size > (UINT64_MAX/scale))
+ if (size > (SIZE_MAX/scale))
                         eprintf("'%s': out of range\n", tmp);
                 size *= scale;
                 break;
_AT_@ -63,7 +63,7 @@ main(int argc, char *argv[])
                 always = 0;
                 tmp = ARGF();
                 if (tmp)
- size = estrtonum(tmp, 0, LLONG_MAX);
+ size = estrtonum(tmp, 0, MIN(LLONG_MAX, SIZE_MAX));
                 break;
         case 'a':
                 slen = estrtonum(EARGF(usage()), 0, INT_MAX);
diff --git a/touch.c b/touch.c
index 753d4f5..b762772 100644
--- a/touch.c
+++ b/touch.c
_AT_@ -65,7 +65,7 @@ main(int argc, char *argv[])
                 mflag = 1;
                 break;
         case 't':
- t = estrtonum(EARGF(usage()), 0, LLONG_MAX);
+ t = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, (time_t)-1));
                 break;
         default:
                 usage();
Received on Tue Mar 24 2015 - 23:53:32 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 25 2015 - 00:06:53 CET