|
| 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 | +``` |
0 commit comments