From 90018fe22ff8b74a22fcd142225b0a00f3f12677 Mon Sep 17 00:00:00 2001
From: Christos Zoulas <christos@zoulas.com>
Date: Thu, 27 Nov 2014 15:40:36 +0000
Subject: [PATCH] bump recursion to 15, and allow it to be set from the
 command line.

---
 src/apprentice.c |    3 ++-
 src/file.c       |   19 +++++++++++++++++--
 src/file.h       |    4 +++-
 src/file_opts.h  |    1 +
 src/magic.c      |   28 +++++++++++++++++++++++++++-
 src/magic.h.in   |    6 +++++-
 src/softmagic.c  |   19 +++++++++----------
 7 files changed, 64 insertions(+), 16 deletions(-)

diff --git src/apprentice.c src/apprentice.c
index 2052c78..3d6cb51 100644
--- src/apprentice.c
+++ src/apprentice.c
@@ -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);
diff --git src/file.c src/file.c
index b6c2c38..ac85f69 100644
--- src/file.c
+++ src/file.c
@@ -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;
 	}
 
diff --git src/file.h src/file.h
index fa0f616..52a0cb8 100644
--- src/file.h
+++ src/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 */
diff --git src/file_opts.h src/file_opts.h
index db34eb7..9cb1d01 100644
--- src/file_opts.h
+++ src/file_opts.h
@@ -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")
diff --git src/magic.c src/magic.c
index b2b87c9..e561796 100644
--- src/magic.c
+++ src/magic.c
@@ -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;
+	}
+}
diff --git src/magic.h.in src/magic.h.in
index 4a35705..9bf7d29 100644
--- src/magic.h.in
+++ src/magic.h.in
@@ -101,6 +101,10 @@ 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
diff --git src/softmagic.c src/softmagic.c
index 15dfce5..5fa2810 100644
--- src/softmagic.c
+++ src/softmagic.c
@@ -47,10 +47,10 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.1
 
 
 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;
 	}
 
-- 
1.7.9.2

