Skip to content

Commit

Permalink
Replace memmove() emulation with copy_within()
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
jedisct1 committed Jan 19, 2021
1 parent 9dc8b2a commit 4aa5d0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
19 changes: 6 additions & 13 deletions src/parsed_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::rr_iterator::*;
use crate::synth::gen;
use byteorder::{BigEndian, ByteOrder};
use rand::prelude::*;
use std::ptr;

/// A `ParsedPacket` structure contains information about a successfully parsed
/// DNS packet, that allows quick access to (extended) flags and to individual sections.
Expand Down Expand Up @@ -318,22 +317,16 @@ impl ParsedPacket {
bail!(DSError::PacketTooLarge)
}
let insertion_offset = self.insertion_offset(section)?;
let new_len = self.packet().len() + rr_len;
let packet_len = self.packet().len();
let new_len = packet_len + rr_len;
self.packet_mut().reserve(rr_len);
if insertion_offset == new_len {
self.packet_mut().extend_from_slice(&rr.packet);
} else {
unsafe {
let packet_ptr = self.packet_mut().as_mut_ptr();
ptr::copy(
packet_ptr.add(insertion_offset),
packet_ptr.add(insertion_offset + rr_len),
self.packet().len() - insertion_offset,
);
self.packet_mut().set_len(new_len);
}
self.packet_mut()[insertion_offset..insertion_offset + rr_len]
.copy_from_slice(&rr.packet);
let packet = self.packet_mut();
packet.resize(new_len, 0);
packet.copy_within(insertion_offset..packet_len, insertion_offset + rr_len);
packet[insertion_offset..insertion_offset + rr_len].copy_from_slice(&rr.packet);
}
self.rrcount_inc(section)?;
match section {
Expand Down
24 changes: 5 additions & 19 deletions src/rr_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::parsed_packet::*;
use byteorder::{BigEndian, ByteOrder};
use std::marker;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ptr;

/// Accessor to the raw packet data.
/// `offset` is the offset to the current RR.
Expand Down Expand Up @@ -207,25 +206,12 @@ pub trait TypedIterable {
new_packet_len,
(offset as isize + shift) as usize + (packet_len - offset) as usize
);
unsafe {
let packet_ptr = packet.as_mut_ptr();
ptr::copy(
packet_ptr.add(offset),
packet_ptr.offset((offset as isize + shift) as isize),
packet_len - offset,
);
}
packet.copy_within(offset..offset + packet_len, offset + shift as usize);
} else if shift < 0 {
assert!(packet_len >= (-shift) as usize);
unsafe {
let packet_ptr = packet.as_mut_ptr();
ptr::copy(
packet_ptr.offset((offset as isize - shift) as isize),
packet_ptr.add(offset),
packet_len - (offset as isize - shift) as usize,
);
}
packet.truncate((packet_len as isize + shift) as usize);
let shift = (-shift) as usize;
assert!(packet_len >= shift);
packet.copy_within(offset + shift.., offset);
packet.truncate(packet_len - shift);
}
}
let new_offset_next = (self.offset_next() as isize + shift) as usize;
Expand Down

0 comments on commit 4aa5d0f

Please sign in to comment.