From 70fbb8bf04f8d2221c88b41cf4387f9ef94554d1 Mon Sep 17 00:00:00 2001
From: "Neal H. Walfield" <neal@gnu.org>
Date: Tue, 1 Sep 2020 13:53:27 +0200
Subject: [PATCH 5/5] Change mget to return whether a USE execute, not whether
 it matched.

  - Consider the following magic:

      0 byte x
      >0 use foo
      >>0 byte x 1
      >>0 use bar

      0 name foo
      >0 byte x 2

      0 name bar
      >0 byte x 3
      !:mime application/3

    Executing normally and getting the mime type result in different
    control flows:

      $ file -m /tmp/mime.magic /tmp/byte.bin
      /tmp/byte.bin: 2 1 3
      $ file --mime-type -m /tmp/mime.magic /tmp/byte.bin
      /tmp/byte.bin: application/octet-stream

    'bar' is executed in the first case, because 'foo' output '2', but
    not in the second case, because no lines in 'foo' have a mime
    annotation.

    This happens, because mget's handling of FILE_USE returns whether
    a match occured, not whether the required data was loaded, which
    is what it returns.

  - Change mget to return whether the use executed.  Let magiccheck
    handle whether the entry matched anything.

    See https://bugs.astron.com/view.php?id=195 for more details.
---
 src/softmagic.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/softmagic.c b/src/softmagic.c
index 5ecdd0b4..3681c75c 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -1907,7 +1907,10 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
 		(*name_count)--;
 		if (rv != 1)
 		    *need_separator = oneed_separator;
-		return rv;
+		if (rv == -1)
+			return -1;
+		else
+			return 1;
 
 	case FILE_NAME:
 		if (ms->flags & MAGIC_NODESC)
-- 
2.20.1

