To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
V716. Suspicious type conversion: HRESU…
Analyzer Diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Diagnosis of micro-optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
MISRA errors
AUTOSAR errors
Additional information
Contents

V716. Suspicious type conversion: HRESULT -> BOOL (BOOL -> HRESULT).

Dec 10 2014

Analyzer has found a code that explicitly or implicitly casts value from BOOL type to HRESULT type or vice versa. Whilst this operation is possible in terms of C++ language, it does not have any practical meaning. HRESULT type is meant to keep a return status. It has relatively difficult format and it does not have anything common with BOOL type.

It is possible to provide an example from real-life application:

BOOL WINAPI DXUT_Dynamic_D3D10StateBlockMaskGetSetting(....)
{
    if( DXUT_EnsureD3D10APIs() &&
        s_DynamicD3D10StateBlockMaskGetSetting != NULL )
        ....
    else
        return E_FAIL;
}

The main danger here is in the fact that HRESULT type is, actually, 'long' type, while BOOL type is 'int'. These types can be easily cast to each other and compiler does not find anything suspicious in code above.

However, from programmer's point of view, these types differs. While BOOL type is a logical variable, HRESULT type haves difficult structure and should signal about an operation result: was operation successful, which result operation returned after successful execution, in case of error - where the error occurred, in which circumstances etc.

Let us talk about HRESULT type. First bit, counting from left side (i.e. the most significant bit) keeps whether operation was successful or not: if it was successful, first bit is set to zero, if not - to one. Next four bits describes kind of error. Next eleven bits describes the module that ran into exception. Last sixteen bits, most insignificant ones, describes operation status: on unsuccessful execution it may hold error code, on successful - its status. MSDN provides detailed description in this article.

BOOL type should be equal to zero to represent logical value "false"; otherwise, it represents logical value "true". Speaking other way, these types look like each other in terms of types and their conversion to each other, but conversion operation makes no sense. Initial idea of HRESULT type is to keep not only information about success or failure, but also some additional information about function call. Value S_FALSE of HRESULT type can be the most dangerous, because it is equal to 0x1. The fact that on successful run non-zero values is rare may be a reason to painful debugging in search for errors that shows up from time to time.

We encourage usage of SUCCEEDED and FAILED macro to control function return value.

HRESULT someFunction(int x);
....
BOOL failure = FAILED(someFunction(q));

In other cases, refactoring is not as simple as it looks and at least require some serious code analysis.

Let us stress that again. Remember that:

  • FALSE == 0
  • TRUE == 1
  • S_OK == 0
  • S_FALSE == 1
  • E_FAIL == 0x80004005
  • etc.

Never mix up HRESULT and BOOL. Mixing these types is a serious error in program's operation logic. To check HRESULT values use special macro.

Related V543 diagnostics tries to search situations, where logical 'true' or 'false' is put into a variable of HRESULT type.

This diagnostic is classified as:

You can look at examples of errors detected by the V716 diagnostic.

This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept