-
Notifications
You must be signed in to change notification settings - Fork 621
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
Constant string concatenation #546
Comments
I don't understand the issue. The following C program prints #include <stdio.h>
int main() {
char* str = "abc""xyz";
printf("%s\n", str);
return 0;
} Can you clarify what pycparser is doing wrong, in your opinion? |
For octal For hexadecimal So if you simply remove consecutive double quotes (what PyCParser does), you get the wrong value
These 2 functions do not return the same value. First one returns 0x07, second one returns 0x3F |
Ah, so it's specific to octal and hex, then... |
As I said, there are not that many solutions
so I won't do a PR, as there is no ideal solution Well, I did create a PR, not sure it will pass the tests (but it works for my needs) From what I saw, it will not pass the I could add the test
and the current version would fail I just saw that |
This seems like it would also apply to all escape sequences. According to §5.1.1.2 — Translation phases:
Also §6.4.5 — String literals:
The standard avoids exactly this issue by ensuring escape sequences in literals are converted before string literals are concatenated. So for sake of correctness, the parser should stop concatenating string literals unless it plans to convert escape sequences. |
The following valid c99 code
is wrongly parsed and returns a c_ast.Constant object with value
'\077'
which is incorrect. Same goes with hexadecimal.The easy solution is to modify CParser.p_unified_string_literal by replacing
p[1].value = p[1].value[:-1] + p[2][1:]
by
p[1].value = p[1].value + p[2]
as simply removing double quotes it not a good idea. The modification would return a value of
'\07""7'
which is better but needs to be parsed to get each characters.Another solution would be to have a list of strings for the value, but that would have way more impacts on other parts of the code (like the generator)
The text was updated successfully, but these errors were encountered: