From 01fe27a77a08c932b6457f1621796a57e6b866d7 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sun, 23 Jun 2024 21:54:03 -0700 Subject: fix InstructionTextSink panicking in write_char --- CHANGELOG | 4 ++++ src/display/display_sink.rs | 4 +++- tests/display.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index b5ac7b8..d8fd706 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,10 @@ TODO: impls of `fn one` and `fn zero` so downstream users don't have to import n TODO: 0.4.0 or later: * remove `mod colors`, crossterm dependency, related feature flags +## 0.3.1 + +fix InstructionTextSink::write_char to not panic in debug builds + ## 0.3.0 added a new crate feature flag, `alloc`. diff --git a/src/display/display_sink.rs b/src/display/display_sink.rs index f5ec026..e83f084 100644 --- a/src/display/display_sink.rs +++ b/src/display/display_sink.rs @@ -453,7 +453,9 @@ mod instruction_text_sink { // write single ASCII characters. this is wrong in the general case, but `write_char` // here is not going to be used in the general case. if cfg!(debug_assertions) { - panic!("InstructionTextSink::write_char would truncate output"); + if c > '\x7f' { + panic!("InstructionTextSink::write_char would truncate output"); + } } let to_push = c as u8; // `ptr::write` here because `underlying.add(underlying.len())` may not point to an diff --git a/tests/display.rs b/tests/display.rs index a6d6eb1..8826303 100644 --- a/tests/display.rs +++ b/tests/display.rs @@ -19,6 +19,35 @@ fn formatters_are_not_feature_gated() { #[cfg(feature="alloc")] #[test] +fn instruction_text_sink_write_char_requires_ascii() { + use core::fmt::Write; + + let mut text = String::with_capacity(512); + let mut sink = unsafe { + yaxpeax_arch::display::InstructionTextSink::new(&mut text) + }; + let expected = "`1234567890-=+_)(*&^%$#@!~\\][poiuytrewq |}{POIUYTREWQ';lkjhgfdsa\":LKJHGFDSA/.,mnbvcxz?>