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

Characters higher than \x7f are being changed to \xff during parser generation #6

Closed
deschutron opened this issue Apr 18, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@deschutron
Copy link

Hi, I had a rule in my grammar that referred to characters higher than 127 ('\x7f'), and it changed all the character escape codes in my rule to \xff.
This is the rule I started out with:

id_uc <-
	[\xc0-\xdf][\x80-\xbf] /
	[\xe0-\xef][\x80-\xbf][\x80-\xbf] /
	[\xf0-\xff][\x80-\xbf][\x80-\xbf][\x80-\xbf]

As I kept simplifying it to track down the problem, the generator kept changing the escape codes to \xff, and leaving lower ones (e.g. \x41) as they are.

I found the cause of the problem too:
On line 487, in the function escape_character, you have a bit that says (unsigned)ch instead of (unsigned char)ch.
Here is the code block that's in:

        if (ch >= '\x20' && ch < '\x7f')
            snprintf(*buf, 5, "%c", ch);
        else
            snprintf(*buf, 5, "\\x%02x", (unsigned)ch);

In your code, it seems you are using the unsigned cast to make sure the higher char values are being fed properly to snprintf, but the C type unsigned is equal to unsigned int, so snprintf is writing strings like \xffffffc0 into the string buffer.
Changing the cast to (unsigned char) makes write them do it correctly (e.g. \xc0).

So can you change that line to this for me?:

            snprintf(*buf, 5, "\\x%02x", (unsigned char)ch);

(Actually it would be for other users, because I've already fixed it in my downloaded copy.)

And thanks for the great tool.

@arithy
Copy link
Owner

arithy commented Apr 26, 2020

Thank you for your report.
I'll fix it with other issues.

@arithy arithy self-assigned this Apr 26, 2020
@arithy arithy added the bug Something isn't working label Apr 26, 2020
@arithy arithy closed this as completed in c1c307e Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants