Skip to content

Commit 10e9567

Browse files
committed
Learn Editor: Update c26132.md
1 parent 8854eee commit 10e9567

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

docs/code-quality/c26132.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,65 @@ 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 C26132
15+
# Warning C26132
16+
17+
> Variable '*variable name*' should be protected by '*lock 1*', but '*lock 2*' is held instead. Possible annotation mismatch.
18+
19+
Warning C26132 is issued when the analyzer detects that the lock that is annotated to protect a value is not held when the value is accessed. 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, C26132 is emitted when `data` is used.
24+
25+
> warning C26132: Variable 'data' should be protected by 'customLock01', but '(&customLock01)->cs' is held instead. Possible annotation mismatch.
26+
27+
The variable `data` is annotated to be protected by `customLock01`, but the locking function `CustomLockAcquire` is annotated to acquire the related lock `customLock01->cs`.
28+
29+
```cpp
30+
#include <sal.h>
31+
struct CustomLock {
32+
int cs; // "Critical Section" lock
33+
};
34+
35+
_Acquires_exclusive_lock_(criticalSection->cs) // notice the `->` indirection
36+
void CustomLockAcquire(CustomLock* criticalSection);
37+
38+
_Releases_lock_(criticalSection->cs) // notice the `->` indirection
39+
void CustomLockRelease(CustomLock* criticalSection);
40+
41+
// global lock
42+
CustomLock customLock01;
43+
44+
void Initialize(_Guarded_by_(customLock01) int* data)
45+
{
46+
CustomLockAcquire(&customLock01);
47+
*data = 1; // C26132
48+
CustomLockRelease(&customLock01);
49+
}
50+
```
51+
52+
In this example the `Initialize` 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 `_Guarded_by_` annotation from `customLock01` to `customLock01.cs`.
53+
54+
```cpp
55+
#include <sal.h>
56+
struct CustomLock {
57+
int cs; // "Critical Section" lock
58+
};
59+
60+
_Acquires_exclusive_lock_(criticalSection)
61+
void CustomLockAcquire(CustomLock* criticalSection);
62+
63+
_Releases_lock_(criticalSection)
64+
void CustomLockRelease(CustomLock* criticalSection);
65+
66+
// global lock
67+
CustomLock customLock01;
68+
69+
void Initialize(_Guarded_by_(customLock01) int* data)
70+
{
71+
CustomLockAcquire(&customLock01);
72+
*data = 1;
73+
CustomLockRelease(&customLock01);
74+
}
75+
```
76+

0 commit comments

Comments
 (0)