Jump to content
JonRobertson

Unsafe code 'String index to var param'

Recommended Posts

A project that I inherited has warning "W1047 Unsafe code" enabled. I normally do not enable that warning. There is a scenario that I don't understand, even after some online searches.

[dcc32 Warning] W1047 Unsafe code 'String index to var param'

From a couple posts that I read, it seems this specific unsafe warning is related to multiple platform support and zero based strings.

 

It occurs in this (totally useless) code segment:

function Test(const aStr: String): String;
begin
  SetLength(Result, Length(aStr));
  var Count := 0;
  for var i := 1 to Length(aStr) do begin
      Inc(Count);
      Result[Count] := aStr[i];
  end;
end;

on Result[Count] := aStr;

 

The wording of the warning confuses me. There is an index to a string, but not what I consider a 'string index'. Does the [] operator for String receive the index as a var param?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×