jqlang/jq

gsub loops infinitely with "^" or ""

garethhumphriesgkc opened this issue · 3 comments

Describe the bug
When you pass the pattern "^" or "" to gsub, it loops forever and eventually runs out of memory.

To Reproduce

$ echo '{"key": "value"}' | jq '.key|=gsub("^";"")'
error: cannot allocate memory
Aborted (core dumped)
$ echo '{"key": "value"}' | jq '.key|=gsub("";"")'
^C
$ echo '{"key": "value"}' | jq '.key|=gsub("^";"new-prefix")'
^C
$

Expected behavior
Once the match has been successful, it shouldn't be tried again for the same location.

Environment (please complete the following information):

$ cat /etc/issue ; jq -V ; echo ; dpkg -s jq | grep -e Version -e Package
Ubuntu 20.04 LTS \n \l

jq-1.6

Package: jq
Version: 1.6-1
$

Additional context
Add any other context about the problem here.

An important test case (in jq.test format):

gsub("";"!")
"abc"
"!a!b!c!"
weeble commented

I tried to fix this with #2564, but it still doesn't pass the test case @pkoppstein describes due to #2565.

Fraasi commented

I just ran into this with the pipe symbol, but after looping around for about 30 seconds, it just says killed...?

$ echo '{"key": "value | value"}' | jq '.key|=gsub("|";"-")'
Killed

$ jq --version
jq-1.6