Skip to content

[libc++] False positive mutex '...' is still held at the end of function #156760

@PiJoules

Description

@PiJoules
#include <mutex>

std::mutex m0, m1;

void func() {
  std::unique_lock ul1(m0, std::defer_lock);
  std::unique_lock ul2(m1, std::defer_lock);
  std::lock(ul1, ul2);
}
$ ~/misc/clang-cipd-latest/bin/clang++ -c -Wall -Wthread-safety /tmp/test.cc
/tmp/test.cc:9:1: warning: mutex 'ul1' is still held at the end of function [-Wthread-safety-analysis]
    9 | }
      | ^
/tmp/test.cc:8:3: note: mutex acquired here
    8 |   std::lock(ul1, ul2);
      |   ^
/tmp/test.cc:9:1: warning: mutex 'ul2' is still held at the end of function [-Wthread-safety-analysis]
    9 | }
      | ^
/tmp/test.cc:8:3: note: mutex acquired here
    8 |   std::lock(ul1, ul2);
      |   ^
2 warnings generated.

This comes up with #154078. Both mutexes should be unlocked by the unique_lock dtors at the end of the function. Also -Wthread-safety-analysis is incorrectly saying ul1 and ul2 are mutexes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions