rust-lang / rust-lang/rust-clippy

Suggestion: detect calling printf-style C APIs in dodgy ways

Open
#5,465 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Specifically, APIS like:

extern "C" {
    fn foo(some: args, fmt: *const c_char, ...);
}

where they're variadic C functions where the last non-variadic argument is a *const c_char.

If you call it as foo(x, some_cstr.as_ptr()) that's almost always a format string vulnerability. Instead you should use a format arg like b"%s\0".as_ptr().cast() and then pass the string as the first entry in the variadic list.

It's might be enough just to detect the case where there's no variadic args? I don't really know if it's worth trying to do the c-style check that the format args are correct -- these APIs get so little use.

I don't know how doable this is as a lint, but I've seen the mistake a lot.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or existing lint entry points are named. Start by determining how rust-clippy represents variadic extern "C" declarations and calls, then scope detection to a non-variadic *const c_char last parameter and absent variadic arguments. Done means lint behavior and tests covering the proposed call patterns.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
security, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.