-1

I am writing code in Go to call some of the Windows trust and crypt dlls to verify file signatures. There are many constants in wincrypt.h that I have tried to port over verbatim but i've hit some issues with integer overflow.

For example, all of the error codes that can be returned from WinVerifyTrust are negative values. If I take one example, TRUST_E_NOSIGNATURE, this is defined in winerror.h as so: #define TRUST_E_NOSIGNATURE _HRESULT_TYPEDEF_(0x800B0100L). In my Go code, I have const TRUST_E_NOSIGNATURE = int32(0x800B0100) but when compiled the error is:

constant 2148204800 overflows int32

when I really expected the value to be -2146762496

So, my questions 1) why does it not wrap like it does in other languages 2) Is there anyway to have the constant still use the hex representation of the number or will I have to change the code to const TRUST_E_NOSIGNATURE = int32(-2146762496) which works ok but will require me to make this change in many other constants that I have ported?

incubus
  • 681
  • 1
  • 5
  • 21

1 Answers1

3

You just set it:

const TRUST_E_NOSIGNATURE = int32(-2146762496)

Use hex if you wish:

const TRUST_E_NOSIGNATURE = int32(-0x7ff4ff00)

But for this, you're probably just using the wrong data type. Use a uint32 instead:

const TRUST_E_NOSIGNATURE = uint32(0x800B0100)

why does it not wrap like it does in other languages?

Because it wasn't designed that way. Go follows the philosophy of being as obvious and intuitive as possible. Silent wrapping is very non-intuitive.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Yup, like I mentioned in my question I could just set it like that but there are many in the windows code that have negative values; this was just one example. Perhaps the wording of my question wasn't quite right....what I meant was is it possible to use the same hex value as they have defined in the windows code? I'm not so familiar with the language (Go) so wasn't sure if there was some trick to make the wrap happen. – incubus Jun 11 '19 at 13:32
  • 1
    Yes, you can use the same hex value if you use a uint32. – Jonathan Hall Jun 11 '19 at 13:34
  • Unfortunately that won't fly when I put it to code review. The definition in Windows is long int and WinVerifyTrust (where i use it) returns a LONG so using the uint32 type (which does make the compile error go away) wasn't an option I had unfortunately. – incubus Jun 11 '19 at 13:38
  • 1
    Then you're stuck with one of the other options. – Jonathan Hall Jun 11 '19 at 13:42