warning C6031: Return value ignored: 'GetLastError'

#ifdef _M_CEE_PURE
#define GetLastError System::Runtime::InteropServices::Marshal::GetLastWin32Error
#else
WINBASEAPI
__checkReturn
DWORD
WINAPI
GetLastError(
    VOID
    );
#endif

 

返回参数有个 __checkReturn 

查msdn发现:

warning C6031: return value ignored: <function> could return unexpected value

This warning indicates that the calling function is not checking the return value of a function call that signals failure via its return value. Depending on which function is being called, this defect can lead to seemingly random program misbehavior, including crashes and data corruptions in error conditions or low-resource situations.

In general, it is not safe to assume that a call to function requiring disk, network, memory, or other resources will always succeed. The caller should always check the return value and handle error cases appropriately.

Example

The following code generates this warning:

#include <stdio.h>
  void f( )
  {
    fopen( "test.c", "r" ); // return value ignored
    // code ...
  }
  
#include <stdio.h>
void f( )
{
  fopen( "test.c", "r" ); // return value ignored
  // code ...
}

To correct this warning, check the return value of the function as shown in the following code:

#include <stdio.h>
  void f( )
  {
    FILE *stream;
    if((stream = fopen( "test.c", "r" )) == NULL ) 
      return;
    // code ...
  }
  
#include <stdio.h>
void f( )
{
  FILE *stream;
  if((stream = fopen( "test.c", "r" )) == NULL ) 
    return;
  // code ...
}

The following code uses safe function fopen_s to correct this warning:

#include <stdio.h>
  void f( )
  {
    FILE *stream;
    errno_t err;
  
    if( (err  = fopen_s( &stream, "test.c", "r" )) !=0 )
    {
      // code ...
    }
  }
  
#include <stdio.h>
void f( )
{
  FILE *stream;
  errno_t err;

  if( (err  = fopen_s( &stream, "test.c", "r" )) !=0 )
  {
    // code ...
  }
}

This warning is also generated if the caller ignores the return value of a function annotated with the MustCheck property as shown in the following code:

  #include <codeanalysis\sourceannotations.h>
  [returnvalue:SA_Post(MustCheck=SA_Yes)] bool func( );
  
  
#include <codeanalysis\sourceannotations.h>
[returnvalue:SA_Post(MustCheck=SA_Yes)] bool func( );

void test_f()

{

func( ); //return value ignored

}

To correct the previous warning, check the return value as shown in the following code:

void test_f()

{

if( func( ) )

{

//code...

}

}

 

继续查MustCheck:

The MustCheck property specifies whether the caller must inspect the return value of a function. This property is used as a post condition on the return value of a function.

The MustCheck property must be set by using one of the following values:

  • SA_Yes - the return value must be checked; otherwise warning 6031 is issued.

  • SA_No - the return value should not be checked.

只有当MustCheck是SA_Yes时才会要求检查返回值(Checking a return value of a function marked with SA_No does not generate a warning.)

而windows SDK的__checkReturn就相当于(MustCheck=SA_Yes)

总结:检查返回值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值