aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2024-06-23 21:54:03 -0700
committeriximeow <me@iximeow.net>2024-06-23 21:54:03 -0700
commit01fe27a77a08c932b6457f1621796a57e6b866d7 (patch)
tree65df47e121eba5bd934ae1338c6f37e01787638a
parentd0b9925bc1f75949f54d5290edfda4bf9ecd7bba (diff)
fix InstructionTextSink panicking in write_char
-rw-r--r--CHANGELOG4
-rw-r--r--src/display/display_sink.rs4
-rw-r--r--tests/display.rs29
3 files changed, 36 insertions, 1 deletions
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?><MNBVCXZ \r\n";
+ for c in expected.as_bytes().iter() {
+ sink.write_char(*c as char).expect("write works");
+ }
+ assert_eq!(text, expected);
+}
+
+#[cfg(feature="alloc")]
+#[test]
+#[should_panic]
+fn instruction_text_sink_write_char_rejects_not_ascii() {
+ use core::fmt::Write;
+
+ let mut text = String::with_capacity(512);
+ let mut sink = unsafe {
+ yaxpeax_arch::display::InstructionTextSink::new(&mut text)
+ };
+ sink.write_char('\u{80}').expect("write works");
+}
+
+#[cfg(feature="alloc")]
+#[test]
fn display_sink_write_hex_helpers() {
use yaxpeax_arch::display::{DisplaySink};