1
1
---
2
2
title : Warning C6031
3
3
description : " Describes C++ Code Analysis warning C6031 and how to resolve it."
4
- ms.date : 10/04/2022
4
+ ms.date : 4/5/2024
5
5
f1_keywords : ["C6031", "RETVAL_IGNORED_FUNC_COULD_FAIL", "__WARNING_RETVAL_IGNORED_FUNC_COULD_FAIL"]
6
6
helpviewer_keywords : ["C6031"]
7
- ms.assetid : 59e1ef0a-b3ca-4ffa-bcb3-ad2bd22ece22
8
7
---
9
8
# Warning C6031
10
9
@@ -16,44 +15,48 @@ Warning C6031 indicates the caller doesn't check a function's return value for f
16
15
17
16
In general, it isn't safe to assume that calls to functions requiring disk, network, memory, or other resources will succeed. The caller should always check the return value and handle error cases appropriately. Also consider using the ` _Must_inspect_result_ ` annotation, which checks that the value is examined in a useful way.
18
17
18
+ This warning applies to both C and C++ code.
19
+
19
20
Code analysis name: ` RETVAL_IGNORED_FUNC_COULD_FAIL `
20
21
21
22
## Example
22
23
23
24
The following code generates warning C6031:
24
25
25
- ``` cpp
26
+ ``` c
26
27
#include < stdio.h>
27
- void f ( )
28
+ int main ( )
28
29
{
29
- fopen( "test.c", "r" ); // C4996, C6031 return value ignored
30
+ fopen ("test.c", "r"); // C4996, C6031 return value ignored
30
31
// code ...
31
32
}
32
33
```
33
34
34
35
To correct this warning, check the return value of the function as shown in the following code:
35
36
36
- ```cpp
37
+ ``` c
37
38
#include < stdio.h>
38
- void f( )
39
+ int main ( )
39
40
{
40
- FILE *stream;
41
- if ( (stream = fopen( "test.c", "r" )) == NULL )
41
+ FILE* stream;
42
+ if ((stream = fopen("test.c", "r")) == NULL)
43
+ {
42
44
return;
45
+ }
43
46
// code ...
44
47
}
45
48
```
46
49
47
50
The following code uses safe function ` fopen_s ` to correct this warning:
48
51
49
- ``` cpp
52
+ ``` c
50
53
#include < stdio.h>
51
- void f ( )
54
+ int main ( )
52
55
{
53
- FILE * stream;
56
+ FILE* stream;
54
57
errno_t err;
55
58
56
- if ( (err = fopen_s( &stream, "test.c", "r" )) !=0 )
59
+ if ((err = fopen_s(&stream, "test.c", "r")) != 0 )
57
60
{
58
61
// code ...
59
62
}
@@ -64,23 +67,30 @@ This warning is also generated if the caller ignores the return value of a funct
64
67
65
68
``` cpp
66
69
#include < sal.h>
67
- _Check_return_ bool func();
70
+ _Check_return_ bool func ()
71
+ {
72
+ return true;
73
+ }
68
74
69
- void test_f ()
75
+ int main ()
70
76
{
71
- func(); // Warning C6031
77
+ func ();
72
78
}
73
79
```
74
80
75
81
To correct the previous warning, check the return value as shown in the following code:
76
82
77
83
``` cpp
78
84
#include < sal.h>
79
- _Check_return_ bool func ();
85
+ _Check_return_ bool func ()
86
+ {
87
+ return true;
88
+ }
80
89
81
- void test_f ()
90
+ int main ()
82
91
{
83
- if ( func() ) {
92
+ if (func())
93
+ {
84
94
// code ...
85
95
}
86
96
}
@@ -91,10 +101,12 @@ In cases where it's necessary to ignore the return value of a function, assign t
91
101
``` cpp
92
102
#include < tuple>
93
103
#include < ctime>
104
+ #include < cstdlib>
94
105
#include < stdio.h>
95
- void f ()
106
+
107
+ int main ()
96
108
{
97
- std::srand (static_cast (std::time (nullptr))); // set initial seed value to system clock
109
+ std::srand (static_cast< unsigned int > (std::time(nullptr))); // set initial seed value to system clock
98
110
std::ignore = std::rand(); // Discard the first result as the few random results are always small.
99
111
// ...
100
112
}
0 commit comments