[fpc-devel] an idea about "const param: string"

Alexander Klenin klenin at gmail.com
Sat May 28 23:47:12 CEST 2011

On Sat, May 28, 2011 at 16:08, Martin <lazarus at mfriebe.de> wrote:
> I was thinking it would be cool to have something like range-checks, but for
> those strings.
> Ideally, the string would be flagged, and other code trying to access it
> would check this. But that would mean that all units, packages everything
> would need awareness, and had to be compiled with support for the flag....
> Not really practical.
> However at least some test would probably work.
> If compiled with -Cc (Check Constant string param) or whatever compiler
> option it would be, then the code in Foo could have checks.
> If foo would copy the value of s (only the address: @s[0], not the string)
> and increase the ref-count of s (it still is a pointer to the original s),
> then Foo could check on exit.
> If anyone modified s, then copy-on-write would have kicked in. either the
> address in s, or the ref count would have changed.
> That would allow to give at least a warning that something wrong was done.

Perhaps an easier thing to do is to make const string param optimization
controllable by compiler switch and/or directive.
This way, users who prefer safe behavior, can get it,
and those who are willing to take a risk still get a (weaker) tool to
detect the error --
by checking if the program works with the switch but crashes without it.

Alexander S. Klenin

More information about the fpc-devel mailing list