Skip to content

Commit

Permalink
Fix string multiplication by 0 (and less than 1) to emit empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny authored and nicowilliams committed Jul 9, 2023
1 parent 88b4577 commit cac3ea3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,12 @@ static jv f_multiply(jq_state *jq, jv input, jv a, jv b) {
str = b;
num = a;
}
jv res = jv_null();
jv res;
double d = jv_number_value(num);
int n = 0.0 < d && d < 1.0 ? 1 : d;
if (n > 0) {
if (d < 0 || isnan(d)) {
res = jv_null();
} else {
int n = d;
size_t alen = jv_string_length_bytes(jv_copy(str));
res = jv_string_empty(alen * n);
for (; n > 0; n--) {
Expand Down
6 changes: 5 additions & 1 deletion tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,11 @@ indices(", ")

[.[] * "abc"]
[-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 3.7, 10.0]
[null,null,null,"abc","abc","abc","abcabcabc","abcabcabcabcabcabcabcabcabcabc"]
[null,null,"","","abc","abc","abcabcabc","abcabcabcabcabcabcabcabcabcabc"]

[. * (nan,-nan)]
"abc"
[null,null]

[.[] / ","]
["a, bc, def, ghij, jklmn, a,b, c,d, e,f", "a,b,c,d, e,f,g,h"]
Expand Down

0 comments on commit cac3ea3

Please sign in to comment.