-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1113 from dtolnay/exception
Fix incorrect string len if an exception's message is invalid UTF-8
- Loading branch information
Showing
3 changed files
with
17 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
#![cfg(feature = "alloc")] | ||
|
||
use crate::result::PtrLen; | ||
use alloc::boxed::Box; | ||
use alloc::string::String; | ||
use core::ptr::NonNull; | ||
use core::slice; | ||
|
||
#[export_name = "cxxbridge1$exception"] | ||
unsafe extern "C" fn exception(ptr: *const u8, len: usize) -> *const u8 { | ||
unsafe extern "C" fn exception(ptr: *const u8, len: usize) -> PtrLen { | ||
let slice = unsafe { slice::from_raw_parts(ptr, len) }; | ||
let boxed = String::from_utf8_lossy(slice).into_owned().into_boxed_str(); | ||
Box::leak(boxed).as_ptr() | ||
let string = String::from_utf8_lossy(slice); | ||
let len = string.len(); | ||
let raw_str = Box::into_raw(string.into_owned().into_boxed_str()); | ||
let raw_u8 = raw_str.cast::<u8>(); | ||
let nonnull = unsafe { NonNull::new_unchecked(raw_u8) }; | ||
PtrLen { ptr: nonnull, len } | ||
} |