overflow on division of large Cardinal by 1, if of range type.
Original Reporter info from Mantis: dleducq
-
Reporter name: Dominique Leducq
Original Reporter info from Mantis: dleducq
- Reporter name: Dominique Leducq
Description:
On i386, when dividing an unsigned 32 bits integer greater than 3^31 by 1 as a ragne type variable, I get an error : investigating this I found that idiv (signed integer division) was used, thus the result can not fit as signed 32 bits integer.
A conversion to 64 bits should be made as whith other cases of mixing cardinals and unsigned numbers.
Steps to reproduce:
program divtest;
type
range = 0..32;
var
a,b : Cardinal;
one : range;
begin
a := $80000000;
one := 1;
b := a div one;
WriteLn(b);
end.
$ ./divtest
Runtime error 200 at $080480AD
$080480AD
$0805EA41
Program received signal SIGFPE, Arithmetic exception.
0x080480ad in main () at divtest.pas:13
warning: Source file is more recent than executable.
13 b := a div one;
(gdb) disassemble
Dump of assembler code for function main:
0x08048080 <main+0>: push %ebp
0x08048081 <main+1>: mov %esp,%ebp
0x08048083 <main+3>: sub $0x4,%esp
0x08048086 <main+6>: mov %ebx,0xfffffffc(%ebp)
0x08048089 <main+9>: call 0x8056dc0 <fpc_initializeunits>
0x0804808e <main+14>: movl $0x80000000,0x8063390
0x08048098 <main+24>: movb $0x1,0x8063398
0x0804809f <main+31>: movzbl 0x8063398,%ecx
0x080480a6 <main+38>: mov 0x8063390,%eax
0x080480ab <main+43>: xor %edx,%edx
0x080480ad <main+45>: idiv %ecx
0x080480af <main+47>: mov %eax,0x8063394
0x080480b4 <main+52>: call 0x805b700 <fpc_get_output>
0x080480b9 <main+57>: mov %eax,%ebx
0x080480bb <main+59>: mov 0x8063394,%ecx
0x080480c1 <main+65>: mov %ebx,%edx
0x080480c3 <main+67>: mov $0x0,%eax
0x080480c8 <main+72>: call 0x805be70 <fpc_write_text_uint>
0x080480cd <main+77>: call 0x8056c40 <fpc_iocheck>
0x080480d2 <main+82>: mov %ebx,%eax
0x080480d4 <main+84>: call 0x805b8a0 <fpc_writeln_end>
0x080480d9 <main+89>: call 0x8056c40 <fpc_iocheck>
0x080480de <main+94>: call 0x8057010 <SYSTEM_DO_EXIT>
0x080480e3 <main+99>: mov 0xfffffffc(%ebp),%ebx
0x080480e6 <main+102>: leave
0x080480e7 <main+103>: ret
End of assembler dump.
(gdb) p $eip
$1 = (^(procedure )) 0x80480ad <main+45>
Mantis conversion info:
- Mantis ID: 8870
- Build: rev 7332
- Platform: i386
- Version: 2.2.0
- Fixed in version: 2.2.0
- Fixed in revision: 7345 (#d4978a95)