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

Fix incorrect string len if an exception's message is invalid UTF-8 #1113

Merged
merged 1 commit into from Oct 15, 2022

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Oct 15, 2022

Previously we'd create a PtrLen with the C++ string's length and the String::from_utf8_lossy string's data pointer. This is later turned back into a Rust string by:

cxx/src/result.rs

Lines 61 to 66 in a396524

let err = self.err;
let slice = slice::from_raw_parts_mut(err.ptr.as_ptr(), err.len);
let s = str::from_utf8_unchecked_mut(slice);
Err(Exception {
what: Box::from_raw(s),
})

Since in general the length of string produced by from_utf8_lossy can be longer than the original string, this could end up truncating characters off the end of the exception message when printed from Rust code.

@dtolnay dtolnay merged commit 031723f into master Oct 15, 2022
@dtolnay dtolnay deleted the exception branch October 15, 2022 22:00
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.

None yet

1 participant