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

The number of columns is misplaced when the shfmt parses the backquote command with escape symbols. #1028

Closed
tegixia opened this issue Sep 1, 2023 · 1 comment

Comments

@tegixia
Copy link

tegixia commented Sep 1, 2023

  • The shell statement is as follows:
#!/bin/bash
demo=3
last=`echo \\$demo`

After parsing,The number of columns of the keyword ‘\$demo’ is misplaced.It's supposed to be 12 to 19, but it's actually 12 to 18.

  • shfmt parse tree:
[
    {
        "Pos": {
            "Offset": 27,
            "Line": 3,
            "Col": 7
        },
        "End": {
            "Offset": 31,
            "Line": 3,
            "Col": 11
        },
        "Parts": [
            {
                "Type": "Lit",
                "Pos": {
                    "Offset": 27,
                    "Line": 3,
                    "Col": 7
                },
                "End": {
                    "Offset": 31,
                    "Line": 3,
                    "Col": 11
                },
                "ValuePos": {
                    "Offset": 27,
                    "Line": 3,
                    "Col": 7
                },
                "ValueEnd": {
                    "Offset": 31,
                    "Line": 3,
                    "Col": 11
                },
                "Value": "echo"
            }
        ]
    },
    {
        "Pos": {
            "Offset": 33,
            "Line": 3,
            "Col": 12
        },
        "End": {
            "Offset": 39,
            "Line": 3,
            "Col": 18
        },
        "Parts": [
            {
                "Type": "Lit",
                "Pos": {
                    "Offset": 33,
                    "Line": 3,
                    "Col": 12
                },
                "End": {
                    "Offset": 39,
                    "Line": 3,
                    "Col": 18
                },
                "ValuePos": {
                    "Offset": 33,
                    "Line": 3,
                    "Col": 12
                },
                "ValueEnd": {
                    "Offset": 39,
                    "Line": 3,
                    "Col": 18
                },
                "Value": "\\$demo"
            }
        ]
    }
]
@mvdan
Copy link
Owner

mvdan commented Dec 26, 2023

Thanks. This one was quite tricky to figure out, but I think I have now.

@mvdan mvdan closed this as completed in 28117db Dec 26, 2023
mvdan added a commit that referenced this issue Sep 28, 2024
Like a recent commit did for null bytes.
This makes the code simpler to follow, avoiding crashes like the one
that the fuzzer found and is being added here,
and prevents weirdness where p.w does not correspond to len(p.r)
and we need to special case it in Parser.endLit.

This does go a bit in the opposite direction compared to the fix
we did for #1028, where we tried to adjust positions with backslashes
inside backticks. We still count those backslashes for column positions,
but any position of an escaped character points past the backslashes.

Fixes #1098.
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

2 participants