diff --git a/src/emitter.rs b/src/emitter.rs index 3a7e160..93e7083 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -4,7 +4,6 @@ use crate::api::OUTPUT_BUFFER_SIZE; use crate::macros::{ is_alpha, is_ascii, is_blank, is_blankz, is_bom, is_break, is_breakz, is_printable, is_space, }; -use crate::ops::ForceMul as _; use crate::yaml::YamlEventData; use crate::{ yaml_emitter_flush, yaml_emitter_t, yaml_event_t, yaml_scalar_style_t, yaml_tag_directive_t, @@ -287,7 +286,7 @@ fn yaml_emitter_emit_stream_start( if emitter.best_indent < 2 || emitter.best_indent > 9 { emitter.best_indent = 2; } - if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent.force_mul(2) { + if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent * 2 { emitter.best_width = 80; } if emitter.best_width < 0 { diff --git a/src/lib.rs b/src/lib.rs index 86c3e25..396ebde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,6 @@ mod dumper; mod emitter; mod error; mod loader; -mod ops; mod parser; mod reader; mod scanner; diff --git a/src/ops.rs b/src/ops.rs deleted file mode 100644 index 4597d3d..0000000 --- a/src/ops.rs +++ /dev/null @@ -1,111 +0,0 @@ -pub(crate) trait ForceAdd: Sized { - fn force_add(self, rhs: Self) -> Self; -} - -impl ForceAdd for u8 { - fn force_add(self, rhs: Self) -> Self { - self.checked_add(rhs).unwrap_or_else(die) - } -} - -impl ForceAdd for i32 { - fn force_add(self, rhs: Self) -> Self { - self.checked_add(rhs).unwrap_or_else(die) - } -} - -impl ForceAdd for u32 { - fn force_add(self, rhs: Self) -> Self { - self.checked_add(rhs).unwrap_or_else(die) - } -} - -impl ForceAdd for u64 { - fn force_add(self, rhs: Self) -> Self { - self.checked_add(rhs).unwrap_or_else(die) - } -} - -impl ForceAdd for usize { - fn force_add(self, rhs: Self) -> Self { - self.checked_add(rhs).unwrap_or_else(die) - } -} - -pub(crate) trait ForceMul: Sized { - fn force_mul(self, rhs: Self) -> Self; -} - -impl ForceMul for i32 { - fn force_mul(self, rhs: Self) -> Self { - self.checked_mul(rhs).unwrap_or_else(die) - } -} - -impl ForceMul for i64 { - fn force_mul(self, rhs: Self) -> Self { - self.checked_mul(rhs).unwrap_or_else(die) - } -} - -impl ForceMul for u64 { - fn force_mul(self, rhs: Self) -> Self { - self.checked_mul(rhs).unwrap_or_else(die) - } -} - -pub(crate) trait ForceInto { - fn force_into(self) -> U - where - Self: TryInto; -} - -impl ForceInto for T { - fn force_into(self) -> U - where - Self: TryInto, - { - >::try_into(self) - .ok() - .unwrap_or_else(die) - } -} - -// Deterministically abort on arithmetic overflow, instead of wrapping and -// continuing with invalid behavior. -// -// This is impossible or nearly impossible to hit as the arithmetic computations -// in libyaml are all related to either: -// -// - small integer processing (ascii, hex digits) -// - allocation sizing -// -// and the only allocations in libyaml are for fixed-sized objects and -// geometrically growing buffers with a growth factor of 2. So in order for an -// allocation computation to overflow usize, the previous allocation for that -// container must have been filled to a size of usize::MAX/2, which is an -// allocation that would have failed in the allocator. But we check for this to -// be pedantic and to find out if it ever does happen. -// -// No-std abort is implemented using a double panic. On most platforms the -// current mechanism for this is for core::intrinsics::abort to invoke an -// invalid instruction. On Unix, the process will probably terminate with a -// signal like SIGABRT, SIGILL, SIGTRAP, SIGSEGV or SIGBUS. The precise -// behaviour is not guaranteed and not stable, but is safe. -#[cold] -pub(crate) fn die() -> T { - struct PanicAgain; - - impl Drop for PanicAgain { - fn drop(&mut self) { - panic!("arithmetic overflow"); - } - } - - fn do_die() -> ! { - let _panic_again = PanicAgain; - panic!("arithmetic overflow"); - } - - do_die(); -} diff --git a/src/scanner.rs b/src/scanner.rs index 3be7cfd..505d2d7 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -1,7 +1,6 @@ use alloc::string::String; use crate::macros::{is_blankz, is_break, vecdeque_starts_with}; -use crate::ops::{ForceAdd as _, ForceMul as _}; use crate::reader::yaml_parser_update_buffer; use crate::yaml::YamlTokenData; use crate::{ @@ -109,7 +108,7 @@ pub fn yaml_parser_scan(parser: &mut yaml_parser_t) -> Result Result<(), Scann for simple_key in &mut parser.simple_keys { let mark = simple_key.mark; if simple_key.possible - && (mark.line < parser.mark.line || mark.index.force_add(1024_u64) < parser.mark.index) + && (mark.line < parser.mark.line || mark.index + 1024 < parser.mark.index) { if simple_key.required { return yaml_parser_set_scanner_error( @@ -298,7 +297,7 @@ fn yaml_parser_save_simple_key(parser: &mut yaml_parser_t) -> Result<(), Scanner let simple_key = yaml_simple_key_t { possible: true, required, - token_number: parser.tokens_parsed.force_add(parser.tokens.len()), + token_number: parser.tokens_parsed + parser.tokens.len(), mark: parser.mark, }; yaml_parser_remove_simple_key(parser)?; @@ -425,7 +424,7 @@ fn yaml_parser_fetch_stream_start(parser: &mut yaml_parser_t) { fn yaml_parser_fetch_stream_end(parser: &mut yaml_parser_t) -> Result<(), ScannerError> { if parser.mark.column != 0_u64 { parser.mark.column = 0_u64; - parser.mark.line = parser.mark.line.force_add(1); + parser.mark.line += 1; } yaml_parser_unroll_indent(parser, -1_i64); yaml_parser_remove_simple_key(parser)?; @@ -868,7 +867,7 @@ fn yaml_parser_scan_version_directive_number( let mut length = 0; CACHE(parser, 1)?; while IS_DIGIT!(parser.buffer) { - length = length.force_add(1); + length += 1; if length > MAX_NUMBER_LENGTH { return yaml_parser_set_scanner_error( parser, @@ -877,9 +876,7 @@ fn yaml_parser_scan_version_directive_number( "found extremely long version number", ); } - value = value - .force_mul(10) - .force_add(AS_DIGIT!(parser.buffer) as i32); + value = (value * 10) + AS_DIGIT!(parser.buffer) as i32; SKIP(parser); CACHE(parser, 1)?; } @@ -1149,7 +1146,7 @@ fn yaml_parser_scan_tag_uri( } else { READ_STRING(parser, &mut string); } - length = length.force_add(1); + length += 1; CACHE(parser, 1)?; } if length == 0 { @@ -1605,9 +1602,8 @@ fn yaml_parser_scan_flow_scalar( "did not find expected hexdecimal number", ); } else { - value = (value << 4) - .force_add(AS_HEX_AT!(parser.buffer, k as usize)); - k = k.force_add(1); + value = (value << 4) + AS_HEX_AT!(parser.buffer, k as usize); + k += 1; } } if let Some(ch) = char::from_u32(value) { @@ -1624,7 +1620,7 @@ fn yaml_parser_scan_flow_scalar( k = 0; while k < code_length { SKIP(parser); - k = k.force_add(1); + k += 1; } } } else {