Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "W1047 Unsafe code '^ operator'" warning message?

I've enabled "Unsafe code" warnings and I'm wondering about how to solve the following warning:

W1047 Unsafe code '^ operator'

Furthermore, why is this considered as "Unsafe code"?

like image 667
Fabrizio Avatar asked Dec 10 '22 15:12

Fabrizio


1 Answers

The documentation says:

You have used a data type or operation for which static code analysis cannot prove that it does not overwrite memory. Such code can be considered a security risk.

For example, using GetMem can elicit this warning because a block of memory has no associated type.

There's really only two ways to avoid these warnings:

  1. Disable the warning.
  2. Change the code to stop using unsafe memory access.

Both options may be useful in some situations. For instance, consider Embarcadero's Delphi RTL code that supports dynamic arrays, for instance. It needs to be able to allocate memory, and access that memory using such unsafe operations. Such fundamental library code needs to be able to use unsafe operations. Considering your own code, if you need to perform unsafe code you might isolate it into a single unit, or a single section of a unit, and disable the warnings for just that code.

On to the second item. It may well be possible for you to replace code using pointers with code using some other construct. For instance, you might have code that uses pointer arithmetic that could instead be written using arrays.

I believe that these warnings were introduced to help developers migrate code to the long since abandoned Delphi .net compiler. So you might well take the view that since you are not targeting .net, you can simply disable those warnings. On the other hand, I can see scenarios where it could be useful to enable the warnings and be alerted to areas of code that are potentially more risky. The choice is yours.

like image 158
David Heffernan Avatar answered Dec 24 '22 00:12

David Heffernan