Skip to content

Commit 51ebfe1

Browse files
committed
Learn Editor: Update c26133.md
1 parent 304b8cb commit 51ebfe1

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

docs/code-quality/c26133.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,66 @@ ms.service: # Add the ms.service or ms.prod value
1212
ms.topic: # Add the ms.topic value
1313
ms.date: 02/11/2025
1414
---
15-
Warning C26133
15+
# Warning C26133
16+
17+
> Caller failing to hold lock '*lock 1*' before calling function '*function name*', but '*lock 2*' is held instead. Possible annotation mismatch.
18+
19+
Warning C26133is issued when the analyzer detects that the lock that is required to call a function, is not held when the function is called. However, another lock that appears to be related is held. It is possible the code is thread safe, and the annotations need to be updated.
20+
21+
## Examples
22+
23+
In the following example, C26133 is emitted when `DoTaskWithCustomLock` is called.
24+
25+
> warning C26133: Caller failing to hold lock 'customLock01' before calling function 'DoTaskWithCustomLock', but '(&customLock01)->cs' is held instead. Possible annotation mismatch.
26+
27+
```cpp
28+
#include <sal.h>
29+
30+
struct CustomLock {
31+
int cs; // "Critical Section"
32+
};
33+
34+
_Acquires_exclusive_lock_(criticalSection->cs) // notice the `->` indirection
35+
void CustomLockAcquire(CustomLock* criticalSection);
36+
37+
_Releases_lock_(criticalSection->cs) // notice the `->` indirection
38+
void CustomLockRelease(CustomLock* criticalSection);
39+
40+
CustomLock customLock01;
41+
42+
_Requires_lock_held_(customLock01) void DoTaskWithCustomLock();
43+
44+
void DoTask()
45+
{
46+
CustomLockAcquire(&customLock01);
47+
DoTaskWithCustomLock(); // C26133
48+
CustomLockRelease(&customLock01);
49+
}
50+
```
51+
52+
In this example the `DoTask` function is thread safe and behaves as designed, but that design is not correctly reflected in the concurrency SAL annotations. This is fixed by adjusting the annotations on the custom locking functions to use `criticalSection` rather than `criticalSection->cs`. This could also be fixed by changing the `_Requires_lock_held_` annotation from `customLock01` to `customLock01.cs`.
53+
54+
```cpp
55+
#include <sal.h>
56+
57+
struct CustomLock {
58+
int cs; // "Critical Section"
59+
};
60+
61+
_Acquires_exclusive_lock_(criticalSection)
62+
void CustomLockAcquire(CustomLock* criticalSection);
63+
64+
_Releases_lock_(criticalSection)
65+
void CustomLockRelease(CustomLock* criticalSection);
66+
67+
CustomLock customLock01;
68+
69+
_Requires_lock_held_(customLock01) void DoTaskWithCustomLock();
70+
71+
void DoTask()
72+
{
73+
CustomLockAcquire(&customLock01);
74+
DoTaskWithCustomLock();
75+
CustomLockRelease(&customLock01);
76+
}
77+
```

0 commit comments

Comments
 (0)