Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing an unclosed string can take a very long time (likely exponential in the length of the string) #76

Open
giuli007 opened this issue Jul 27, 2021 · 0 comments

Comments

@giuli007
Copy link

giuli007 commented Jul 27, 2021

I have found that using hcl2.loads on a string that contains a variable that is assigned to a string that is missing the closing double quote " can take a long time before erroring with lark.exceptions.UnexpectedCharacters: No terminal defined for '"' at line 1 col 14

e.g. a simple snippet like

a_variable = "0123456789abcdef

takes 37s on my pretty big-sized machine.

I've tested this with python-hcl2==2.0.0 and the latest python-hcl2==3.0.1

The following script shows how long it takes before the library throws an exception with invalid strings of increasing lenght.

import time
import hcl2

def test(s):
    code = 'a_variable = "{}'.format(s)
    try:
        hcl2.loads(code)
    except:
        pass

for s in ['0123456789ab', '0123456789abc', '0123456789abcd', '0123456789abcde', '0123456789abcdef']:
    start = time.time()
    test(s)
    print('{:<16} of length {} took {}'.format(s, len(s), time.time() - start))

# outputs:
# 0123456789ab     of length 12 took 0.46656274795532227
# 0123456789abc    of length 13 took 1.3698146343231201
# 0123456789abcd   of length 14 took 4.1063151359558105
# 0123456789abcde  of length 15 took 12.480230569839478
# 0123456789abcdef of length 16 took 37.197903871536255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant