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"?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With