Skip to content

clang-tidy clang-analyzer-unix.BlockInCriticalSection false positive with std::defer_lock #166573

@SRHMorris

Description

@SRHMorris

I've run into this false positive when using std::unique_lock with std::defer_lock. This is using LLVM 21.1.4.

Minimum reproducible example, real code is more complex than this:

#include <mutex>
#include <sys/socket.h> 
int main()
{
    std::mutex m;
    std::unique_lock lock(m, std::defer_lock);
    recv(0, nullptr, 0, 0);
}

Produces the following output:

.../main.cpp:8:3: error: Call to blocking function 'recv' inside of critical section [clang-analyzer-unix.BlockInCriticalSection,-warnings-as-errors]
    8 |   recv(0, nullptr, 0, 0);
      |   ^~~~~~~~~~~~~~~~~~~~~~
.../main.cpp:7:20: note: Entering critical section here
    7 |   std::unique_lock lock(m, std::defer_lock);
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~
.../main.cpp:8:3: note: Call to blocking function 'recv' inside of critical section
    8 |   recv(0, nullptr, 0, 0);
      |   ^~~~~~~~~~~~~~~~~~~~~~

Expected result is that this shouldn't produce a warning, as the mutex is unlocked at the point where recv is called.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions