View Issue Details

IDProjectCategoryView StatusLast Update
0000193fileGeneralpublic2020-09-05 17:17
Reporterneal Assigned Tochristos  
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
Fixed in Version5.40 
Summary0000193: use incorrectly resets the offset
DescriptionConsider the following magic file:

0 byte x
>&10 byte x 1:%d
>>&10 byte x 2:%d
>&20 use foo
>>&10 byte x 3:%d

0 name foo
>&200 byte x 4:%d

I would expect it to print:

/tmp/byte.bin: 1:11 2:22 4:221 3:31

but it actually prints:

/tmp/byte.bin: 1:11 2:22 4:221 3:10

It appears that when a 'use' returns, it does not reset ms->offset to the parent continuation's offset.
Steps To Reproduce$ file -d -m /tmp/offset.magic /tmp/byte.bin
unknown, 0: Warning: using regular magic file `/tmp/offset.magic'
(no description): binary
(no description): text
[try zmagic 0]
[try tar 0]
[try json 0]
[try cdf 0]
[try elf 0]
bb=[0x7f3ab19bb010,256], 0 [b=0x7f3ab19bb010,256], [o=0, c=0]
mget(type=1, flag=0x20, offset=0, o=0, nbytes=256, il=0, nc=0)
mget/96 @0: \000\001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_

1: > 0 byte&,x,""]
0 == *any* = 1
bb=[0x7f3ab19bb010,256], 10 [b=0x7f3ab19bb010,256], [o=0xa, c=1]
mget(type=1, flag=0x2, offset=11, o=0, nbytes=256, il=0, nc=0)
mget/96 @11: \v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij

2: >> 10 byte&,x,"1:%d"]
11 == *any* = 1
bb=[0x7f3ab19bb010,256], 10 [b=0x7f3ab19bb010,256], [o=0xa, c=2]
mget(type=1, flag=0x2, offset=22, o=0, nbytes=256, il=0, nc=0)
mget/96 @22: \026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu

3: >>> 10 byte&,x,"2:%d"]
22 == *any* = 1
bb=[0x7f3ab19bb010,256], 20 [b=0x7f3ab19bb010,256], [o=0x14, c=1]
mget(type=46, flag=0x2, offset=21, o=0, nbytes=256, il=0, nc=0)
mget/96 @21: \025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrst

4: >> 20 use,='foo',""]
bb=[0x7f3ab19bb010,256], 0 [b=0x7f3ab19bb010,256], [o=0, c=0]
mget(type=45, flag=0, offset=0, o=21, nbytes=256, il=0, nc=1)
mget/96 @0: \025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrst

7: > 0 name,='foo',""]
bb=[0x7f3ab19bb010,256], 200 [b=0x7f3ab19bb010,256], [o=0xc8, c=1]
mget(type=1, flag=0x2, offset=200, o=21, nbytes=256, il=0, nc=1)
mget/96 @200: \335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000

8: >> 200 byte&,x,"4:%d"]
18446744073709551581 == *any* = 1
bb=[0x7f3ab19bb010,256], 10 [b=0x7f3ab19bb010,256], [o=0xa, c=2]
mget(type=1, flag=0x2, offset=10, o=0, nbytes=256, il=0, nc=0)
mget/96 @10: \n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghi

5: >>> 10 byte&,x,"3:%d"]
10 == *any* = 1
[try softmagic 1]
/tmp/byte.bin: 1:11 2:22 4:221 3:10
TagsNo tags attached.

Activities

neal

2020-08-27 19:47

reporter  

offset.magic (105 bytes)
byte.bin (256 bytes)

neal

2020-08-27 20:27

reporter   ~0003467

Another example:

0 byte x
>&10 byte x 1:%d
>>&10 byte x 2:%d
>&10 byte x 3:%d
>>&20 use foo
>>>&10 byte x 4:%d
>&10 byte x 5:%d

0 name foo
>&200 byte x 6:%d

$ file -m /tmp/offset.magic /tmp/byte.bin
/tmp/byte.bin: 1:11 2:22 3:11 6:232 4:10 5:10

Since we have "4:10", it appears that not only is the parent continuation's offset ignored, but the offset is reset to 0.

neal

2020-08-28 13:01

reporter   ~0003468

The attached patch appears to fix the problem:

$ src/file -d -m /tmp/offset.magic /tmp/byte.bin
unknown, 0: Warning: using regular magic file `/tmp/offset.magic'
(no description): binary
(no description): text
[try zmagic 0]
[try tar 0]
[try json 0]
[try csv 0]
[try cdf 0]
[try elf 0]
bb=[0x7f6d3cd7c010,256,0], 0 [b=0x7f6d3cd7c010,256,0], [o=0, c=0]
mget(type=1, flag=0x20, offset=0, o=0, nbytes=256, il=0, nc=0)
mget/128 @0: \000\001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177

1: > 0 byte&,x,""]
0 == *any* = 1
bb=[0x7f6d3cd7c010,256,0], 10 [b=0x7f6d3cd7c010,256,0], [o=0xa, c=1]
mget(type=1, flag=0x2, offset=11, o=0, nbytes=256, il=0, nc=0)
mget/128 @11: \v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212

2: >> 10 byte&,x,"1:%d"]
11 == *any* = 1
bb=[0x7f6d3cd7c010,256,0], 10 [b=0x7f6d3cd7c010,256,0], [o=0xa, c=2]
mget(type=1, flag=0x2, offset=22, o=0, nbytes=256, il=0, nc=0)
mget/128 @22: \026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225

3: >>> 10 byte&,x,"2:%d"]
22 == *any* = 1
bb=[0x7f6d3cd7c010,256,0], 10 [b=0x7f6d3cd7c010,256,0], [o=0xa, c=1]
mget(type=1, flag=0x2, offset=11, o=0, nbytes=256, il=0, nc=0)
mget/128 @11: \v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212

4: >> 10 byte&,x,"3:%d"]
11 == *any* = 1
bb=[0x7f6d3cd7c010,256,0], 20 [b=0x7f6d3cd7c010,256,0], [o=0x14, c=2]
mget(type=46, flag=0x2, offset=32, o=0, nbytes=256, il=0, nc=0)
mget/128 @32: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237

5: >>> 20 use,='foo',""]
bb=[0x7f6d3cd7c010,256,0], 0 [b=0x7f6d3cd7c010,256,0], [o=0, c=0]
mget(type=45, flag=0, offset=0, o=32, nbytes=256, il=0, nc=1)
mget/128 @0: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237

9: > 0 name,='foo',""]
bb=[0x7f6d3cd7c010,256,0], 200 [b=0x7f6d3cd7c010,256,0], [o=0xc8, c=1]
mget(type=1, flag=0x2, offset=200, o=32, nbytes=256, il=0, nc=1)
mget/128 @200: \350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000

10: >> 200 byte&,x,"6:%d"]
18446744073709551592 == *any* = 1
bb=[0x7f6d3cd7c010,256,0], 10 [b=0x7f6d3cd7c010,256,0], [o=0xa, c=3]
mget(type=1, flag=0x2, offset=42, o=0, nbytes=256, il=0, nc=0)
mget/128 @42: *+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251

6: >>>> 10 byte&,x,"4:%d"]
42 == *any* = 1
bb=[0x7f6d3cd7c010,256,0], 10 [b=0x7f6d3cd7c010,256,0], [o=0xa, c=1]
mget(type=1, flag=0x2, offset=11, o=0, nbytes=256, il=0, nc=0)
mget/128 @11: \v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212

7: >> 10 byte&,x,"5:%d"]
11 == *any* = 1
[try softmagic 1]
/tmp/byte.bin: 1:11 2:22 3:11 6:232 4:42 5:11



Also, it fixes the value of line 7 (which I had overlooked): it should be 5:11, not 5:10.
0004-Save-ms-offset-around-calls-to-use.patch (1,505 bytes)   
From 8f7abb631219b4fb58359250337aabf1e6ac4476 Mon Sep 17 00:00:00 2001
From: "Neal H. Walfield" <neal@gnu.org>
Date: Fri, 28 Aug 2020 14:47:39 +0200
Subject: [PATCH 4/4] Save ms->offset around calls to use.

---
 src/softmagic.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/softmagic.c b/src/softmagic.c
index f3ad21d0..5ecdd0b4 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -992,6 +992,10 @@ moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
 		o = CAST(int32_t, (ms->offset + 2 * sizeof(uint64_t)));
 		break;
 
+	case FILE_USE:
+		o = ms->offset;
+		break;
+
 	default:
 		o = 0;
 		break;
@@ -1574,6 +1578,7 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
 	union VALUETYPE *p = &ms->ms_value;
 	struct mlist ml;
 	int got_match;
+	uint32_t saved_offset;
 
 	if (*indir_count >= ms->indir_max) {
 		file_error(ms, 0, "indirect count (%hu) exceeded",
@@ -1885,9 +1890,11 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
 			*need_separator = 0;
 		if (file_check_mem(ms, 0, 1) == -1)
 			return -1;
+                saved_offset = ms->offset;
 		rv = match(ms, ml.magic, ml.nmagic, b, offset + o,
 		    mode, text, flip, indir_count, name_count,
 		    printed_something, need_separator, returnval, found_match);
+                ms->offset = saved_offset;
 		// If the named continuation is empty, then return
 		// true.  Otherwise, return the body's 'got_match' value.
 		if (ml.nmagic == 1) {
-- 
2.20.1

christos

2020-09-05 17:17

manager   ~0003477

Fixed, thanks!

Issue History

Date Modified Username Field Change
2020-08-27 19:47 neal New Issue
2020-08-27 19:47 neal File Added: offset.magic
2020-08-27 19:47 neal File Added: byte.bin
2020-08-27 20:27 neal Note Added: 0003467
2020-08-28 13:01 neal File Added: 0004-Save-ms-offset-around-calls-to-use.patch
2020-08-28 13:01 neal Note Added: 0003468
2020-09-05 17:17 christos Assigned To => christos
2020-09-05 17:17 christos Status new => assigned
2020-09-05 17:17 christos Status assigned => resolved
2020-09-05 17:17 christos Resolution open => fixed
2020-09-05 17:17 christos Fixed in Version => 5.40
2020-09-05 17:17 christos Note Added: 0003477