Skip to content

force casting Vec<T> to &[T] in Table::AsRef is causing trouble #145

Closed
@david0u0

Description

@david0u0

The compiler version: rustc 1.67.0-nightly (c090c6880 2022-12-01)

image

As shown in the image in gdb:

  • $8 is a ref to Table
  • $7 is a ref to TableSlice, returned by Table::AsRef

They have identical pointer value, 0x7ffffff782e0, but the rows field in them are in different address. rows of Table is the correct one, while rows of TableSlice contains random data.

Further more, the rows of TableSlice has length = 93825009397280 (!!!).

The cause is in the implementation of Table::AsRef. It force casts a &Table into &TableSlice, and when the layout of these two types are not the same, it'll cause trouble.

impl<'a> AsRef<TableSlice<'a>> for Table {
    fn as_ref(&self) -> &TableSlice<'a> {
        unsafe {
            // All this is a bit hacky. Let's try to find something else
            let s = &mut *((self as *const Table) as *mut Table);
            s.rows.shrink_to_fit();
            &*(self as *const Table as *const TableSlice<'a>)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions