summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2019-01-18 22:33:46 -0600
committerNicolas Williams <nico@cryptonector.com>2019-01-18 22:33:46 -0600
commit61cd6dbb3b7f3423d49a1a9beb2b82ad16a34755 (patch)
treed4add40f2a19bb89ded0b62df377b406414d6348
parent4b4fefa254346524c787b862e35e4fbb70e01e95 (diff)
`contains` should handle embedded NULs (fix #1732)
-rw-r--r--src/jv.c3
-rw-r--r--tests/jq.test5
2 files changed, 7 insertions, 1 deletions
diff --git a/src/jv.c b/src/jv.c
index 979d188e..f051d730 100644
--- a/src/jv.c
+++ b/src/jv.c
@@ -1342,7 +1342,8 @@ int jv_contains(jv a, jv b) {
} else if (jv_get_kind(a) == JV_KIND_ARRAY) {
r = jv_array_contains(jv_copy(a), jv_copy(b));
} else if (jv_get_kind(a) == JV_KIND_STRING) {
- r = strstr(jv_string_value(a), jv_string_value(b)) != 0;
+ r = _jq_memmem(jv_string_value(a), jv_string_length_bytes(jv_copy(a)),
+ jv_string_value(b), jv_string_length_bytes(jv_copy(b))) != 0;
} else {
r = jv_equal(jv_copy(a), jv_copy(b));
}
diff --git a/tests/jq.test b/tests/jq.test
index 7e2dd430..041ca1ea 100644
--- a/tests/jq.test
+++ b/tests/jq.test
@@ -1091,6 +1091,11 @@ null
{}
[true, true, false]
+# containment operator (embedded NULs!)
+[contains("foo"), contains("\u0000b"), contains("\u0000z"), contains("bar"), contains("baz")]
+"foo\u0000bar"
+[true, true, false, true, false]
+
# Try/catch and general `?` operator
[.[]|try if . == 0 then error("foo") elif . == 1 then .a elif . == 2 then empty else . end catch .]
[0,1,2,3]