> More troubles with undefined-behavior. > > This type of code can leaves overflow: > var = (cast) (expression); > diff = (int32) (id1 - id2); > > See: > diff64 = ((long int) d1 - (long int) d2); > diff64=-4294901760
Did you compile this with gcc -fwrapv?
gcc 10.2 -O2 -fwrapv
bool test1()
{
unsignedint d1 =3;
unsignedint d2 =4294901763;
longint diff64 =0;
diff64 =((longint) d1 -(longint) d2);
return(diff64 <0);
}
output:
moveax, 1
ret
What is a workaround for msvc 2019 (64 bits) and clang 64 bits (linux)?
transam.c:311:22: runtime error: unsigned integer overflow: 3 - 4294901763 cannot be represented in type 'unsigned int'
with Debug:
#include<stdio.h>
#include<stdint.h>
bool test1(void)
{
unsignedint d1 =3;
unsignedint d2 =4294901763;
int32_t diff;
diff =(int32_t)(d1 - d2);
return(diff <0);
}
gcc 10.2 -g
output:
pushrbp
movrbp, rsp
movDWORDPTR[rbp-4], 3
movDWORDPTR[rbp-8], -65533
moveax, DWORDPTR[rbp-4]
subeax, DWORDPTR[rbp-8]
movDWORDPTR[rbp-12], eax
moveax, DWORDPTR[rbp-12]
shreax, 31
poprbp
ret
it is possible to conclude that: 1. TransactionIdPrecedes works in release mode, because the compiler treats undefined-behavior and corrects it, treating a possible overflow. 2. TransactionIdPrecedes does not work in debug mode, and overflow occurs. 3. TransactionID cannot contain the largest possible ID or an invalid ID (4294901763) has been generated and passed to TransactionIdPrecedes.