aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2021-07-06 21:42:09 -0700
committeriximeow <me@iximeow.net>2021-07-06 21:45:56 -0700
commit12bba22a456ed452efda8c951f61bdff1a516252 (patch)
tree410b22cf7a1253c83506651831dd7a9d91a8f96b
parentf393fae8906a0bc9431743db09bf6268b8f3810c (diff)
fix incorrect `offset` and `total_offset` counts for non-`u8` Word0.2.4
also update yaxpeax-arch to 0.2.4
-rw-r--r--CHANGELOG2
-rw-r--r--Cargo.toml2
-rw-r--r--src/reader.rs7
-rw-r--r--tests/lib.rs2
-rw-r--r--tests/reader.rs24
5 files changed, 34 insertions, 3 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 31aa495..8bce871 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,8 @@
added `Reader` impls for `U8Reader` on `u16` addresses
+fix incorrect `Reader` impls of `offset` and `total_offset` on non-`u8` words
+
## 0.2.2
added `ReaderBuilder` trait and impls for `U8Reader` on various address and word types.
diff --git a/Cargo.toml b/Cargo.toml
index 2325591..351f07b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,7 +7,7 @@ keywords = ["disassembly", "disassembler"]
license = "0BSD"
name = "yaxpeax-arch"
repository = "https://git.iximeow.net/yaxpeax-arch/"
-version = "0.2.3"
+version = "0.2.4"
[dependencies]
"num-traits" = { version = "0.2", default-features = false }
diff --git a/src/reader.rs b/src/reader.rs
index db72b79..acb0146 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -6,6 +6,7 @@ impl From<ReadError> for StandardDecodeError {
}
}
+#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ReadError {
ExhaustedInput,
IOError(&'static str),
@@ -167,11 +168,13 @@ macro_rules! u8reader_reader_impl {
}
#[inline]
fn offset(&mut self) -> $addr_size {
- (self.data as usize - self.mark as usize) as $addr_size
+ (self.data as usize - self.mark as usize) as $addr_size /
+ (core::mem::size_of::<$word>() as $addr_size)
}
#[inline]
fn total_offset(&mut self) -> $addr_size {
- (self.data as usize - self.start as usize) as $addr_size
+ (self.data as usize - self.start as usize) as $addr_size /
+ (core::mem::size_of::<$word>() as $addr_size)
}
}
diff --git a/tests/lib.rs b/tests/lib.rs
index 9add6e3..1d5e964 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -1,5 +1,7 @@
use yaxpeax_arch::AddressBase;
+mod reader;
+
#[test]
fn test_u16() {
for l in 0..100 {
diff --git a/tests/reader.rs b/tests/reader.rs
new file mode 100644
index 0000000..d5d5440
--- /dev/null
+++ b/tests/reader.rs
@@ -0,0 +1,24 @@
+use yaxpeax_arch::{Reader, U8Reader, U16le, U32le};
+
+#[test]
+fn reader_offset_is_words_not_bytes() {
+ fn test_u16<T: Reader<u64, U16le>>(reader: &mut T) {
+ reader.mark();
+ assert_eq!(reader.offset(), 0);
+ reader.next().unwrap();
+ assert_eq!(reader.offset(), 1);
+ reader.mark();
+ reader.next().unwrap();
+ assert_eq!(reader.offset(), 1);
+ assert_eq!(reader.total_offset(), 2);
+ }
+ fn test_u32<T: Reader<u64, U32le>>(reader: &mut T) {
+ reader.mark();
+ assert_eq!(reader.offset(), 0);
+ reader.next().unwrap();
+ assert_eq!(reader.offset(), 1);
+ }
+
+ test_u16(&mut U8Reader::new(&[0x01, 0x02, 0x03, 0x04]));
+ test_u32(&mut U8Reader::new(&[0x01, 0x02, 0x03, 0x04]));
+}