Is this code safe? function Some(const Str : AnsiString); var arr : TBytes …
Posted by jpluimers on 2020/12/24
[WayBack] Is this code safe? function Some(const Str : AnsiString); var arr : TBytes absolute Str; begin fSomeMethodWithTBytesParam(arr); <- arr is properly c… – Jacek Laskowski – Google+
Such a seemingly simple question:
Is this code safe?
procedure Some(const Str : AnsiString); var arr : TBytes absolute Str; begin fSomeMethodWithTBytesParam(arr); // <- arr is properly casted to string bytes? end;
Such a wealth of information in the comments:
- No it is not, except for the nil-pointer case
- The compiler has for instance an implicit length prefix
- In 32-bit mode, the Length prefix of strings and dynamic arrays are both 32-bits
- In 64-bit mode, the Length prefix of strings is 32-bits, but the one for dynamic arrays is 64-bits
- use
BytesOf
to transform from characters to bytes- do not store binary data into strings of single-byte character sets
–jeroen
Leave a comment