Vulnerability in enum-map package: enum_map macro can cause UB when `Enum` trait is incorrectly implemented

enum-map Updated 02 Oct 2025

Summary

16/06/2022
HIGH
Yes i
High
Not published

GHSA-rxhx-9fj6-6h2m: enum_map macro can cause UB when Enum trait is incorrectly implemented. Affects enum-map versions >= 2.0.0-2, < 2.0.2. Fixed in 2.0.2. How to fix ↓

Free scan

Check if this vulnerability affects your application

Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.

Check my app free

No signup required · Results in about 60 seconds

Upload your dependency manifest or SBOM

Drag & drop your file here or

Java Javascript PHP Python Go Rust Ruby Dart C/C++ Elixir CycloneDX SPDX

Upload a supported dependency manifest or SBOM. No installation or source-code access required.

See supported files and SBOM formats
SBOM sbom.json · bom.json · bom.xml · *.spdx.json · *.cdx.json · *.cdx.xml
Java pom.xml · gradle.lockfile · buildscript-gradle.lockfile
Javascript package-lock.json · yarn.lock · pnpm-lock.yaml
PHP composer.lock
Python poetry.lock · requirements.txt · Pipfile.lock
Go go.mod
Rust Cargo.lock
Ruby Gemfile.lock
Dart pubspec.lock
C/C++ conan.lock
Elixir mix.lock
Enter your work email to receive your report link.

Verify your email to view the full report and start 30 days of continuous monitoring. No credit card required.

Sponsored Devnack Mobile apps built by Devnack Design, build and launch iOS and Android apps with a senior product team. From idea to store release Start a project

Background

The enum-map package is a crate available on crates.io that provides a data structure called EnumMap. EnumMap is a map-like data structure that is optimized for enums as keys. It allows efficient storage and retrieval of values associated with enum variants. The package is commonly used in Rust projects that make use of enums and require a mapping between enum variants and values.

The vulnerability in the enum-map package arises from an incorrect implementation of the Enum trait, which is used by the enum_map! macro. This macro is responsible for creating an EnumMap instance based on the provided enum and associated values. Affected versions of the crate did not properly validate the length of the enum, leading to potential uninitialized types and the possibility of arbitrary code execution.

Vulnerability Detail

The vulnerability in the enum-map package allows an attacker to execute arbitrary code by exploiting an incorrect implementation of the Enum trait. When the LENGTH property in the Enum trait does not match the array length in the EnumArray trait, the enum map can be initialized with uninitialized types. This can be triggered when a manual implementation of the Enum trait is used, rather than relying on the #[derive(Enum)] macro. The uninitialized types can then be manipulated by an attacker to execute arbitrary code, potentially leading to a compromise of the system.

To illustrate this vulnerability, consider the following example code:

enum E {
    A,
    B,
    C,
}

impl Enum for E {
    const LENGTH: usize = 2;

    fn from_usize(value: usize) -> E {
        match value {
            0 => E::A,
            1 => E::B,
            2 => E::C,
            _ => unimplemented!(),
        }
    }

    fn into_usize(self) -> usize {
        self as usize
    }
}

impl<V> EnumArray<V> for E {
    type Array = [V; 3];
}

let _map: EnumMap<E, String> = enum_map! { _ => "Hello, world!".into() };

In this code, the Enum trait is manually implemented for the E enum, but the LENGTH property is set to 2, while the associated array length in the EnumArray trait is 3. This mismatch can result in uninitialized types and potential code execution.

How To Fix

Latest published version: 3.1.0 as of 23 Oct 2026

Upgrading to it may include breaking changes; the version below is the minimum fixed release for this vulnerability.

If you need the minimum change

2.0.2

This is the first release that contains the fix.

Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →

Workaround

If updating the enum-map package is not immediately feasible, a possible workaround is to avoid using manual implementations of the Enum trait and instead rely on the #[derive(Enum)] macro. This macro ensures that the LENGTH property is correctly set based on the number of enum variants. By using the macro, the vulnerability can be mitigated. Here is an example of using the #[derive(Enum)] macro:

#[derive(Enum)]
enum E {
    A,
    B,
    C,
}

impl<V> EnumArray<V> for E {
    type Array = [V; 3];
}

let _map: EnumMap<E, String> = enum_map! { _ => "Hello, world!".into() };

Conclusion

The enum-map package is vulnerable to a code execution vulnerability due to an incorrect implementation of the Enum trait. This vulnerability can be mitigated by updating to version 2.0.2 or later of the package. Alternatively, using the #[derive(Enum)] macro instead of manual implementations of the Enum trait can also mitigate the vulnerability. It is recommended to update the package or apply the workaround to ensure the security of the system.

For further support on vulnerability remediation, please contact DevNack.com.

See every vulnerability recorded for enum-map →

Share this vulnerability

FAQ

The enum-map package is a crate available on crates.io that provides a data structure called EnumMap. EnumMap is a map-like data structure optimized for enums as keys.

The vulnerability in the enum-map package arises from an incorrect implementation of the Enum trait, which can lead to uninitialized types and potential arbitrary code execution.

The vulnerability can be exploited by providing a manual implementation of the Enum trait with a mismatched LENGTH property, allowing the initialization of the enum map with uninitialized types.

Yes, the vulnerability has been fixed in version 2.0.2 of the enum-map package. It is recommended to update to this version or later to mitigate the vulnerability.

A possible workaround is to avoid using manual implementations of the Enum trait and instead rely on the #[derive(Enum)] macro. This ensures that the LENGTH property is correctly set based on the number of enum variants.

Related vulnerabilities

Recent vulnerabilities recorded for enum-map.

How To Fix

Latest published version: 3.1.0 as of 23 Oct 2026

Upgrading to it may include breaking changes; the version below is the minimum fixed release for this vulnerability.

If you need the minimum change

2.0.2

This is the first release that contains the fix.

Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback