38 / d4-format

The D4 Quantitative Data Format

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

misaligned pointer dereference in `read_next_value`

shinmao opened this issue · comments

commented

Hi, I found some potential unsoundness in safe method read_next_value:

pub fn read_next_value(&mut self) -> Result<(u32, i32)> {
let pos = self.cursor;
self.ensure_primary_table_buffer()?;
let data = if let Some((start_pos, buf)) = self.primary_table_buffer.as_ref() {
let bit_idx = (self.cursor - *start_pos) as usize * self.dictionary.bit_width();
let idx = bit_idx / 8;
let shift = bit_idx % 8;
let data: &u32 = unsafe { std::mem::transmute(&buf[idx]) };
(*data >> shift) & ((1 << self.dictionary.bit_width()) - 1)

In line 184, the code will transmute type of &u8 to &u32 which will create a misaligned pointer, and the pointer is dereferenced in line 185 which will lead to undefined behavior. Is there any test function for read_next_value?