]> granicus.if.org Git - file/commitdiff
PR/337: Fix incorrect handling of offsets (wrong sign)
authorChristos Zoulas <christos@zoulas.com>
Sat, 12 Apr 2014 15:47:10 +0000 (15:47 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sat, 12 Apr 2014 15:47:10 +0000 (15:47 +0000)
Test:
$ perl -e 'print "ABC", "\xff\xff", "\x00"x70000, "DEF"' > abc.data
$ cat abc.magic
0 string ABC ABC test file format
>(3.s) search/0x3000 DEF \b, DEF sub type
$ file -m abc.magic abc.data
abc.data: ABC test file format, DEF sub type

src/softmagic.c

index 017a578f84bd6721e460e1f6271c19b4fe69bd91..d4f3a64b0db41c39a6c824c9695721df938cc204 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.183 2014/04/01 15:44:26 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.184 2014/04/12 15:47:10 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -1179,6 +1179,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
 {
        uint32_t soffset, offset = ms->offset;
        uint32_t count = m->str_range;
+       uint32_t lhs;
        int rv, oneed_separator, in_type;
        char *sbuf, *rbuf;
        union VALUETYPE *p = &ms->ms_value;
@@ -1281,104 +1282,72 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
                case FILE_BESHORT:
                        if (OFFSET_OOB(nbytes, offset, 2))
                                return 0;
+                       lhs = (p->hs[0] << 8) | p->hs[1];
                        if (off) {
                                switch (m->in_op & FILE_OPS_MASK) {
                                case FILE_OPAND:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) &
-                                                off;
+                                       offset = lhs & off;
                                        break;
                                case FILE_OPOR:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) |
-                                                off;
+                                       offset = lhs | off;
                                        break;
                                case FILE_OPXOR:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) ^
-                                                off;
+                                       offset = lhs ^ off;
                                        break;
                                case FILE_OPADD:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) +
-                                                off;
+                                       offset = lhs + off;
                                        break;
                                case FILE_OPMINUS:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) -
-                                                off;
+                                       offset = lhs - off;
                                        break;
                                case FILE_OPMULTIPLY:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) *
-                                                off;
+                                       offset = lhs * off;
                                        break;
                                case FILE_OPDIVIDE:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) /
-                                                off;
+                                       offset = lhs / off;
                                        break;
                                case FILE_OPMODULO:
-                                       offset = (short)((p->hs[0]<<8)|
-                                                        (p->hs[1])) %
-                                                off;
+                                       offset = lhs % off;
                                        break;
                                }
                        } else
-                               offset = (short)((p->hs[0]<<8)|
-                                                (p->hs[1]));
+                               offset = lhs;
                        if (m->in_op & FILE_OPINVERSE)
                                offset = ~offset;
                        break;
                case FILE_LESHORT:
                        if (OFFSET_OOB(nbytes, offset, 2))
                                return 0;
+                       lhs = (p->hs[1] << 8) | p->hs[0];
                        if (off) {
                                switch (m->in_op & FILE_OPS_MASK) {
                                case FILE_OPAND:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) &
-                                                off;
+                                       offset = lhs & off;
                                        break;
                                case FILE_OPOR:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) |
-                                                off;
+                                       offset = lhs | off;
                                        break;
                                case FILE_OPXOR:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) ^
-                                                off;
+                                       offset = lhs ^ off;
                                        break;
                                case FILE_OPADD:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) +
-                                                off;
+                                       offset = lhs + off;
                                        break;
                                case FILE_OPMINUS:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) -
-                                                off;
+                                       offset = lhs - off;
                                        break;
                                case FILE_OPMULTIPLY:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) *
-                                                off;
+                                       offset = lhs * off;
                                        break;
                                case FILE_OPDIVIDE:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) /
-                                                off;
+                                       offset = lhs / off;
                                        break;
                                case FILE_OPMODULO:
-                                       offset = (short)((p->hs[1]<<8)|
-                                                        (p->hs[0])) %
-                                                off;
+                                       offset = lhs % off;
                                        break;
                                }
                        } else
-                               offset = (short)((p->hs[1]<<8)|
-                                                (p->hs[0]));
+                               offset = lhs;
                        if (m->in_op & FILE_OPINVERSE)
                                offset = ~offset;
                        break;
@@ -1422,70 +1391,37 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
                case FILE_BEID3:
                        if (OFFSET_OOB(nbytes, offset, 4))
                                return 0;
+                       lhs = (p->hl[0] << 24) | (p->hl[1] << 16) |
+                           (p->hl[2] << 8) | p->hl[3];
                        if (off) {
                                switch (m->in_op & FILE_OPS_MASK) {
                                case FILE_OPAND:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) &
-                                                off;
+                                       offset = lhs & off;
                                        break;
                                case FILE_OPOR:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) |
-                                                off;
+                                       offset = lhs | off;
                                        break;
                                case FILE_OPXOR:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) ^
-                                                off;
+                                       offset = lhs ^ off;
                                        break;
                                case FILE_OPADD:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) +
-                                                off;
+                                       offset = lhs + off;
                                        break;
                                case FILE_OPMINUS:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) -
-                                                off;
+                                       offset = lhs - off;
                                        break;
                                case FILE_OPMULTIPLY:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) *
-                                                off;
+                                       offset = lhs * off;
                                        break;
                                case FILE_OPDIVIDE:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) /
-                                                off;
+                                       offset = lhs / off;
                                        break;
                                case FILE_OPMODULO:
-                                       offset = (int32_t)((p->hl[0]<<24)|
-                                                        (p->hl[1]<<16)|
-                                                        (p->hl[2]<<8)|
-                                                        (p->hl[3])) %
-                                                off;
+                                       offset = lhs % off;
                                        break;
                                }
                        } else
-                               offset = (int32_t)((p->hl[0]<<24)|
-                                                (p->hl[1]<<16)|
-                                                (p->hl[2]<<8)|
-                                                (p->hl[3]));
+                               offset = lhs;
                        if (m->in_op & FILE_OPINVERSE)
                                offset = ~offset;
                        break;
@@ -1493,140 +1429,74 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
                case FILE_LEID3:
                        if (OFFSET_OOB(nbytes, offset, 4))
                                return 0;
+                       lhs = (p->hl[3] << 24) | (p->hl[2] << 16) |
+                           (p->hl[1] << 8) | p->hl[0];
                        if (off) {
                                switch (m->in_op & FILE_OPS_MASK) {
                                case FILE_OPAND:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) &
-                                                off;
+                                       offset = lhs & off;
                                        break;
                                case FILE_OPOR:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) |
-                                                off;
+                                       offset = lhs | off;
                                        break;
                                case FILE_OPXOR:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) ^
-                                                off;
+                                       offset = lhs ^ off;
                                        break;
                                case FILE_OPADD:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) +
-                                                off;
+                                       offset = lhs + off;
                                        break;
                                case FILE_OPMINUS:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) -
-                                                off;
+                                       offset = lhs - off;
                                        break;
                                case FILE_OPMULTIPLY:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) *
-                                                off;
+                                       offset = lhs * off;
                                        break;
                                case FILE_OPDIVIDE:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) /
-                                                off;
+                                       offset = lhs / off;
                                        break;
                                case FILE_OPMODULO:
-                                       offset = (int32_t)((p->hl[3]<<24)|
-                                                        (p->hl[2]<<16)|
-                                                        (p->hl[1]<<8)|
-                                                        (p->hl[0])) %
-                                                off;
+                                       offset = lhs % off;
                                        break;
                                }
                        } else
-                               offset = (int32_t)((p->hl[3]<<24)|
-                                                (p->hl[2]<<16)|
-                                                (p->hl[1]<<8)|
-                                                (p->hl[0]));
+                               offset = lhs;
                        if (m->in_op & FILE_OPINVERSE)
                                offset = ~offset;
                        break;
                case FILE_MELONG:
                        if (OFFSET_OOB(nbytes, offset, 4))
                                return 0;
+                       lhs = (p->hl[1] << 24) | (p->hl[0] << 16) |
+                           (p->hl[3] << 8) | p->hl[2];
                        if (off) {
                                switch (m->in_op & FILE_OPS_MASK) {
                                case FILE_OPAND:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) &
-                                                off;
+                                       offset = lhs & off;
                                        break;
                                case FILE_OPOR:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) |
-                                                off;
+                                       offset = lhs | off;
                                        break;
                                case FILE_OPXOR:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) ^
-                                                off;
+                                       offset = lhs ^ off;
                                        break;
                                case FILE_OPADD:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) +
-                                                off;
+                                       offset = lhs + off;
                                        break;
                                case FILE_OPMINUS:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) -
-                                                off;
+                                       offset = lhs - off;
                                        break;
                                case FILE_OPMULTIPLY:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) *
-                                                off;
+                                       offset = lhs * off;
                                        break;
                                case FILE_OPDIVIDE:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) /
-                                                off;
+                                       offset = lhs / off;
                                        break;
                                case FILE_OPMODULO:
-                                       offset = (int32_t)((p->hl[1]<<24)|
-                                                        (p->hl[0]<<16)|
-                                                        (p->hl[3]<<8)|
-                                                        (p->hl[2])) %
-                                                off;
+                                       offset = lhs % off;
                                        break;
                                }
                        } else
-                               offset = (int32_t)((p->hl[1]<<24)|
-                                                (p->hl[0]<<16)|
-                                                (p->hl[3]<<8)|
-                                                (p->hl[2]));
+                               offset = lhs;
                        if (m->in_op & FILE_OPINVERSE)
                                offset = ~offset;
                        break;