SQLite Forum

integer overflow in pager.c
Login
I asked my C compiler (GCC 4.8.5/RH7/IA64) about this:

    int32_t              a = 65536;
    int32_t              b = 0x7ffffff;
    int64_t              p;

    p = a*b;

and got this

32 bit init:   c7 45 fc 00 00 01 00    movl   $0x10000,-0x4(%rbp)
32 bit init:   c7 45 f8 ff ff ff 07    movl   $0x7ffffff,-0x8(%rbp)
32 bit load:   8b 45 fc                mov    -0x4(%rbp),%eax
32 bit mult:   0f af 45 f8             imul   -0x8(%rbp),%eax
32->64 conv:   48 98                   cltq
64 bit stor:   48 89 45 f0             mov    %rax,-0x10(%rbp)

and then about this:

    int32_t              a = 65536;
    int32_t              b = 0x7ffffff;
    int64_t              p;

    p = (int64_t)a*b;

32 bit init:   c7 45 fc 00 00 01 00    movl   $0x10000,-0x4(%rbp)
32 bit init:   c7 45 f8 ff ff ff 07    movl   $0x7ffffff,-0x8(%rbp)
32 bit load:   8b 45 fc                mov    -0x4(%rbp),%eax
64 bit cast:   48 63 d0                movslq %eax,%rdx
32 bit load:   8b 45 f8                mov    -0x8(%rbp),%eax
32->64 conv:   48 98                   cltq
64 bit mult:   48 0f af c2             imul   %rdx,%rax
64 bit stor:   48 89 45 f0             mov    %rax,-0x10(%rbp)

So yes, there may be truncation without a cast. Machine code is 6 bytes longer and 64 bit multiplication seems to require both operands in registers.