Browse Source

* fixed web bug #2139: checking for division by zero fixed

Jonas Maebe 23 years ago
parent
commit
e3700b1d79
2 changed files with 43 additions and 32 deletions
  1. 23 16
      compiler/nadd.pas
  2. 20 16
      compiler/nmat.pas

+ 23 - 16
compiler/nadd.pas

@@ -197,10 +197,26 @@ implementation
          rt:=right.nodetype;
          lt:=left.nodetype;
 
+       if (nodetype = slashn) and
+          (((rt = ordconstn) and
+            (tordconstnode(right).value = 0)) or
+           ((rt = realconstn) and
+            (trealconstnode(right).value_real = 0.0))) then
+         begin
+           Message(parser_e_division_by_zero);
+           case rt of
+             ordconstn:
+                tordconstnode(right).value := 1;  
+             realconstn:   
+                trealconstnode(right).value_real := 1.0;
+           end;
+         end;
+
+
          { both are int constants }
          if (((is_constintnode(left) and is_constintnode(right)) or
               (is_constboolnode(left) and is_constboolnode(right) and
-               (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])))) or
+               (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])))) or
             { support pointer arithmetics on constants (JM) }
             ((lt = pointerconstn) and is_constintnode(right) and
              (nodetype in [addn,subn])) or
@@ -299,13 +315,7 @@ implementation
                     { int/int becomes a real }
                     rvd:=rv;
                     lvd:=lv;
-                    if int(rvd)=0 then
-                     begin
-                       Message(parser_e_invalid_float_operation);
-                       t:=crealconstnode.create(0,pbestrealtype^);
-                     end
-                    else
-                     t:=crealconstnode.create(int(lvd)/int(rvd),pbestrealtype^);
+                    t:=crealconstnode.create(lvd/rvd,pbestrealtype^);
                   end;
                 else
                   CGMessage(type_e_mismatch);
@@ -341,13 +351,7 @@ implementation
                    end;
                  slashn :
                    begin
-                     if rvd=0 then
-                      begin
-                        Message(parser_e_invalid_float_operation);
-                        t:=crealconstnode.create(0,pbestrealtype^);
-                      end
-                     else
-                      t:=crealconstnode.create(lvd/rvd,pbestrealtype^);
+                     t:=crealconstnode.create(lvd/rvd,pbestrealtype^);
                    end;
                  ltn :
                    t:=cordconstnode.create(ord(lvd<rvd),booltype,true);
@@ -1814,7 +1818,10 @@ begin
 end.
 {
   $Log$
-  Revision 1.65  2002-09-07 15:25:02  peter
+  Revision 1.66  2002-10-04 21:19:28  jonas
+    * fixed web bug 2139: checking for division by zero fixed
+
+  Revision 1.65  2002/09/07 15:25:02  peter
     * old logs removed and tabs fixed
 
   Revision 1.64  2002/09/07 12:16:05  carl

+ 20 - 16
compiler/nmat.pas

@@ -106,29 +106,30 @@ implementation
          if codegenerror then
            exit;
 
-         { constant folding }
-         if is_constintnode(left) and is_constintnode(right) then
+         { check for division by zero }
+         if is_constintnode(right) then
            begin
-              rv:=tordconstnode(right).value;
-              lv:=tordconstnode(left).value;
-
-              { check for division by zero }
-              if (rv=0) then
+             rv:=tordconstnode(right).value;
+             if (rv=0) then
                begin
                  Message(parser_e_division_by_zero);
                  { recover }
                  rv:=1;
                end;
+             if is_constintnode(left) then
+               begin
+                 lv:=tordconstnode(left).value; 
 
-              case nodetype of
-                modn:
-                  t:=genintconstnode(lv mod rv);
-                divn:
-                  t:=genintconstnode(lv div rv);
+                  case nodetype of
+                   modn:
+                     t:=genintconstnode(lv mod rv);
+                   divn:
+                     t:=genintconstnode(lv div rv);
+                 end;
+                 result:=t;
+                 exit;
               end;
-              result:=t;
-              exit;
-           end;
+            end;
 
          { allow operator overloading }
          t:=self;
@@ -747,7 +748,10 @@ begin
 end.
 {
   $Log$
-  Revision 1.42  2002-09-07 12:16:04  carl
+  Revision 1.43  2002-10-04 21:19:28  jonas
+    * fixed web bug 2139: checking for division by zero fixed
+
+  Revision 1.42  2002/09/07 12:16:04  carl
     * second part bug report 1996 fix, testrange in cordconstnode
       only called if option is set (also make parsing a tiny faster)