Fixing "toStringz"
Jonathan M Davis
jmdavisProg at gmx.com
Wed Aug 21 11:28:54 PDT 2013
On Wednesday, August 21, 2013 08:18:47 monarch_dodra wrote:
> I'm looking for advice on fixing "toStringz".
[snip]
> ideas?
Well, I suspect that we have one of two options. Either
1. Do something with the type system as Andrej suggested.
2. Check whether the string refers to the section of memory that the string
literals are put in.
Anything else would probably involve making assumptions like toStringz is
currently doing. Augmenting the type system clearly isn't a short term
solution, even if it were determined to be a good idea (and I'm not convinced
that it's at all worth it), so that's out at least for the moment.
Checking whether the string is in the memory range that string literals may or
may not be feasible. It is my understanding that on Linux, string literals are
put into a section of the binary which is treated as read-only, so I would
think that it would be possible to get the start and end points of that memory
range and check it, but I don't know how you'd do that (you'd also have to do
it in a static constructor or somethnig if you wanted to maintain purity,
which isn't entirely ideal). I also have no idea whether that's possible on
Windows, since, as I recall, the string literals get put into writable memory
on Windows, so there may be no special memory range to check.
Other than that, unfortunately, I don't know how you'd check for string
literals unless there's some piece of information that druntime has that I
don't know about or can't think of at the moment.
Now, another optimization that could be added which would be of less benefit
and would not affect string literals would be to check whether the last
character in the string is '\0', and that would at least catch the cases where
someone had already appended a null character or had a null-terminated string
for one reason or another. So, as a first-pass fix, I'd suggest simply doing
that check and dropping the attempt to catch string literals. That way, we at
least get the correctness issue fixed. Once that's done, we can look further
into how we might determine whether a string is a literal or not.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list