Browse Source

+ added explicit check for a nil function parameter in ConvUtils.RegisterConversionType, in order to fix the units/convutils/tconv1 test on the WASI platform

Nikolay Nikolov 1 week ago
parent
commit
239d4fe7b7
1 changed files with 11 additions and 2 deletions
  1. 11 2
      packages/rtl-objpas/src/inc/convutil.inc

+ 11 - 2
packages/rtl-objpas/src/inc/convutil.inc

@@ -29,10 +29,10 @@ interface
 {$ifndef FPUNONE}
 {$ifndef FPUNONE}
 {$IFDEF FPC_DOTTEDUNITS}
 {$IFDEF FPC_DOTTEDUNITS}
 uses
 uses
-  System.SysUtils, System.Math;
+  System.SysUtils, System.Math, System.SysConst;
 {$ELSE FPC_DOTTEDUNITS}  
 {$ELSE FPC_DOTTEDUNITS}  
 uses
 uses
-  sysutils, math;
+  sysutils, math, SysConst;
 {$ENDIF FPC_DOTTEDUNITS}
 {$ENDIF FPC_DOTTEDUNITS}
 
 
 
 
@@ -569,6 +569,15 @@ end;
 function RegisterConversionType(Fam: TConvFamily; const S: String;
 function RegisterConversionType(Fam: TConvFamily; const S: String;
   const AToCommonFunc, AFromCommonFunc: TConversionProc): TConvType;
   const AToCommonFunc, AFromCommonFunc: TConversionProc): TConvType;
 begin
 begin
+  // Many platforms automatically raise an exception when calling a nil pointer
+  // function, but some don't. WebAssembly, for example, traps and terminates
+  // the program, without the ability to handle the exception. Others, like
+  // real mode DOS, crash. Since we have tests that pass a nil parameter and
+  // expect an exception, we also add an explicit check, so it works
+  // consistently on all platforms.
+  if not Assigned(AToCommonFunc) then
+    raise EAccessViolation.Create(SAccessViolation);
+
   result:=InternalRegisterConversionType(Fam,S,(AToCommonFunc(1)-AToCommonFunc(0)),AToCommonFunc,AFromCommonFunc);
   result:=InternalRegisterConversionType(Fam,S,(AToCommonFunc(1)-AToCommonFunc(0)),AToCommonFunc,AFromCommonFunc);
 end;
 end;