-
August 2nd, 2001, 04:48 PM
#1
Warning! MAKE_HRESULT macro doesn't work!
I was using the SDK supplied "MAKE_HRESULT" macro to generate custom HRESULT values. Then I noticed that some of my values were getting duplicated and traced it back to the MAKE_RESULT macro having a serious flaw. The macro pieces together values using the << shift operator. This operator pushes bits off the edge causing duplicate HRESULTs to appear!
First of all, an HRESULT is defined as a LONG, meaning it contains at most 32 bits.
Here is how the MAKE_HRESULT macro works:
Pass in 3 values and those are bit shifted into different sections of the HRESULT allowing you to make unique values. Here is the macro:
#define MAKE_HRESULT(sev,fac,code)\
((HRESULT) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) \
| ((unsigned long)(code))) )
For the "Sev" value, you are the use a different value signifying the severity of the error:
00 - Success
01 - Informational
10 - Warning
11 - Error
If you notice, the macro shifts the "Sev" value by 31 bits, not 30. This generates 2 problems:
1) The upper bit of Warning and Error are lost
2) Success and Warning have have the same value and Informational and Error have the same value.
For example, say you define 2 HRESULTS:
MY_INFO = MAKE_HRESULT(0x01, FACILITY_ITF, 5);
MY_ERROR = MAKE_HRESULT(0x03, FACILITY_ITF, 5);
// Note: 0x03 is 11 in binary
The actual value of my 2 error codes should be different, but due to the bug in MAKE_HRESULT the values will be identical!
Imagine if you were checking a function return for MY_INFO so you could treat it as a success, but the function actually returned MY_ERR!
I'm planning on contacting Microsoft about this tomorrow because this is a serious issue and who knows how many app crashes this has generated.
Why are the "tolerant" so easy to offend?
-
August 2nd, 2001, 05:28 PM
#2
Re: Warning! MAKE_HRESULT macro doesn't work!
Unlike NTSTATUS codes, HRESULTs have only two severity values:
0 - success
1 - failure
so, MAKE_HRESULT definition is correct and the way you use it
in MAKE_HRESULT(0x03, FACILITY_ITF, 5) is incorrect.
-
August 3rd, 2001, 06:35 AM
#3
Re: Warning! MAKE_HRESULT macro doesn't work!
According to the documentation, the "sev" portion of the HRESULT can be one of 4 different values requiring 2 bits of data but only 1 bit is allowed.
Either one thing is wrong: either an HRESULT can only be an error or success and the documentation is incorrect or the MAKE_HRESULT macro is incorrect.
Why are the "tolerant" so easy to offend?
-
August 3rd, 2001, 10:29 AM
#4
Re: Warning! MAKE_HRESULT macro doesn't work!
-
February 11th, 2002, 02:48 AM
#5
Re: Warning! MAKE_HRESULT macro doesn't work!
MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x200+1)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|