Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Fill in unsafe blocks inside unsafe functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 21, 2023
1 parent 0f6dba1 commit 5bac247
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
11 changes: 6 additions & 5 deletions src/libyaml/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,14 @@ impl<'a> Emitter<'a> {

unsafe fn write_handler(data: *mut c_void, buffer: *mut u8, size: u64) -> i32 {
let data = data.cast::<EmitterPinned>();
match io::Write::write_all(
&mut *(*data).write,
slice::from_raw_parts(buffer, size as usize),
) {
match io::Write::write_all(unsafe { &mut *(*data).write }, unsafe {
slice::from_raw_parts(buffer, size as usize)
}) {
Ok(()) => 1,
Err(err) => {
(*data).write_error = Some(err);
unsafe {
(*data).write_error = Some(err);
}
0
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/libyaml/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,41 @@ pub(crate) struct Error {
impl Error {
pub unsafe fn parse_error(parser: *const sys::yaml_parser_t) -> Self {
Error {
kind: (*parser).error,
problem: match NonNull::new((*parser).problem as *mut _) {
Some(problem) => CStr::from_ptr(problem),
kind: unsafe { (*parser).error },
problem: match NonNull::new(unsafe { (*parser).problem as *mut _ }) {
Some(problem) => unsafe { CStr::from_ptr(problem) },
None => CStr::from_bytes_with_nul(b"libyaml parser failed but there is no error\0"),
},
problem_offset: (*parser).problem_offset,
problem_offset: unsafe { (*parser).problem_offset },
problem_mark: Mark {
sys: (*parser).problem_mark,
sys: unsafe { (*parser).problem_mark },
},
context: match NonNull::new((*parser).context as *mut _) {
Some(context) => Some(CStr::from_ptr(context)),
context: match NonNull::new(unsafe { (*parser).context as *mut _ }) {
Some(context) => Some(unsafe { CStr::from_ptr(context) }),
None => None,
},
context_mark: Mark {
sys: (*parser).context_mark,
sys: unsafe { (*parser).context_mark },
},
}
}

pub unsafe fn emit_error(emitter: *const sys::yaml_emitter_t) -> Self {
Error {
kind: (*emitter).error,
problem: match NonNull::new((*emitter).problem as *mut _) {
Some(problem) => CStr::from_ptr(problem),
kind: unsafe { (*emitter).error },
problem: match NonNull::new(unsafe { (*emitter).problem as *mut _ }) {
Some(problem) => unsafe { CStr::from_ptr(problem) },
None => {
CStr::from_bytes_with_nul(b"libyaml emitter failed but there is no error\0")
}
},
problem_offset: 0,
problem_mark: Mark {
sys: MaybeUninit::<sys::yaml_mark_t>::zeroed().assume_init(),
sys: unsafe { MaybeUninit::<sys::yaml_mark_t>::zeroed().assume_init() },
},
context: None,
context_mark: Mark {
sys: MaybeUninit::<sys::yaml_mark_t>::zeroed().assume_init(),
sys: unsafe { MaybeUninit::<sys::yaml_mark_t>::zeroed().assume_init() },
},
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/libyaml/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ unsafe fn convert_event<'input>(
sys::YAML_STREAM_END_EVENT => Event::StreamEnd,
sys::YAML_DOCUMENT_START_EVENT => Event::DocumentStart,
sys::YAML_DOCUMENT_END_EVENT => Event::DocumentEnd,
sys::YAML_ALIAS_EVENT => Event::Alias(optional_anchor(sys.data.alias.anchor).unwrap()),
sys::YAML_ALIAS_EVENT => {
Event::Alias(unsafe { optional_anchor(sys.data.alias.anchor) }.unwrap())
}
sys::YAML_SCALAR_EVENT => Event::Scalar(Scalar {
anchor: optional_anchor(sys.data.scalar.anchor),
tag: optional_tag(sys.data.scalar.tag),
value: Box::from(slice::from_raw_parts(
sys.data.scalar.value,
sys.data.scalar.length as usize,
)),
style: match sys.data.scalar.style {
anchor: unsafe { optional_anchor(sys.data.scalar.anchor) },
tag: unsafe { optional_tag(sys.data.scalar.tag) },
value: Box::from(unsafe {
slice::from_raw_parts(sys.data.scalar.value, sys.data.scalar.length as usize)
}),
style: match unsafe { sys.data.scalar.style } {
sys::YAML_PLAIN_SCALAR_STYLE => ScalarStyle::Plain,
sys::YAML_SINGLE_QUOTED_SCALAR_STYLE => ScalarStyle::SingleQuoted,
sys::YAML_DOUBLE_QUOTED_SCALAR_STYLE => ScalarStyle::DoubleQuoted,
Expand All @@ -133,13 +134,13 @@ unsafe fn convert_event<'input>(
},
}),
sys::YAML_SEQUENCE_START_EVENT => Event::SequenceStart(SequenceStart {
anchor: optional_anchor(sys.data.sequence_start.anchor),
tag: optional_tag(sys.data.sequence_start.tag),
anchor: unsafe { optional_anchor(sys.data.sequence_start.anchor) },
tag: unsafe { optional_tag(sys.data.sequence_start.tag) },
}),
sys::YAML_SEQUENCE_END_EVENT => Event::SequenceEnd,
sys::YAML_MAPPING_START_EVENT => Event::MappingStart(MappingStart {
anchor: optional_anchor(sys.data.mapping_start.anchor),
tag: optional_tag(sys.data.mapping_start.tag),
anchor: unsafe { optional_anchor(sys.data.mapping_start.anchor) },
tag: unsafe { optional_tag(sys.data.mapping_start.tag) },
}),
sys::YAML_MAPPING_END_EVENT => Event::MappingEnd,
sys::YAML_NO_EVENT => unreachable!(),
Expand All @@ -149,13 +150,13 @@ unsafe fn convert_event<'input>(

unsafe fn optional_anchor(anchor: *const u8) -> Option<Anchor> {
let ptr = NonNull::new(anchor as *mut i8)?;
let cstr = CStr::from_ptr(ptr);
let cstr = unsafe { CStr::from_ptr(ptr) };
Some(Anchor(Box::from(cstr.to_bytes())))
}

unsafe fn optional_tag(tag: *const u8) -> Option<Tag> {
let ptr = NonNull::new(tag as *mut i8)?;
let cstr = CStr::from_ptr(ptr);
let cstr = unsafe { CStr::from_ptr(ptr) };
Some(Tag(Box::from(cstr.to_bytes())))
}

Expand Down

0 comments on commit 5bac247

Please sign in to comment.