[DUG] StrCopy problem - Reason : Const value not allowedtobe changed

Paul Heinz paul at accredo.co.nz
Mon May 13 16:11:26 NZST 2013


Hi all

 

Since Delphi will store string constants in the code segment (that might
depend on the writable constants compiler option), 

I suspect you're hitting a DEP (Data Execution Protection) exception
trying to modify executable but not writeable memory.

 

DEP errors show up as Access Violations of the form AV at address <XYZ>
write of address <XYZ>  or AV at address <XYZ> read of address <XYZ>
where *both* addresses are the same.

 

This is in contrast to your garden variety Access Violations (e.g. nil
derefs, etc.) which have two *different* addresses in the message  - one
for the faulting code, the other indicating the faulting data.

 

Whether you will see these can be hardware and OS dependent as DEP
tracking is only available on Intel or AMD CPUs with NX support (which
is most these days) and DEP is only enabled by default for 'normal
applications' (as opposed to 'essential system components') on Server
OSes.

 

This OS difference mostly catches you out when running MS Office
applications under TS since MS's own applications aren't fully DEP safe,
especially their aging MAPI support. I suspect they are generating code
on the stack or in memory and then jumping to it without first mapping
the page executable like you're supposed to, but which never used to
matter on Intel processors pre them adding NX support.

 

Cheers,

  Paul.

 

 

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Jianming Lin
(FMI)
Sent: Monday, 13 May 2013 3:09 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] StrCopy problem - Reason : Const value not
allowedtobe changed

 

Hi, Ross,

 

a := 'abc';

a[1] := 'A';

 

Certainly it will fail. 'abc' is defined in a reserved memory area when
OS allocates memory for the executable.

 

As for 

a := @ConsStr[1];

a[1] := 'A';

 

I am wondering how did you define ConsStr? 

var ConsStr : string = 'abc'; ?

 

 

________________________________

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Monday, 13 May 2013 2:55 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] StrCopy problem - Reason : Const value not allowed
tobe changed

 

The String and pChar in my case are global vars and the code is
executing in a library dpr between begin..end.  There are no units or
objects or procedures in use.  This works fine when using global vars, I
just tried it...

 

a := @ConsStr[1];

a[1] := 'A';

 

But this fails...

 

a := 'abc';

a[1] := 'A';

 

Ross.

 

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Jianming Lin
(FMI)
Sent: Monday, 13 May 2013 10:35 AM
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] StrCopy problem - Reason : Const value not allowed to
be changed

 

It seems that we all focus on the StrCopy. That's misleading.

 

Let's look at this code:

 

procedure test;

var  a: pChar;

begin

   a := 'abc';

   a[1] := 'A';

end;

 

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  test;

end;

 

The line : a[1] will cause exactly same AV as your example.

 

As CPU window shows cpu instruction in assembly language, not many
people can understand it, I do further experiment:

 

 

 

procedure test;

var  a: pChar;

const ConsStr  = 'abc';

begin

   a := @ConsStr[1];

   a[1] := 'A';

end;

 

 

The same AV will happen. And that's the key problem where your code
actually is.

 

Now we can see that the reason of your code crash:

 

Delphi compile 'abcdefghi' as the local const value. 

Certainly constant value is not allowed to change.

If you put a line : ConsStr := 'def'; Compiler won't let you go.

If you purposely use the trick of pointer : 

   a := @ConsStr[1];

   a[1] := 'A';

to bypass compiler checking, the  operating system then has to activate
the last protection by showing the AV and stop your program to run. 

 

 

Bevan is right : the area of memory for const value is protected while
the application is running.

 

 

 

 

________________________________

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Sunday, 12 May 2013 2:39 a.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: [DUG] StrCopy problem

 

var

  a: pChar;

  b: pChar;

begin

  a := 'abcdefghi';

  b := 'jklmnopqr';

  StrCopy(a,b);

end;

 

Question:  Why does this code crash?

 

________________________________

Attention: 
This mail and any attachments are for the use of the intended recipient
only, and may contain information which is confidential and/or
privileged. If you have received this email in error, please advise us
by return email and immediately delete this email together with all
attachments. The contents of this email may only be used, distributed or
copied with the consent of the author. Fairview Metal Industries Ltd
takes reasonable precautions to minimise the risk of this email
containing any viruses but does not accept liability for any damage
caused by software viruses and advises the recipient to carry out their
own virus check on any attachments.

________________________________

________________________________

Attention: 
This mail and any attachments are for the use of the intended recipient
only, and may contain information which is confidential and/or
privileged. If you have received this email in error, please advise us
by return email and immediately delete this email together with all
attachments. The contents of this email may only be used, distributed or
copied with the consent of the author. Fairview Metal Industries Ltd
takes reasonable precautions to minimise the risk of this email
containing any viruses but does not accept liability for any damage
caused by software viruses and advises the recipient to carry out their
own virus check on any attachments.

________________________________

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20130513/a4cc4cc6/attachment-0001.html 


More information about the Delphi mailing list