Delphi - W1048 Insecure string type in TFormatSettings

I am migrating an old project to Delphi XE and I get this warning in the code below.

function RemoveThousandSeperator(Text: String) : String; Var P : Integer; begin if length(Text) > 3 then begin p := Pos(FormatSettings.ThousandSeparator,Text); while p >0 do begin Delete(Text,p,1); p := Pos(FormatSettings.ThousandSeparator,Text); end; end; result := Text; end; 

even FormatSettings.ThousandSeparator is of type char.

LE: I ask if anyone can tell me why this warning occurs. The code is old and it will be redone.

LE2: To receive this warning, all warnings must be set to true in the Delphi hint and warning compiler

LE3: If someone needs it - {$ WARN UNSAFE_CAST OFF}, the warning is turned off.

LE4: A screenshot of the warning for those who find the warning difficult to believe.

enter image description here

+4
source share
1 answer

The origin of the warning is the declaration of the FormatSettings variable in SysUtils.pas :

 var // Note: Using the global FormatSettings variable corresponds to using the // individual global formatting variables and is not thread-safe. FormatSettings: TFormatSettings absolute CurrencyString; 

which builds the string ( CurrencyString ) for the record ( TFormatSettings ).

So the problem that generates the warning is in SysUtils.pas , not in the code you posted, although the warning is generated in your code.


Here is a test case (Delphi XE):

 program Project1; {$APPTYPE CONSOLE} {$WARN UNSAFE_CAST ON} type TTest = record FS: string; end; var Str: string; Test: TTest absolute Str; begin Str:= 'abc'; Writeln(Test.FS); // W1048 Unsafe typecast of 'string' to 'TTest' end. 
+4
source

Source: https://habr.com/ru/post/1443395/


All Articles