Explorar el Código

Merge pull request #27 from jackcmay/fix_clippy_errors

fix clippy error: cast to more strict alignment, warning: not consumed copy
Jan-Erik Rediger hace 6 años
padre
commit
afcfff22c9
Se han modificado 4 ficheros con 13 adiciones y 15 borrados
  1. 1 0
      Cargo.toml
  2. 4 6
      src/ebpf.rs
  3. 1 0
      src/jit.rs
  4. 7 9
      src/lib.rs

+ 1 - 0
Cargo.toml

@@ -27,6 +27,7 @@ include = [
 combine = "2.1.1"
 libc = "0.2.0"
 time = "0.1"
+byteorder = "1.2.1"
 
 [dev-dependencies]
 

+ 4 - 6
src/ebpf.rs

@@ -18,6 +18,8 @@
 //! <https://www.kernel.org/doc/Documentation/networking/filter.txt>, or for a shorter version of
 //! the list of the operation codes: <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md>
 
+use byteorder::{ByteOrder, LittleEndian};
+
 /// Maximum number of instructions in an eBPF program.
 pub const PROG_MAX_INSNS: usize = 4096;
 /// Size of an eBPF instructions, in bytes.
@@ -513,12 +515,8 @@ pub fn get_insn(prog: &[u8], idx: usize) -> Insn {
         opc:  prog[INSN_SIZE * idx],
         dst:  prog[INSN_SIZE * idx + 1] & 0x0f,
         src: (prog[INSN_SIZE * idx + 1] & 0xf0) >> 4,
-        off: unsafe {
-            let x = prog.as_ptr().offset((INSN_SIZE * idx + 2) as isize) as *const i16; *x
-        },
-        imm: unsafe {
-            let x = prog.as_ptr().offset((INSN_SIZE * idx + 4) as isize) as *const i32; *x
-        },
+        off: LittleEndian::read_i16(&prog[(INSN_SIZE * idx + 2) .. ]),
+        imm: LittleEndian::read_i32(&prog[(INSN_SIZE * idx + 4) .. ]),
     }
 }
 

+ 1 - 0
src/jit.rs

@@ -26,6 +26,7 @@ const TARGET_OFFSET: isize = ebpf::PROG_MAX_INSNS as isize;
 const TARGET_PC_EXIT:         isize = TARGET_OFFSET + 1;
 const TARGET_PC_DIV_BY_ZERO:  isize = TARGET_OFFSET + 2;
 
+#[derive(Copy, Clone)]
 enum OperandSize {
     S8  = 8,
     S16 = 16,

+ 7 - 9
src/lib.rs

@@ -19,12 +19,14 @@
 
 #![cfg_attr(feature = "cargo-clippy", allow(cast_lossless, doc_markdown, match_same_arms, unreadable_literal))]
 
-use std::u32;
-use std::collections::HashMap;
-
+extern crate byteorder;
 extern crate combine;
 extern crate time;
 
+use std::u32;
+use std::collections::HashMap;
+use byteorder::{ByteOrder, LittleEndian};
+
 pub mod assembler;
 pub mod disassembler;
 pub mod ebpf;
@@ -885,12 +887,8 @@ impl<'a> EbpfVmFixedMbuff<'a> {
             panic!("Error: buffer too small ({:?}), cannot use data_offset {:?} and data_end_offset {:?}",
             l, self.mbuff.data_offset, self.mbuff.data_end_offset);
         }
-        unsafe {
-            let mut data     = self.mbuff.buffer.as_ptr().offset(self.mbuff.data_offset as isize)     as *mut u64;
-            let mut data_end = self.mbuff.buffer.as_ptr().offset(self.mbuff.data_end_offset as isize) as *mut u64;
-            *data     = mem.as_ptr() as u64;
-            *data_end = mem.as_ptr() as u64 + mem.len() as u64;
-        }
+        LittleEndian::write_u64(&mut self.mbuff.buffer[(self.mbuff.data_offset) .. ], mem.as_ptr() as u64);
+        LittleEndian::write_u64(&mut self.mbuff.buffer[(self.mbuff.data_end_offset) .. ], mem.as_ptr() as u64 + mem.len() as u64);
         self.parent.prog_exec(mem, &self.mbuff.buffer)
     }