Skip to content

Commit 6da5c70

Browse files
Merge pull request #5200 from MicrosoftDocs/main638757595060880020sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents aa7d7bf + 8099965 commit 6da5c70

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

docs/code-quality/c26132.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Warning C26132
3+
description: Documentation on static analysis warning C26132
4+
author: Rastaban
5+
ms.author: philc
6+
ms.service: visual-cpp
7+
ms.topic: article
8+
ms.date: 02/11/2025
9+
---
10+
# Warning C26132
11+
12+
> Variable '*variable name*' should be protected by '*lock 1*', but '*lock 2*' is held instead. Possible annotation mismatch.
13+
14+
The analyzer issues Warning C26132 when it detects that a lock, which is annotated to protect a value, isn't held while accessing the value. However, a related lock is held. The code may be thread-safe, so you might need to update the annotations.
15+
16+
This diagnostic usually doesn't indicate a bug in the code, but rather a mismatch between the annotations and the actual locking behavior. If so, the diagnostic should be resolved as there may be other static analysis issues that aren't being reported due to the inconsistent annotations.
17+
18+
## Examples
19+
20+
In the following example, C26132 is emitted when `data` is used.
21+
22+
The annotation specifies that `customLock01` should protect the variable `data`, but `CustomLockAcquire` is responsible for acquiring the related lock `customLock01->cs`.
23+
24+
```cpp
25+
#include <sal.h>
26+
struct CustomLock
27+
{
28+
int cs; // "Critical Section" lock
29+
};
30+
31+
_Acquires_exclusive_lock_(criticalSection->cs) // notice the `->` indirection
32+
void CustomLockAcquire(CustomLock* criticalSection);
33+
34+
_Releases_lock_(criticalSection->cs) // notice the `->` indirection
35+
void CustomLockRelease(CustomLock* criticalSection);
36+
37+
// global lock
38+
CustomLock customLock01;
39+
40+
void Initialize(_Guarded_by_(customLock01) int* data)
41+
{
42+
CustomLockAcquire(&customLock01);
43+
*data = 1; // C26132
44+
CustomLockRelease(&customLock01);
45+
}
46+
```
47+
48+
In this example, the `Initialize` function is thread-safe and behaves as designed, but that design isn't correctly reflected in the concurrency SAL annotations. Fix by adjusting the annotations on the custom locking functions to use `criticalSection` rather than `criticalSection->cs`. The warning could also be fixed by changing the `_Guarded_by_` annotation from `customLock01` to `customLock01.cs`.
49+
50+
```cpp
51+
#include <sal.h>
52+
struct CustomLock
53+
{
54+
int cs; // "Critical Section" lock
55+
};
56+
57+
_Acquires_exclusive_lock_(criticalSection)
58+
void CustomLockAcquire(CustomLock* criticalSection);
59+
60+
_Releases_lock_(criticalSection)
61+
void CustomLockRelease(CustomLock* criticalSection);
62+
63+
// global lock
64+
CustomLock customLock01;
65+
66+
void Initialize(_Guarded_by_(customLock01) int* data)
67+
{
68+
CustomLockAcquire(&customLock01);
69+
*data = 1;
70+
CustomLockRelease(&customLock01);
71+
}
72+
```
73+

docs/code-quality/c26133.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: Warning C26133
3+
description: Documentation on static analysis warning C26133
4+
author: Rastaban
5+
ms.author: philc
6+
ms.service: visual-cpp
7+
ms.topic: article
8+
ms.date: 02/19/2025
9+
---
10+
# Warning C26133
11+
12+
> Caller failing to hold lock '*lock 1*' before calling function '*function name*', but '*lock 2*' is held instead. Possible annotation mismatch.
13+
14+
Warning C26133 is issued when the analyzer detects that the lock required to call a function isn't held when the function is called. However, another lock that appears to be related is held. It's possible the code is thread-safe, and the annotations need to be updated.
15+
16+
This diagnostic usually doesn't indicate a bug in the code, but rather a mismatch between the annotations and the actual locking behavior. If so, the diagnostic should be resolved as there may be other static analysis issues that aren't being reported due to the inconsistent annotations.
17+
18+
## Examples
19+
20+
In the following example, C26133 is emitted when `DoTaskWithCustomLock` is called.
21+
22+
> warning C26133: Caller failing to hold lock 'customLock01' before calling function 'DoTaskWithCustomLock', but '(&customLock01)->cs' is held instead. Possible annotation mismatch.
23+
24+
```cpp
25+
#include <sal.h>
26+
27+
struct CustomLock
28+
{
29+
int cs; // "Critical Section"
30+
};
31+
32+
_Acquires_exclusive_lock_(criticalSection->cs) // notice the `->` indirection
33+
void CustomLockAcquire(CustomLock* criticalSection);
34+
35+
_Releases_lock_(criticalSection->cs) // notice the `->` indirection
36+
void CustomLockRelease(CustomLock* criticalSection);
37+
38+
CustomLock customLock01;
39+
40+
_Requires_lock_held_(customLock01) void DoTaskWithCustomLock();
41+
42+
void DoTask()
43+
{
44+
CustomLockAcquire(&customLock01);
45+
DoTaskWithCustomLock(); // C26133
46+
CustomLockRelease(&customLock01);
47+
}
48+
```
49+
50+
In this example, the `DoTask` function is thread-safe and behaves as designed, but that design isn't correctly reflected in the concurrency SAL annotations. Fix by adjusting the annotations on the custom locking functions to use `criticalSection` rather than `criticalSection->cs`. The warning could also be fixed by changing the `_Requires_lock_held_` annotation from `customLock01` to `customLock01.cs`.
51+
52+
```cpp
53+
#include <sal.h>
54+
55+
struct CustomLock
56+
{
57+
int cs; // "Critical Section"
58+
};
59+
60+
_Acquires_exclusive_lock_(criticalSection)
61+
void CustomLockAcquire(CustomLock* criticalSection);
62+
63+
_Releases_lock_(criticalSection)
64+
void CustomLockRelease(CustomLock* criticalSection);
65+
66+
CustomLock customLock01;
67+
68+
_Requires_lock_held_(customLock01) void DoTaskWithCustomLock();
69+
70+
void DoTask()
71+
{
72+
CustomLockAcquire(&customLock01);
73+
DoTaskWithCustomLock();
74+
CustomLockRelease(&customLock01);
75+
}
76+
```

docs/code-quality/toc.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ items:
591591
href: ../code-quality/c26117.md
592592
- name: Warning C26130
593593
href: ../code-quality/c26130.md
594+
- name: Warning C26132
595+
href: c26132.md
596+
- name: Warning C26133
597+
href: c26133.md
594598
- name: Warning C26135
595599
href: ../code-quality/c26135.md
596600
- name: Warning C26138
@@ -634,9 +638,9 @@ items:
634638
- name: Warning C26831
635639
href: ../code-quality/c26831.md
636640
- name: Warning C26838
637-
href: c26838.md
641+
href: ../code-quality/c26838.md
638642
- name: Warning C26839
639-
href: c26839.md
643+
href: ../code-quality/c26839.md
640644
- name: Warning C26832
641645
href: ../code-quality/c26832.md
642646
- name: Warning C26833

0 commit comments

Comments
 (0)