Skip to content

Detect multiple versions of crates with global state, which can result in bugs. #2363

Closed
@eddyb

Description

@eddyb

Generally, in the absence of global state, via static or linking to C libraries, multiple instances of the same crate should be harmless, and the same-version case is already getting deduplicated.
As thread-local support and lazy_static use static internally, they should get caught as well.

I'm not sure what the best way for detecting this is, short of using --pretty=expanded and searching for static and link attributes.

As far as I'm aware, this has caused issues in the past with the log crate, where env_logger would have a visible effect only for some crates and not others.

It could have worse consequences, if a crate is using some static flag to guard initialization/uses of a C library, and multiple versions of that crate use the same C library (e.g. system-wide OpenGL), potentially allowing data races and other memory unsafe behavior.

In those instances, it might be even better to have an option in the Cargo.toml of such crates to deny having multiple versions downstream, with the static & linkage detection as a lint suggesting that the option be enabled.

cc @whitequark @edef1c

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions