Skip to content

Handle non-printable ASCII in str and byte arrays#169

Merged
nunoplopes merged 5 commits into
Cpp2Rust:masterfrom
lucic71:fix-escape-char
Jun 3, 2026
Merged

Handle non-printable ASCII in str and byte arrays#169
nunoplopes merged 5 commits into
Cpp2Rust:masterfrom
lucic71:fix-escape-char

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented Jun 3, 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.

@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented Jun 3, 2026

@nunoplopes, should we add a github runner that flags usages of single-char string literals?

@nunoplopes nunoplopes merged commit cee15a3 into Cpp2Rust:master Jun 3, 2026
9 checks passed
@nunoplopes
Copy link
Copy Markdown
Contributor

@nunoplopes, should we add a github runner that flags usages of single-char string literals?

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.
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

Successfully merging this pull request may close these issues.

2 participants