From: William Langford Date: Sat, 23 Feb 2019 00:33:24 +0000 (-0500) Subject: Change contains to return true for empty string needles X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=22e6f9e966dc5896a37ad2bc3d9238a66efe518f;p=jq Change contains to return true for empty string needles 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. --- diff --git a/src/jv.c b/src/jv.c index 06b4e6a..2427b00 100644 --- 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)); }