]> granicus.if.org Git - jq/commitdiff
Change contains to return true for empty string needles
authorWilliam Langford <wlangfor@gmail.com>
Sat, 23 Feb 2019 00:33:24 +0000 (19:33 -0500)
committerWilliam Langford <wlangfor@gmail.com>
Sat, 23 Feb 2019 00:50:26 +0000 (19:50 -0500)
The behavior of memmem for an empty needle is inconsistent between
implementations of libc.
Our tests imply that we want an empty string needle to be true,
so check for an empty needle before calling memmem.

src/jv.c

index 06b4e6a29511910ce5f93bda82f43f4d177f9d54..2427b009a99b80f8c6e9a8a448a3f874359aaf9d 100644 (file)
--- a/src/jv.c
+++ b/src/jv.c
@@ -1344,8 +1344,13 @@ 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 = _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;
+    int b_len = jv_string_length_bytes(jv_copy(b));
+    if (b_len != 0) {
+      r = _jq_memmem(jv_string_value(a), jv_string_length_bytes(jv_copy(a)),
+                     jv_string_value(b), b_len) != 0;
+    } else {
+      r = 1;
+    }
   } else {
     r = jv_equal(jv_copy(a), jv_copy(b));
   }