Skip to content

Incorrect thread safety analysis warning on accessing union member. #78131

Open
@hokein

Description

@hokein

See godbolt

#define LOCKABLE                        __attribute__((lockable))
#define EXCLUSIVE_LOCK_FUNCTION(...)    __attribute__((exclusive_lock_function(__VA_ARGS__)))
#define UNLOCK_FUNCTION(...)            __attribute__((unlock_function(__VA_ARGS__)))
#define GUARDED_BY(x)                   __attribute__((guarded_by(x)))

class LOCKABLE Mutex {
 public:
  void Lock() EXCLUSIVE_LOCK_FUNCTION();
  void Unlock() UNLOCK_FUNCTION();
};

class Test {
  Mutex mu;

  union {
    int a GUARDED_BY(mu);
    int b GUARDED_BY(mu);
  } ;
  void test1() {  
    mu.Lock();
    a = 0;   // unexpected warning: writing variable 'a' requires holding mutex 'mu' exclusively
    mu.Unlock();
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions