ckurt99
asked on
Testing for USHORT variable overflow
As part of a bit of defensive programming, I would like to use an assert to make sure that when I cast a ULONG into a USHORT variable that I won't be overflowing the size of numbers that a USHORT can handle. Will the following work, do you think? I think it needs the cast to ULONG so that the comparison is done as a ULONG.
#define MAX_USHORT ((ULONG) ~((USHORT) 0x0))
ASSERT(myushortvar <= MAX_USHORT);
#define MAX_USHORT ((ULONG) ~((USHORT) 0x0))
ASSERT(myushortvar <= MAX_USHORT);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If myushortvar is USHORT the ASSERT will always be true
ASKER
Thank you for pointing that out, ozo. I should have used result_of_ulong_calc instead of myushortvar.