The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,231 other followers

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: