GHSA-gfgm-chr3-x6px
JSON

medium

prettytable-rs: Force cast a &Vec<T> to &[T] may lead to undefined behavior

Severity
medium rating by GitHub
Published
Last changed
Also known as
RUSTSEC-2022-0074

Description

In function Table::as_ref, a reference of vector is force cast to slice. There are multiple problems here:

  1. To guarantee the size is correct, we have to first do Vec::shrink_to_fit. The function requires a mutable reference, so we have to force cast from immutable to mutable, which is undefined behavior (UB).
  2. Even if (1) is sound, &Vec<T> and &[T] still might not have the same layout. Treating them equally may lead to undefinted behavior (UB).

Affected

Product or packageVersionsReported by
prettytable-rs (crates.io)before 0.10.0, from 0.0.0-0 before 0.10.0GitHub Advisory Database, RustSec Advisory Database

What each source says

GitHub Advisory Database

In function Table::as_ref, a reference of vector is force cast to slice. There are multiple problems here:

  1. To guarantee the size is correct, we have to first do Vec::shrink_to_fit. The function requires a mutable reference, so we have to force cast from immutable to mutable, which is undefined behavior (UB).
  2. Even if (1) is sound, &Vec<T> and &[T] still might not have the same layout. Treating them equally may lead to undefinted behavior (UB).
RustSec Advisory Database

In function Table::as_ref, a reference of vector is force cast to slice. There are multiple problems here:

  1. To guarantee the size is correct, we have to first do Vec::shrink_to_fit. The function requires a mutable reference, so we have to force cast from immutable to mutable, which is UB.
  2. Even if (1) is sound, &Vec<T> and &[T] still might not have the same layout. Treating them equally may lead to UB.

References

Sources

SourceRecordLicence
GitHub Advisory DatabaseGHSA-gfgm-chr3-x6px changed 2023-11-08CC-BY-4.0
RustSec Advisory DatabaseRUSTSEC-2022-0074 changed 2023-11-08CC0-1.0