Skip to content

Commit 24b33d5

Browse files
committed
document one more stray unsafe
1 parent 6a5ea10 commit 24b33d5

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/long_mode/display.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::long_mode::{RegSpec, Opcode, Operand, MergeMode, InstDecoder, Instruc
99

1010
use yaxpeax_arch::display::DisplaySink;
1111
use yaxpeax_arch::safer_unchecked::GetSaferUnchecked as _;
12+
use yaxpeax_arch::safer_unchecked::unreachable_kinda_unchecked as unreachable_unchecked;
1213

1314
trait DisplaySinkExt {
1415
// `write_opcode` depends on all mnemonics being less than 32 bytes long. check that here, at
@@ -3693,15 +3694,16 @@ pub(crate) fn contextualize_intel<T: DisplaySink>(instr: &Instruction, out: &mut
36933694
// don't worry about checking for `instr.operands[i] != Nothing`, it would be a bug to
36943695
// reach that while iterating only to `operand_count`..
36953696
out.write_fixed_size(", ")?;
3697+
// hint that accessing `inster.operands[i]` can't panic: this is useful for
3698+
// `instr.operands` and the segment selector check after.
36963699
if i >= 4 {
3697-
unsafe { core::hint::unreachable_unchecked(); }
3700+
// Safety: Instruction::operands is a four-element array; operand_count is always
3701+
// low enough that 0..operand_count is a valid index.
3702+
unsafe { unreachable_unchecked(); }
36983703
}
36993704

37003705
if instr.operands[i as usize].is_memory() {
37013706
out.write_mem_size_label(instr.mem_size)?;
3702-
if i >= 4 {
3703-
unsafe { core::hint::unreachable_unchecked(); }
3704-
}
37053707
if let Some(prefix) = instr.segment_override_for_op(i) {
37063708
let name = prefix.name();
37073709
out.write_char(' ')?;

src/protected_mode/display.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::protected_mode::{RegSpec, Opcode, Operand, MergeMode, InstDecoder, In
99

1010
use yaxpeax_arch::display::DisplaySink;
1111
use yaxpeax_arch::safer_unchecked::GetSaferUnchecked as _;
12+
use yaxpeax_arch::safer_unchecked::unreachable_kinda_unchecked as unreachable_unchecked;
1213

1314
trait DisplaySinkExt {
1415
// `write_opcode` depends on all mnemonics being less than 32 bytes long. check that here, at
@@ -2249,15 +2250,16 @@ pub(crate) fn contextualize_intel<T: DisplaySink>(instr: &Instruction, out: &mut
22492250
// don't worry about checking for `instr.operands[i] != Nothing`, it would be a bug to
22502251
// reach that while iterating only to `operand_count`..
22512252
out.write_fixed_size(", ")?;
2253+
// hint that accessing `inster.operands[i]` can't panic: this is useful for
2254+
// `instr.operands` and the segment selector check after.
22522255
if i >= 4 {
2253-
unsafe { core::hint::unreachable_unchecked(); }
2256+
// Safety: Instruction::operands is a four-element array; operand_count is always
2257+
// low enough that 0..operand_count is a valid index.
2258+
unsafe { unreachable_unchecked(); }
22542259
}
22552260

22562261
if instr.operands[i as usize].is_memory() {
22572262
out.write_mem_size_label(instr.mem_size)?;
2258-
if i >= 4 {
2259-
unsafe { core::hint::unreachable_unchecked(); }
2260-
}
22612263
if let Some(prefix) = instr.segment_override_for_op(i) {
22622264
let name = prefix.name();
22632265
out.write_char(' ')?;

src/real_mode/display.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::real_mode::{RegSpec, Opcode, Operand, MergeMode, InstDecoder, Instruc
99

1010
use yaxpeax_arch::display::DisplaySink;
1111
use yaxpeax_arch::safer_unchecked::GetSaferUnchecked as _;
12+
use yaxpeax_arch::safer_unchecked::unreachable_kinda_unchecked as unreachable_unchecked;
1213

1314
trait DisplaySinkExt {
1415
// `write_opcode` depends on all mnemonics being less than 32 bytes long. check that here, at
@@ -2251,15 +2252,16 @@ pub(crate) fn contextualize_intel<T: DisplaySink>(instr: &Instruction, out: &mut
22512252
// don't worry about checking for `instr.operands[i] != Nothing`, it would be a bug to
22522253
// reach that while iterating only to `operand_count`..
22532254
out.write_fixed_size(", ")?;
2255+
// hint that accessing `inster.operands[i]` can't panic: this is useful for
2256+
// `instr.operands` and the segment selector check after.
22542257
if i >= 4 {
2255-
unsafe { core::hint::unreachable_unchecked(); }
2258+
// Safety: Instruction::operands is a four-element array; operand_count is always
2259+
// low enough that 0..operand_count is a valid index.
2260+
unsafe { unreachable_unchecked(); }
22562261
}
22572262

22582263
if instr.operands[i as usize].is_memory() {
22592264
out.write_mem_size_label(instr.mem_size)?;
2260-
if i >= 4 {
2261-
unsafe { core::hint::unreachable_unchecked(); }
2262-
}
22632265
if let Some(prefix) = instr.segment_override_for_op(i) {
22642266
let name = prefix.name();
22652267
out.write_char(' ')?;

0 commit comments

Comments
 (0)