Skip to content

Clang thread-safety analysis not producing a warning for accessing a guarded field in different object #127349

Open
@Qwinci

Description

@Qwinci

Given the following code (full code in godbolt link below) Clang doesn't report any warnings for accessing b.some_field even though b.lock is not locked at any point (removing a.lock.acquire() + a.lock.release() calls makes the warning appear but that's obviously not something that you should have to do as they are two distinct objects):

struct CAPABILITY("spinlock") Spinlock {
    void acquire() ACQUIRE();
    void release() RELEASE();
};

struct Foo {
    Spinlock lock;
    bool some_field GUARDED_BY(lock);
};

void foobar() {
    Foo a {};
    Foo b {};
    a.lock.acquire();
    b.some_field = true;
    a.lock.release();
}

godbolt

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