Package: file
Version: 1:5.41-4
Severity: normal
Tags: upstream patch
Forwarded: https://bugs.astron.com/view.php?id=354

When a JSON file has an empty array with spaces between the brackets,
it is misdetected as a text file:

$ echo '{"a":[ ]}' | file -
/dev/stdin: ASCII text

I've attached a patch (same fix as I've submitted upstream).

-- System Information:
Debian Release: bookworm/sid
  APT prefers unstable-debug
  APT policy: (500, 'unstable-debug'), (500, 'stable-updates'), (500, 
'stable-security'), (500, 'unstable'), (500, 'testing'), (500, 'stable'), (1, 
'experimental')
Architecture: amd64 (x86_64)

Kernel: Linux 5.17.0-2-amd64 (SMP w/8 CPU threads; PREEMPT)
Kernel taint flags: TAINT_PROPRIETARY_MODULE, TAINT_OOT_MODULE, 
TAINT_UNSIGNED_MODULE
Locale: LANG=POSIX, LC_CTYPE=C.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages file depends on:
ii  libc6      2.33-7
ii  libmagic1  1:5.41-4

file recommends no packages.

file suggests no packages.

-- no debconf information

-- 
Vincent Lefèvre <vinc...@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
Description: Fails to detect JSON in case of empty array with spaces.
Author: Vincent Lefevre <vinc...@vinc17.net>
Last-Update: 2022-06-06

diff --git a/src/is_json.c b/src/is_json.c
index c276b7be..36cccbdf 100644
--- a/src/is_json.c
+++ b/src/is_json.c
@@ -183,6 +183,7 @@ json_parse_array(const unsigned char **ucp, const unsigned char *ue,
 
 	DPRINTF("Parse array: ", uc, *ucp);
 	while (uc < ue) {
+		uc = json_skip_space(uc, ue);
 		if (*uc == ']')
 			goto done;
 		if (!json_parse(&uc, ue, st, lvl + 1))

Reply via email to