View Issue Details

IDProjectCategoryView StatusLast Update
0000354fileGeneralpublic2022-06-10 14:14
Reportervinc17 Assigned Tochristos  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version5.41 
Fixed in VersionHEAD 
Summary0000354: Fails to detect JSON in case of empty array with spaces
DescriptionWhen a JSON file has an empty array with spaces between the brackets, it is misdetected as a text file.
Steps To Reproduce$ echo '{"a":[ ]}' | file -
/dev/stdin: ASCII text
Additional InformationThis is due to a missing call to json_skip_space in function json_parse_array of src/is_json.c (see json_parse_object as a comparison). I've attached a patch with a testcase.
TagsNo tags attached.

Activities

vinc17

2022-06-06 21:37

reporter  

file-json-array.patch (1,189 bytes)   
Description: Fails to detect JSON in case of empty array with spaces.
Author: Vincent Lefevre <vincent@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))
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b17dfb12..4b2f4989 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -41,6 +41,8 @@ json4.result \
 json4.testfile \
 json5.result \
 json5.testfile \
+json6.result \
+json6.testfile \
 matilde.arm.result \
 matilde.arm.testfile \
 pcjr.result \
diff --git a/tests/json6.result b/tests/json6.result
new file mode 100644
index 00000000..550d7ac2
--- /dev/null
+++ b/tests/json6.result
@@ -0,0 +1 @@
+JSON text data
\ No newline at end of file
diff --git a/tests/json6.testfile b/tests/json6.testfile
new file mode 100644
index 00000000..db310f37
--- /dev/null
+++ b/tests/json6.testfile
@@ -0,0 +1 @@
+{"a":[ ]}
file-json-array.patch (1,189 bytes)   

christos

2022-06-10 14:14

manager   ~0003760

Committed, many thanks (and for providing the test case!).

Issue History

Date Modified Username Field Change
2022-06-06 21:37 vinc17 New Issue
2022-06-06 21:37 vinc17 File Added: file-json-array.patch
2022-06-10 14:14 christos Assigned To => christos
2022-06-10 14:14 christos Status new => assigned
2022-06-10 14:14 christos Status assigned => resolved
2022-06-10 14:14 christos Resolution open => fixed
2022-06-10 14:14 christos Fixed in Version => HEAD
2022-06-10 14:14 christos Note Added: 0003760