{"id":"GHSA-rxhx-9fj6-6h2m","aliases":["GHSA-rxhx-9fj6-6h2m","RUSTSEC-2022-0010"],"related":[],"kind":"vulnerability","state":"published","title":"enum_map macro can cause UB when `Enum` trait is incorrectly implemented","summary":"Affected versions of this crate did not properly check the length of an enum when using `enum_map!` macro, trusting user-provided length.\n\nWhen the `LENGTH` in the `Enum` trait does not match the array length in the `EnumArray` trait, this can result in the initialization of the enum map with uninitialized types, which in turn can allow an attacker to execute arbitrary code.\n\nThis problem can only occur with a manual implementation of the Enum trait, it will never occur for enums that use `#[derive(Enum)]`.\n\nExample code that triggers this vulnerability looks like this:\n\n```rust\nenum E {\n    A,\n    B,\n    C,\n}\n\nimpl Enum for E {\n    const LENGTH: usize = 2;\n\n    fn from_usize(value: usize) -> E {\n        match value {\n            0 => E::A,\n            1 => E::B,\n            2 => E::C,\n            _ => unimplemented!(),\n        }\n    }\n\n    fn into_usize(self) -> usize {\n        self as usize\n    }\n}\n\nimpl<V> EnumArray<V> for E {\n    type Array = [V; 3];\n}\n\nlet _map: EnumMap<E, String> = enum_map! { _ => \"Hello, world!\".into() };\n```\n\nThe flaw was corrected in commit [b824e23](https://github.com/xfix/enum-map/commit/b824e232f2fb47837740070096ac253df8e80dfc) by putting `LENGTH` property on sealed trait for macro to read.","summaryFormat":"markdown","summarySource":"ghsa","descriptions":[{"lang":"en","text":"Affected versions of this crate did not properly check the length of an enum when using `enum_map!` macro, trusting user-provided length.\n\nWhen the `LENGTH` in the `Enum` trait does not match the array length in the `EnumArray` trait, this can result in the initialization of the enum map with uninitialized types, which in turn can allow an attacker to execute arbitrary code.\n\nThis problem can only occur with a manual implementation of the Enum trait, it will never occur for enums that use `#[derive(Enum)]`.\n\nExample code that triggers this vulnerability looks like this:\n\n```rust\nenum E {\n    A,\n    B,\n    C,\n}\n\nimpl Enum for E {\n    const LENGTH: usize = 2;\n\n    fn from_usize(value: usize) -> E {\n        match value {\n            0 => E::A,\n            1 => E::B,\n            2 => E::C,\n            _ => unimplemented!(),\n        }\n    }\n\n    fn into_usize(self) -> usize {\n        self as usize\n    }\n}\n\nimpl<V> EnumArray<V> for E {\n    type Array = [V; 3];\n}\n\nlet _map: EnumMap<E, String> = enum_map! { _ => \"Hello, world!\".into() };\n```\n\nThe flaw was corrected in commit [b824e23](https://github.com/xfix/enum-map/commit/b824e232f2fb47837740070096ac253df8e80dfc) by putting `LENGTH` property on sealed trait for macro to read.","format":"markdown","source":"ghsa"},{"lang":"en","text":"Affected versions of this crate did not properly check the length of an enum when using `enum_map!` macro, trusting user-provided length.\n\nWhen the `LENGTH` in the `Enum` trait does not match the array length in the `EnumArray` trait, this can result in the initialization of the enum map with uninitialized types, which in turn can allow an attacker to execute arbitrary code.\n\nThis problem can only occur with a manual implementation of the Enum trait, it will never occur for enums that use `#[derive(Enum)]`.\n\nExample code that triggers this vulnerability looks like this:\n\n```rust\nenum E {\n    A,\n    B,\n    C,\n}\n\nimpl Enum for E {\n    const LENGTH: usize = 2;\n\n    fn from_usize(value: usize) -> E {\n        match value {\n            0 => E::A,\n            1 => E::B,\n            2 => E::C,\n            _ => unimplemented!(),\n        }\n    }\n\n    fn into_usize(self) -> usize {\n        self as usize\n    }\n}\n\nimpl<V> EnumArray<V> for E {\n    type Array = [V; 3];\n}\n\nlet _map: EnumMap<E, String> = enum_map! { _ => \"Hello, world!\".into() };\n```\n\nThe flaw was corrected in commit [b824e23](https://gitlab.com/KonradBorowski/enum-map/-/commit/b824e232f2fb47837740070096ac253df8e80dfc) by putting `LENGTH` property on sealed trait for macro to read.","format":"markdown","source":"rustsec"}],"published":"2022-02-17T12:00:00.000Z","modified":"2023-11-08T04:22:58.719Z","severity":{"scorer":"GitHub","scheme":"rating","rating":"HIGH"},"severities":[{"scorer":"GitHub","scheme":"rating","rating":"HIGH"}],"cwes":[],"affected":[{"ranges":[{"type":"semver","introduced":"2.0.0-2","status":"affected","fixed":"2.0.2"}],"ecosystem":"crates.io","package":"enum-map","purl":"pkg:cargo/enum-map","source":"ghsa"},{"ranges":[{"type":"semver","introduced":"2.0.0-2","status":"affected","fixed":"2.0.2"}],"ecosystem":"crates.io","package":"enum-map","purl":"pkg:cargo/enum-map","source":"rustsec"}],"references":[{"url":"https://github.com/xfix/enum-map/commit/b824e232f2fb47837740070096ac253df8e80dfc","tags":["web"],"sources":["ghsa"]},{"url":"https://github.com/rustsec/advisory-db/blob/main/crates/enum-map/RUSTSEC-2022-0010.md","tags":["web"],"sources":["ghsa"]},{"url":"https://github.com/xfix/enum-map","tags":["package"],"sources":["ghsa"]},{"url":"https://github.com/xfix/enum-map/blob/master/CHANGELOG.md#version-202","tags":["web"],"sources":["ghsa"]},{"url":"https://gitlab.com/KonradBorowski/enum-map/-/blob/master/CHANGELOG.md#version-202","tags":["web"],"sources":["ghsa","rustsec"]},{"url":"https://rustsec.org/advisories/RUSTSEC-2022-0010.html","tags":["advisory","web"],"sources":["ghsa","rustsec"]},{"url":"https://crates.io/crates/enum-map","tags":["package"],"sources":["rustsec"]}],"credits":[],"sources":[{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-rxhx-9fj6-6h2m","url":"https://github.com/advisories/GHSA-rxhx-9fj6-6h2m","licence":"CC-BY-4.0","modified":"2023-11-08T04:22:58.719Z"},{"source":"rustsec","name":"RustSec Advisory Database","id":"RUSTSEC-2022-0010","url":"https://rustsec.org/advisories/RUSTSEC-2022-0010.html","licence":"CC0-1.0","modified":"2023-11-08T04:22:58.719Z"}],"updatedAt":"2026-11-25T22:56:43.091Z"}