Handle non-printable ASCII in str and byte arrays#169
Merged
Conversation
Contributor
Author
|
@nunoplopes, should we add a github runner that flags usages of single-char string literals? |
Contributor
sounds like a bit overkill.. Copilot finds these. |
lucic71
added a commit
to lucic71/cpp2rust
that referenced
this pull request
Jun 4, 2026
`"\xff"` or `'\xff'` are valid strings/char literals in C/C++. They were not escaped and produced an invalid character that broke the compilation. After I escaped them, I realized that they cannot be translated to `\xff` in Rust `str`'s because str only accepts `\x00 - \x7f`. Byte array accepts `> \x7f`. I changed Ptr::from_string_literal to accept a byte array instead of a str. I also changed GetFmtArg to refuse to create Rust `str` that contains chars `> \x7f` and fallback to GetRawArg that produces escaped byte arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
"\xff"or'\xff'are valid strings/char literals in C/C++. They were not escaped and produced an invalid character that broke the compilation.After I escaped them, I realized that they cannot be translated to
\xffin Ruststr's because str only accepts\x00 - \x7f. Byte array accepts> \x7f.I changed Ptr::from_string_literal to accept a byte array instead of a str. I also changed GetFmtArg to refuse to create Rust
strthat contains chars> \x7fand fallback to GetRawArg that produces escaped byte arrays.