Skip to content

gsub loops infinitely with "^" or "" #2148

@garethhumphriesgkc

Description

@garethhumphriesgkc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions