From 4630800a8a9edced40d463b731b57281f3f4c39a Mon Sep 17 00:00:00 2001 From: Simon Ask Ulsnes Date: Wed, 31 Jan 2024 17:23:45 +0100 Subject: [PATCH] Get rid of some unsafe paranoia --- src/bin/run-emitter-test-suite.rs | 2 +- src/bin/run-parser-test-suite.rs | 6 +-- src/yaml.rs | 76 ++----------------------------- 3 files changed, 9 insertions(+), 75 deletions(-) diff --git a/src/bin/run-emitter-test-suite.rs b/src/bin/run-emitter-test-suite.rs index 25d7102..c4a55a9 100644 --- a/src/bin/run-emitter-test-suite.rs +++ b/src/bin/run-emitter-test-suite.rs @@ -127,7 +127,7 @@ pub(crate) unsafe fn unsafe_main( break Err("Memory error: Not enough memory for creating an event".into()); } if yaml_emitter_emit(&mut emitter, event).is_err() { - break Err(match (*emitter).error { + break Err(match emitter.error { YAML_MEMORY_ERROR => "Memory error: Not enough memory for emitting".into(), YAML_WRITER_ERROR => format!( "Writer error: {}", diff --git a/src/bin/run-parser-test-suite.rs b/src/bin/run-parser-test-suite.rs index 18edd7d..be7a2db 100644 --- a/src/bin/run-parser-test-suite.rs +++ b/src/bin/run-parser-test-suite.rs @@ -61,12 +61,12 @@ pub(crate) unsafe fn unsafe_main( loop { if yaml_parser_parse(&mut parser, &mut event).is_err() { let mut error = format!("Parse error: {}", parser.problem.unwrap_or("")); - if parser.problem_mark.line != 0 || (*parser).problem_mark.column != 0 { + if parser.problem_mark.line != 0 || parser.problem_mark.column != 0 { let _ = write!( error, "\nLine: {} Column: {}", - ((*parser).problem_mark.line).wrapping_add(1_u64), - ((*parser).problem_mark.column).wrapping_add(1_u64), + (parser.problem_mark.line).wrapping_add(1_u64), + (parser.problem_mark.column).wrapping_add(1_u64), ); } yaml_parser_delete(&mut parser); diff --git a/src/yaml.rs b/src/yaml.rs index 84358ea..eaa5023 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -3,8 +3,7 @@ use alloc::string::String; use alloc::vec::Vec; use crate::libc; -use core::ops::Deref; -use core::ptr::{self, addr_of}; +use core::ptr; pub use self::yaml_encoding_t::*; pub use core::primitive::{i64 as ptrdiff_t, u64 as size_t, u8 as yaml_char_t}; @@ -672,40 +671,19 @@ pub struct yaml_alias_data_t { #[non_exhaustive] pub struct yaml_parser_t { /// Error type. - #[cfg(doc)] pub error: yaml_error_type_t, - #[cfg(not(doc))] - pub(crate) error: yaml_error_type_t, /// Error description. - #[cfg(doc)] - pub problem: *const libc::c_char, - #[cfg(not(doc))] - pub(crate) problem: Option<&'static str>, + pub problem: Option<&'static str>, /// The byte about which the problem occured. - #[cfg(doc)] pub problem_offset: size_t, - #[cfg(not(doc))] - pub(crate) problem_offset: size_t, /// The problematic value (-1 is none). - #[cfg(doc)] pub problem_value: libc::c_int, - #[cfg(not(doc))] - pub(crate) problem_value: libc::c_int, /// The problem position. - #[cfg(doc)] pub problem_mark: yaml_mark_t, - #[cfg(not(doc))] - pub(crate) problem_mark: yaml_mark_t, /// The error context. - #[cfg(doc)] - pub context: *const libc::c_char, - #[cfg(not(doc))] - pub(crate) context: Option<&'static str>, + pub context: Option<&'static str>, /// The context position. - #[cfg(doc)] pub context_mark: yaml_mark_t, - #[cfg(not(doc))] - pub(crate) context_mark: yaml_mark_t, /// Read handler. pub(crate) read_handler: Option, /// A pointer for passing to the read handler. @@ -805,33 +783,6 @@ impl Default for yaml_parser_t { } } -#[repr(C)] -#[non_exhaustive] -pub struct yaml_parser_t_prefix { - /// Error type. - pub error: yaml_error_type_t, - /// Error description. - pub problem: Option<&'static str>, - /// The byte about which the problem occured. - pub problem_offset: size_t, - /// The problematic value (-1 is none). - pub problem_value: libc::c_int, - /// The problem position. - pub problem_mark: yaml_mark_t, - /// The error context. - pub context: Option<&'static str>, - /// The context position. - pub context_mark: yaml_mark_t, -} - -#[doc(hidden)] -impl Deref for yaml_parser_t { - type Target = yaml_parser_t_prefix; - fn deref(&self) -> &Self::Target { - unsafe { &*addr_of!(*self).cast() } - } -} - #[repr(C)] pub(crate) struct unnamed_yaml_parser_t_input_string { /// The string start pointer. @@ -926,9 +877,9 @@ pub(crate) struct yaml_anchors_t { #[non_exhaustive] pub struct yaml_emitter_t { /// Error type. - pub(crate) error: yaml_error_type_t, + pub error: yaml_error_type_t, /// Error description. - pub(crate) problem: Option<&'static str>, + pub problem: Option<&'static str>, /// Write handler. pub(crate) write_handler: Option, /// A pointer for passing to the write handler. @@ -1048,23 +999,6 @@ impl Default for yaml_emitter_t { } } -#[repr(C)] -#[non_exhaustive] -pub struct yaml_emitter_t_prefix { - /// Error type. - pub error: yaml_error_type_t, - /// Error description. - pub problem: Option<&'static str>, -} - -#[doc(hidden)] -impl Deref for yaml_emitter_t { - type Target = yaml_emitter_t_prefix; - fn deref(&self) -> &Self::Target { - unsafe { &*addr_of!(*self).cast() } - } -} - #[repr(C)] pub(crate) struct unnamed_yaml_emitter_t_output_string { /// The buffer pointer.