diff --git a/src/builtin.c b/src/builtin.c index 35645ea8ac..bf80c7ba9e 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -320,19 +320,17 @@ static jv f_multiply(jq_state *jq, jv input, jv a, jv b) { str = b; num = a; } - int n; - size_t alen = jv_string_length_bytes(jv_copy(str)); - jv res = jv_copy(str); - - for (n = jv_number_value(num) - 1; n > 0; n--) - res = jv_string_append_buf(res, jv_string_value(str), alen); - + jv res = jv_null(); + int n = jv_number_value(num); + if (n > 0) { + size_t alen = jv_string_length_bytes(jv_copy(str)); + res = jv_string_empty(alen * n); + for (; n > 0; n--) { + res = jv_string_append_buf(res, jv_string_value(str), alen); + } + } jv_free(str); jv_free(num); - if (n < 0) { - jv_free(res); - return jv_null(); - } return res; } else if (ak == JV_KIND_OBJECT && bk == JV_KIND_OBJECT) { return jv_object_merge_recursive(a, b);