View Issue Details

IDProjectCategoryView StatusLast Update
0027173FPCCompilerpublic2015-01-05 13:04
ReporterCyrax Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformWindows OSServer 2008 R2 
Product Version2.7.1 
Fixed in Version3.0.0 
Summary0027173: Exception 'External: SIGFPE' raised when using variable of subrange type as divisor.
DescriptionSee summary, additional information and this bug report for more info : http://bugs.freepascal.org/view.php?id=27167

Possible patch by Do-wan Kim ("ranged_division_overflow_nx86mat.pas.patch") is attached in that bug report, too
Steps To ReproduceCompile attached file "error.pp" in bug report http://bugs.freepascal.org/view.php?id=27167
Additional InformationProject error raised exception class 'External: SIGFPE'.

 In file 'error.pp' at line 14:
c := -5 div b;
TagsNo tags attached.
Fixed in Revision27173
FPCOldBugId
FPCTarget
Attached Files

Relationships

related to 0027167 resolvedJuha Manninen Lazarus TControlbar Arithmetic overflow 

Activities

Do-wan Kim

2014-12-19 23:54

reporter  

division_overflow_nx86mat.pas.patch (811 bytes)   
Index: compiler/x86/nx86mat.pas
===================================================================
--- compiler/x86/nx86mat.pas	(revision 29305)
+++ compiler/x86/nx86mat.pas	(working copy)
@@ -528,7 +528,9 @@
               emit_reg_reg(A_XOR,opsize,regd,regd);
 
             {Division depends on the right type.}
-            if is_signed(right.resultdef) then
+			{ A_DIV generate SIGFPE when result does not fit register size(edx still remains after divide) after full-register-size unsigned division. 
+			  To prevent this, full-register-size signed division should be used A_IDIV. }
+            if is_signed(right.resultdef) or (is_signed(left.resultdef) and (left.resultdef.size={$ifdef x86_64}8{$else}4{$endif x86_64})) then
               op:=A_IDIV
             else
               op:=A_DIV;

Do-wan Kim

2014-12-20 00:05

reporter   ~0079906

Last edited: 2014-12-20 07:56

View 2 revisions

SIGFPE generated when edx:eax fair divide result doesn't fit eax.
if signed value, edx filled with sign bit unsigned division cannot work with it.
Full register signed division(edx:eax) should be used IDIV.
If not, edx assumed unsigned value of high dword, and unsigned division result cannot fit eax, it raise SIGFPE integer overflow exception.

movl $2,%edx
movl $-1,%eax
movl $2,%ecx
divl %ecx

it raise error because result has 33bit(edx has 1bit). if %ecx is $4, there is no problem.

more detail informations, #DE is key of this problem.
http://www.tptp.cc/mirrors/siyobik.info/instruction/DIV.html

Do-wan Kim

2014-12-20 08:24

reporter  

division_overflow_rev2_nx86mat.pas.patch (654 bytes)   
Index: compiler/x86/nx86mat.pas
===================================================================
--- compiler/x86/nx86mat.pas	(revision 29306)
+++ compiler/x86/nx86mat.pas	(working copy)
@@ -528,7 +528,9 @@
               emit_reg_reg(A_XOR,opsize,regd,regd);
 
             {Division depends on the right type.}
-            if is_signed(right.resultdef) then
+			{ SIGFPE when quotient is too large for the designated register.
+			  http://www.tptp.cc/mirrors/siyobik.info/instruction/DIV.html }
+            if is_signed(right.resultdef) or is_signed(left.resultdef) then
               op:=A_IDIV
             else
               op:=A_DIV;

Do-wan Kim

2015-01-03 05:38

reporter   ~0080077

Is This bug not proven yet? Interger division overflow(quotient too big, not fit register) can raise SIGFPE.

Florian

2015-01-04 14:13

administrator   ~0080103

I used a different approach, the instruction is determined as the generic cg does.

Issue History

Date Modified Username Field Change
2014-12-17 03:02 Cyrax New Issue
2014-12-17 10:31 Juha Manninen Relationship added related to 0027167
2014-12-19 23:54 Do-wan Kim File Added: division_overflow_nx86mat.pas.patch
2014-12-20 00:05 Do-wan Kim Note Added: 0079906
2014-12-20 07:56 Do-wan Kim Note Edited: 0079906 View Revisions
2014-12-20 08:24 Do-wan Kim File Added: division_overflow_rev2_nx86mat.pas.patch
2015-01-03 05:38 Do-wan Kim Note Added: 0080077
2015-01-04 14:12 Florian Fixed in Revision => 27173
2015-01-04 14:12 Florian Status new => resolved
2015-01-04 14:12 Florian Fixed in Version => 2.7.1
2015-01-04 14:12 Florian Resolution open => fixed
2015-01-04 14:12 Florian Assigned To => Florian
2015-01-04 14:13 Florian Note Added: 0080103
2015-01-05 13:04 Cyrax Status resolved => closed