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

string escapes can lead to parse errors via Python API #11

Open
abathur opened this issue Apr 21, 2024 · 0 comments
Open

string escapes can lead to parse errors via Python API #11

abathur opened this issue Apr 21, 2024 · 0 comments

Comments

@abathur
Copy link

abathur commented Apr 21, 2024

Just want to document a sharp corner I ran into while using this. Not sure there's a fix. (It looks like sedparse is parsing correctly and IDK if there's a reliable way to normalize the input in all cases.)

I'm handling a script that has something like the below (I'm simplifying it to just the expression that triggers the error):

sed -E -e "s,.*(query\\[A|DHCP).*,${COL_NC}&${COL_NC},"

I'm using argparse to identify the expressions and just pass those to sedparse, and when it tries to parse the expression above it raises:

...
  File "/nix/store/q0fjmh9bjn7shcbrmcnir34bxlbs7a81-resholve-0.9.1-dirty/bin/.resholve-wrapped", line 3726, in _find_sed_e_cmd
    sedparse.compile_string(parsed, expr)
  File "/nix/store/py54k9w06iqh5x2pssv87mwpmjh702g5-python2.7-sedparse-0.1.2/lib/python2.7/site-packages/sedparse.py", line 1408, in compile_string
    compile_program(cur_program)
  File "/nix/store/py54k9w06iqh5x2pssv87mwpmjh702g5-python2.7-sedparse-0.1.2/lib/python2.7/site-packages/sedparse.py", line 1321, in compile_program
    bad_prog(UNTERM_S_CMD)
  File "/nix/store/py54k9w06iqh5x2pssv87mwpmjh702g5-python2.7-sedparse-0.1.2/lib/python2.7/site-packages/sedparse.py", line 648, in bad_prog
    exitcode=EXIT_BAD_USAGE,
sedparse.ParseError: sedparse: -e expression #3, char 35: unterminated `s' command

I'm blaming escaping because of the behavior I see when I spot-check this from the CLI.

The parse error doesn't happen if I feed a similar string in to the CLI:

$ echo heh | sed -e "s,.*(query\\[A|DHCP).*,&,"
heh

$ ... /sedparse.py -e "s,.*(query\\[A|DHCP).*,&,"
[
    {
        "cmd": "s",
        "line": 1,
        "x": {
            "cmd_subst": {
                "regx": {
                    "pattern": ".*(query\\[A|DHCP).*",
                    "slash": ","
                },
                "replacement": {
                    "text": "&"
                }
            }
        }
    }
]

But if I switch to single quotes, sed and sedparse both emit the same parse error:

$ echo heh | sed -e 's,.*(query\\[A|DHCP).*,&,'
sed: -e expression #1, char 25: unterminated `s' command

$ ... /sedparse.py -e 's,.*(query\\[A|DHCP).*,&,'
sedparse: -e expression #1, char 25: unterminated `s' command

The single-quoted form works in both cases (and the parse tree is the same) if I remove one backslash:

$ echo heh | sed -e 's,.*(query\[A|DHCP).*,&,'
heh

$ ... /sedparse.py -e 's,.*(query\[A|DHCP).*,&,'
[
    {
        "cmd": "s",
        "line": 1,
        "x": {
            "cmd_subst": {
                "regx": {
                    "pattern": ".*(query\\[A|DHCP).*",
                    "slash": ","
                },
                "replacement": {
                    "text": "&"
                }
            }
        }
    }
]
@abathur abathur changed the title unhandled string escapes can lead to parse errors string escapes can lead to parse errors Apr 21, 2024
@abathur abathur changed the title string escapes can lead to parse errors string escapes can lead to parse errors via Python API Apr 21, 2024
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