How to set a constant to negative value in Go

-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?

go
constants
integer-overflow
asked on Stack Overflow Jun 11, 2019 by john

1 Answer

2

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.

answered on Stack Overflow Jun 11, 2019 by Flimzy

User contributions licensed under CC BY-SA 3.0