Skip to content

Generic framework for detecting mis-use of C-APIs with init functions #4904

Open
@oli-obk

Description

@oli-obk

I developed a proof of concept MIR analysis that detects if an init function is not called before other functions of an API. This is a common pattern in C, you need to call an init function that sets up all kinds of global state, and only then you can call other functions of the API. While the correct solution would be to make the init function return a handle that is then passed to all other functions, this is the status we have.

Right now the analysis is very much hardcoded to the way you can annotate your code with init and a single other API function, but I could definitely generalize this to some arbitrary #[clippy::something] annotations that users could mark their APIs with.

Is there interest in such a lint?

Prototype impl: https://github.com/rust-lang/rust-clippy/blob/init_before_foo/clippy_lints/src/init_before_foo.rs
Example tests:

The analysis also has a bunch of false alarms (false positives) that would need to be eliminated before it becomes really useful, but I believe it detects all mis-uses (so it has no false negatives).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsC-questionCategory: QuestionsL-correctnessLint: Belongs in the correctness lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions