Skip to content

Commit 4cd3fc5

Browse files
Updated C6101
Mirror of my public PR. Added an example. Added proper spacing between headers and code blocks, matched formatting to my other PRs
1 parent 56c97d5 commit 4cd3fc5

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

docs/code-quality/c6101.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@ ms.assetid: 8546367c-5de5-479a-a231-c15c0aa89ef1
99
---
1010
# C6101
1111

12-
> warning C6101: Returning uninitialized memory
12+
**Warning C6101: Returning uninitialized memory**\
13+
Example output:
14+
> Returning uninitialized memory '\**parameter-name*'. A successful path through the function does not set the named \_Out\_ parameter.
1315
14-
A successful path through the function does not set the named `_Out_` parameter. This message is generated based on SAL annotations that indicate that the function in question always succeeds. A function that doesn't return a success/failure indication should set all of its `_Out_` parameters because the analyzer assumes that the `_Out_` parameter is uninitialized data before the function is called, and that the function will set the parameter so that it's no longer uninitialized. If the function does indicate success/failure, then the `_Out_` parameter doesn't have to be set in the case of failure, and you can detect and avoid the uninitialized location. In either case, the objective is to avoid the reading of an uninitialized location. If the function sometimes doesn't touch an `_Out_` parameter that's subsequently used, then the parameter should be initialized before the function call and be marked with the `_Inout_` annotation, or the more explicit `_Pre_null_` or `_Pre_satisfies_()` when appropriate. "Partial success" can be handled with the `_When_` annotation. For more information, see [Using SAL Annotations to Reduce C/C++ Code Defects](../code-quality/using-sal-annotations-to-reduce-c-cpp-code-defects.md).
16+
## Description
17+
18+
This message is generated based on SAL annotations that indicate that the function in question always succeeds. A function that doesn't return a success/failure indication should set all of its `_Out_` parameters because the analyzer assumes that the `_Out_` parameter is uninitialized data before the function is called, and that the function will set the parameter so that it's no longer uninitialized. If the function does indicate success/failure, then the `_Out_` parameter doesn't have to be set in the case of failure, and you can detect and avoid the uninitialized location. In either case, the objective is to avoid the reading of an uninitialized location. If the function sometimes doesn't touch an `_Out_` parameter that's subsequently used, then the parameter should be initialized before the function call and be marked with the `_Inout_` annotation, or the more explicit `_Pre_null_` or `_Pre_satisfies_()` when appropriate. "Partial success" can be handled with the `_When_` annotation. For more information, see [Using SAL Annotations to Reduce C/C++ Code Defects](../code-quality/using-sal-annotations-to-reduce-c-cpp-code-defects.md).
19+
20+
## Example
21+
22+
The following code generates this warning. This is due to the pointer p1 not being set despite being annotated with ```_Out_```.
23+
24+
```cpp
25+
void example_func(_Out_ int *p1)
26+
{
27+
return;
28+
}
29+
```
30+
31+
To resolve the issue, you can set the value of the parameter. Or, if the value is always initialized before the function is called, change the SAL annotation to `_InOut_`. By setting the value of the parameter, the following code avoids the warning:
32+
33+
```cpp
34+
void example_func(_Out_ int *p1)
35+
{
36+
*p1 = 1;
37+
return;
38+
}
39+
```

0 commit comments

Comments
 (0)