]> granicus.if.org Git - file/commitdiff
bump recursion to 15, and allow it to be set from the command line.
authorChristos Zoulas <christos@zoulas.com>
Thu, 27 Nov 2014 15:40:36 +0000 (15:40 +0000)
committerChristos Zoulas <christos@zoulas.com>
Thu, 27 Nov 2014 15:40:36 +0000 (15:40 +0000)
src/apprentice.c
src/file.c
src/file.h
src/file_opts.h
src/magic.c
src/magic.h.in
src/softmagic.c

index d4fb8afb3f6ba1a70fb8bd84ddb9edc464e45e7f..1b0ff5d7ef998a9d797d46fa6b8259002eaced24 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.223 2014/11/12 15:28:34 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.224 2014/11/27 15:40:36 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -524,6 +524,7 @@ file_ms_alloc(int flags)
                ms->mlist[i] = NULL;
        ms->file = "unknown";
        ms->line = 0;
+       ms->max_recursion = FILE_MAX_RECURSION;
        return ms;
 free:
        free(ms);
index aac9efaefa6325ef04dd8e28d41ec56101e8c5ee..74ad2aa40bc91a332701bca9357e0a00f86074a0 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.155 2014/10/11 15:03:16 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.156 2014/11/27 15:40:36 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -98,7 +98,7 @@ private const struct option long_options[] = {
 #undef OPT_LONGONLY
     {0, 0, NULL, 0}
 };
-#define OPTSTRING      "bcCde:Ef:F:hiklLm:nNprsvz0"
+#define OPTSTRING      "bcCde:Ef:F:hiklLm:nNprR:svz0"
 
 private const struct {
        const char *name;
@@ -137,6 +137,7 @@ main(int argc, char *argv[])
        size_t i;
        int action = 0, didsomefiles = 0, errflg = 0;
        int flags = 0, e = 0;
+       size_t max_recursion = 0;
        struct magic_set *magic = NULL;
        int longindex;
        const char *magicfile = NULL;           /* where the magic is   */
@@ -245,6 +246,9 @@ main(int argc, char *argv[])
                case 'r':
                        flags |= MAGIC_RAW;
                        break;
+               case 'R':
+                       max_recursion = atoi(optarg);
+                       break;
                case 's':
                        flags |= MAGIC_DEVICES;
                        break;
@@ -297,6 +301,8 @@ main(int argc, char *argv[])
                            strerror(errno));
                        return 1;
                }
+
+
                switch(action) {
                case FILE_CHECK:
                        c = magic_check(magic, magicfile);
@@ -320,6 +326,15 @@ main(int argc, char *argv[])
                if (magic == NULL)
                        if ((magic = load(magicfile, flags)) == NULL)
                                return 1;
+               if (max_recursion) {
+                       if (magic_setparam(magic, MAGIC_PARAM_MAX_RECURSION,
+                           &max_recursion) == -1) {
+                               (void)fprintf(stderr,
+                                   "%s: Can't set recurision %s\n", progname,
+                                   strerror(errno));
+                               return 1;
+                       }
+               }
                break;
        }
 
index 5145e0a34c23e814c69388155dcf3a304eeb1f73..aca0e4d3f8c210445eeb7affef9d220f6d553753 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.156 2014/11/23 13:54:27 christos Exp $
+ * @(#)$File: file.h,v 1.157 2014/11/27 15:40:36 christos Exp $
  */
 
 #ifndef __file_h__
@@ -401,6 +401,8 @@ struct magic_set {
        /* FIXME: Make the string dynamically allocated so that e.g.
           strings matched in files can be longer than MAXstring */
        union VALUETYPE ms_value;       /* either number or string */
+       size_t max_recursion;
+#define        FILE_MAX_RECURSION      15
 };
 
 /* Type for Unicode characters */
index db34eb732b05def0467837fa1f6a2d6e9e8035b5..9cb1d01deeee8966d22ba37c34f71503219e10db 100644 (file)
@@ -44,6 +44,7 @@ OPT('0', "print0", 0, "               terminate filenames with ASCII NUL\n")
 OPT('p', "preserve-date", 0, "        preserve access times on files\n")
 #endif
 OPT('r', "raw", 0, "                  don't translate unprintable chars to \\ooo\n")
+OPT('R', "recursion", 0, "            set maximum recursion level\n")
 OPT('s', "special-files", 0, "        treat special (block/char devices) files as\n"
     "                             ordinary ones\n")
 OPT('C', "compile", 0, "              compile file specified by -m\n")
index 371167f516a51559397748e9baf1fe09e8beedf2..39c4a2fbc9651abb3fb93b979ca58c8b9ca8baf8 100644 (file)
@@ -33,7 +33,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.85 2014/08/04 06:19:44 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.86 2014/11/27 15:40:36 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -536,3 +536,29 @@ magic_version(void)
 {
        return MAGIC_VERSION;
 }
+
+public int
+magic_setparam(struct magic_set *ms, int param, const void *val)
+{
+       switch (param) {
+       case MAGIC_PARAM_MAX_RECURSION:
+               ms->max_recursion = *(const size_t *)val;
+               return 0;
+       default:
+               errno = EINVAL;
+               return -1;
+       }
+}
+
+public int
+magic_getparam(struct magic_set *ms, int param, void *val)
+{
+       switch (param) {
+       case MAGIC_PARAM_MAX_RECURSION:
+               *(size_t *)val = ms->max_recursion;
+               return 0;
+       default:
+               errno = EINVAL;
+               return -1;
+       }
+}
index 4a3570523dd41147c04c035109c025620e561f71..9bf7d29015c5626d8e3dd40b621f4406a24956e8 100644 (file)
@@ -96,13 +96,17 @@ int magic_setflags(magic_t, int);
 
 int magic_version(void);
 int magic_load(magic_t, const char *);
-int magic_load_buffers(struct magic_set *, void **, size_t *, size_t);
+int magic_load_buffers(magic_t, void **, size_t *, size_t);
 
 int magic_compile(magic_t, const char *);
 int magic_check(magic_t, const char *);
 int magic_list(magic_t, const char *);
 int magic_errno(magic_t);
 
+#define MAGIC_PARAM_MAX_RECURSION      0
+int magic_setparam(magic_t, int, const void *);
+int magic_getparam(magic_t, int, void *);
+
 #ifdef __cplusplus
 };
 #endif
index 6b7c2d675e3b3fc9fdf5cf62fccff043cb6b93ad..bbb82e60fbe622d13acd12856940ff833111000b 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.198 2014/11/23 13:54:27 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.199 2014/11/27 15:40:36 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -43,10 +43,10 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.198 2014/11/23 13:54:27 christos Exp $")
 #include <time.h>
 
 private int match(struct magic_set *, struct magic *, uint32_t,
-    const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,
+    const unsigned char *, size_t, size_t, int, int, int, size_t, int *, int *,
     int *);
 private int mget(struct magic_set *, const unsigned char *,
-    struct magic *, size_t, size_t, unsigned int, int, int, int, int, int *,
+    struct magic *, size_t, size_t, unsigned int, int, int, int, size_t, int *,
     int *, int *);
 private int magiccheck(struct magic_set *, struct magic *);
 private int32_t mprint(struct magic_set *, struct magic *);
@@ -64,8 +64,6 @@ private void cvt_64(union VALUETYPE *, const struct magic *);
 
 #define OFFSET_OOB(n, o, i)    ((n) < (o) || (i) > ((n) - (o)))
 
-#define MAX_RECURSION_LEVEL    10
-
 /*
  * softmagic - lookup one file in parsed, in-memory copy of database
  * Passed the name and FILE * of one file to be typed.
@@ -136,8 +134,8 @@ file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
 private int
 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
     const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
-    int flip, int recursion_level, int *printed_something, int *need_separator,
-    int *returnval)
+    int flip, size_t recursion_level, int *printed_something,
+    int *need_separator, int *returnval)
 {
        uint32_t magindex = 0;
        unsigned int cont_level = 0;
@@ -1217,7 +1215,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
 private int
 mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
     size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
-    int flip, int recursion_level, int *printed_something,
+    int flip, size_t recursion_level, int *printed_something,
     int *need_separator, int *returnval)
 {
        uint32_t offset = ms->offset;
@@ -1228,8 +1226,9 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
        union VALUETYPE *p = &ms->ms_value;
        struct mlist ml;
 
-       if (recursion_level >= MAX_RECURSION_LEVEL) {
-               file_error(ms, 0, "recursion nesting exceeded");
+       if (recursion_level >= ms->max_recursion) {
+               file_error(ms, 0, "recursion nesting (%zu) exceeded",
+                   recursion_level);
                return -1;
        }