]> granicus.if.org Git - file/commitdiff
Fix relative offsets after search again, this time I think properly.
authorReuben Thomas <rrt@sc3d.org>
Mon, 25 Feb 2008 02:54:08 +0000 (02:54 +0000)
committerReuben Thomas <rrt@sc3d.org>
Mon, 25 Feb 2008 02:54:08 +0000 (02:54 +0000)
Since this fix was complex (for me!), I'll explain it; the fix also is
slightly more than minimal in order to make the code clearer.

The bug was in lines 1644 and 1652: ms->search.offset = m->offset
overwrites the earlier addition, in lines 192-3, of the relevant offset
to ms->offset. So m->offset should become ms->offset in both lines.

This is a correct fix, but it is not ideal. For starters (and
indepdently of this fix), line 1652 can be changed simply to
ms->search.offset += idx, as this does not mention ms->offset
unnecessarily. Secondly, the code for FILE_REGEX initialises
ms->search.offset in mcopy at about line 820, not in magiccheck at
all, so it makes sense to move FILE_SEARCH's initialisation there too
for consistency. It then becomes
ms->search.offset = offset. We can check that "offset" is earlier set
to "ms->offset" in mcopy's caller, mget, so it's the same value.

src/softmagic.c

index 10fc6f4121e4d8e5667691252889e184aaec0764..b1846293c4644de19eabb8850cc52c5361cee78e 100644 (file)
@@ -38,7 +38,7 @@
 
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.114 2008/02/24 01:17:54 rrt Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.115 2008/02/25 01:05:30 rrt Exp $")
 #endif /* lint */
 
 private int match(struct magic_set *, struct magic *, uint32_t,
@@ -786,6 +786,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
                case FILE_SEARCH:
                        ms->search.s = (const char *)s + offset;
                        ms->search.s_len = nbytes - offset;
+                       ms->search.offset = offset;
                        return 0;
 
                case FILE_REGEX: {
@@ -1641,7 +1642,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
                slen = MIN(m->vallen, sizeof(m->value.s));
                l = 0;
                v = 0;
-               ms->search.offset = m->offset;
 
                for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
                        if (slen + idx > ms->search.s_len)
@@ -1649,7 +1649,7 @@ magiccheck(struct magic_set *ms, struct magic *m)
 
                        v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
                        if (v == 0) {   /* found match */
-                               ms->search.offset = m->offset + idx;
+                               ms->search.offset += idx;
                                break;
                        }
                }