File::Glob: silence some compiler warnings
authorDavid Mitchell <davem@iabyn.com>
Wed, 13 Nov 2013 12:33:40 +0000 (12:33 +0000)
committerDavid Mitchell <davem@iabyn.com>
Wed, 13 Nov 2013 17:38:43 +0000 (17:38 +0000)
In bsd_glob.c, there are nested set of functions glob0(), glob1() etc,
which among their parameters pass a pointer to a compiled pattern, and also
to its end. It turns out the end pointer isn't usually used, since the
pattern is usually scanned for BG_EOS instead. Also, glob3() is
passed two pattern ranges, both within the same pattern, and both with
the same pointer to the end of the pattern buffer. Since both end pointers
are the same, eliminate one of them. This removed an unused var compiler
warning. Use the remaining end pointer in the glob*() functions to add
assertions that we haven't fallen off the end of the buffer.

Finally, ARG_MAX may be signed.

ext/File-Glob/bsd_glob.c

index 7ec24df6647f4593d510fde0999ba646678e6c08..8090b194ae4a50c5cbaacb42147239a6dd466b35 100644 (file)
@@ -171,7 +171,7 @@ static int   glob0(const Char *, glob_t *);
 static int      glob1(Char *, Char *, glob_t *, size_t *);
 static int      glob2(Char *, Char *, Char *, Char *, Char *, Char *,
                       glob_t *, size_t *);
-static int      glob3(Char *, Char *, Char *, Char *, Char *, Char *,
+static int      glob3(Char *, Char *, Char *, Char *, Char *,
                       Char *, Char *, glob_t *, size_t *);
 static int      globextend(const Char *, glob_t *, size_t *);
 static const Char *
@@ -639,6 +639,8 @@ glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
 {
        Char pathbuf[MAXPATHLEN];
 
+        assert(pattern < pattern_last);
+
        /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
        if (*pattern == BG_EOS)
                return(0);
@@ -660,6 +662,8 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
        Char *p, *q;
        int anymeta;
 
+        assert(pattern < pattern_last);
+
        /*
         * Loop over pattern segments until end of pattern or until
         * segment with meta character found.
@@ -699,6 +703,7 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
                       && *p != BG_SEP2
 #endif
                       ) {
+                        assert(p < pattern_last);
                        if (ismeta(*p))
                                anymeta = 1;
                        if (q+1 > pathend_last)
@@ -714,6 +719,7 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
                               || *pattern == BG_SEP2
 #endif
                               ) {
+                                assert(p < pattern_last);
                                if (pathend+1 > pathend_last)
                                        return (1);
                                *pathend++ = *pattern++;
@@ -721,7 +727,7 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
                } else
                        /* Need expansion, recurse. */
                        return(glob3(pathbuf, pathbuf_last, pathend,
-                                    pathend_last, pattern, pattern_last,
+                                    pathend_last, pattern,
                                     p, pattern_last, pglob, limitp));
        }
        /* NOTREACHED */
@@ -729,7 +735,7 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
 
 static int
 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
-      Char *pattern, Char *pattern_last,
+      Char *pattern,
       Char *restpattern, Char *restpattern_last, glob_t *pglob, size_t *limitp)
 {
        Direntry_t *dp;
@@ -738,6 +744,9 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
        int nocase;
        char buf[MAXPATHLEN];
 
+        assert(pattern < restpattern_last);
+        assert(restpattern < restpattern_last);
+
        /*
         * The readdirfunc declaration can't be prototyped, because it is
         * assigned, below, to two functions which are prototyped in glob.h
@@ -889,7 +898,7 @@ globextend(const Char *path, glob_t *pglob, size_t *limitp)
        pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
 
        if ((pglob->gl_flags & GLOB_LIMIT) &&
-           newsize + *limitp >= ARG_MAX) {
+           newsize + *limitp >= (unsigned long)ARG_MAX) {
                errno = 0;
                return(GLOB_NOSPACE);
        }