89be67
diff --git a/src/softmagic.c b/src/softmagic.c
89be67
index 56f09ee..8d08cad 100644
89be67
--- a/src/softmagic.c
89be67
+++ b/src/softmagic.c
89be67
@@ -61,6 +61,7 @@ private void cvt_16(union VALUETYPE *, const struct magic *);
89be67
 private void cvt_32(union VALUETYPE *, const struct magic *);
89be67
 private void cvt_64(union VALUETYPE *, const struct magic *);
89be67
 
89be67
+#define OFFSET_OOB(n, o, i)	((n) < (o) || (i) > ((n) - (o)))
89be67
 /*
89be67
  * softmagic - lookup one file in parsed, in-memory copy of database
89be67
  * Passed the name and FILE * of one file to be typed.
89be67
@@ -1081,7 +1082,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 		}
89be67
 		switch (m->in_type) {
89be67
 		case FILE_BYTE:
89be67
-			if (nbytes < (offset + 1))
89be67
+			if (OFFSET_OOB(nbytes, offset, 1))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1116,7 +1117,8 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 				offset = ~offset;
89be67
 			break;
89be67
 		case FILE_BESHORT:
89be67
-			if (nbytes < (offset + 2))
89be67
+			
89be67
+			if (OFFSET_OOB(nbytes, offset, 2))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1168,7 +1170,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 				offset = ~offset;
89be67
 			break;
89be67
 		case FILE_LESHORT:
89be67
-			if (nbytes < (offset + 2))
89be67
+			if (OFFSET_OOB(nbytes, offset, 2))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1220,7 +1222,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 				offset = ~offset;
89be67
 			break;
89be67
 		case FILE_SHORT:
89be67
-			if (nbytes < (offset + 2))
89be67
+			if (OFFSET_OOB(nbytes, offset, 2))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1257,7 +1259,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 			break;
89be67
 		case FILE_BELONG:
89be67
 		case FILE_BEID3:
89be67
-			if (nbytes < (offset + 4))
89be67
+			if (OFFSET_OOB(nbytes, offset, 4))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1328,7 +1330,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 			break;
89be67
 		case FILE_LELONG:
89be67
 		case FILE_LEID3:
89be67
-			if (nbytes < (offset + 4))
89be67
+			if (OFFSET_OOB(nbytes, offset, 4))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1398,7 +1400,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 				offset = ~offset;
89be67
 			break;
89be67
 		case FILE_MELONG:
89be67
-			if (nbytes < (offset + 4))
89be67
+			if (OFFSET_OOB(nbytes, offset, 4))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1468,7 +1470,7 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 				offset = ~offset;
89be67
 			break;
89be67
 		case FILE_LONG:
89be67
-			if (nbytes < (offset + 4))
89be67
+			if (OFFSET_OOB(nbytes, offset, 4))
89be67
 				return 0;
89be67
 			if (off) {
89be67
 				switch (m->in_op & FILE_OPS_MASK) {
89be67
@@ -1535,14 +1537,14 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 	/* Verify we have enough data to match magic type */
89be67
 	switch (m->type) {
89be67
 	case FILE_BYTE:
89be67
-		if (nbytes < (offset + 1)) /* should alway be true */
89be67
+		if (OFFSET_OOB(nbytes, offset, 1))
89be67
 			return 0;
89be67
 		break;
89be67
 
89be67
 	case FILE_SHORT:
89be67
 	case FILE_BESHORT:
89be67
 	case FILE_LESHORT:
89be67
-		if (nbytes < (offset + 2))
89be67
+		if (OFFSET_OOB(nbytes, offset, 2))
89be67
 			return 0;
89be67
 		break;
89be67
 
89be67
@@ -1561,26 +1563,26 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 	case FILE_FLOAT:
89be67
 	case FILE_BEFLOAT:
89be67
 	case FILE_LEFLOAT:
89be67
-		if (nbytes < (offset + 4))
89be67
+		if (OFFSET_OOB(nbytes, offset, 4))
89be67
 			return 0;
89be67
 		break;
89be67
 
89be67
 	case FILE_DOUBLE:
89be67
 	case FILE_BEDOUBLE:
89be67
 	case FILE_LEDOUBLE:
89be67
-		if (nbytes < (offset + 8))
89be67
+		if (OFFSET_OOB(nbytes, offset, 8))
89be67
 			return 0;
89be67
 		break;
89be67
 
89be67
 	case FILE_STRING:
89be67
 	case FILE_PSTRING:
89be67
 	case FILE_SEARCH:
89be67
-		if (nbytes < (offset + m->vallen))
89be67
+		if (OFFSET_OOB(nbytes, offset, m->vallen))
89be67
 			return 0;
89be67
 		break;
89be67
 
89be67
 	case FILE_REGEX:
89be67
-		if (nbytes < offset)
89be67
+		if (OFFSET_OOB(nbytes, offset, 0))
89be67
 			return 0;
89be67
 		break;
89be67