String copying fails when output from assert

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Nov 21 19:21:00 UTC 2017


On Tue, Nov 21, 2017 at 12:05:21PM -0700, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Tuesday, November 21, 2017 18:49:40 David Zhang via Digitalmars-d-learn 
> wrote:
[...]
> >   char[64] chars;
> >   chars[0..str.length] = str;
> >   assert(false, chars[0..str.length]);
[...]
> Well, the assertion is going to throw an AssertError, which takes a
> string for its message. It doesn't copy the contents of the string.
> It's just taking a slice just like whenever you pass a string to any
> other function.  So, it refers to the same memory as what's passed in.
> So, if what it's passed is a string that refers to memory on the
> stack, then when it goes to print the message, it's going to be
> garbage, because the stack was unwound, and the static array is gone.
> 
> Honestly, I'd argue that it's a bug that it allows you provide a
> message as a char[] instead of immutable(char)[]. It seems that the
> compiler is implicitly converting char[] to immutable(char)[] in this
> case, which is very bad. It would matter less if you were giving the
> assertion a char[] that referred to memory on the heap, but it still
> shouldn't be allowed.

Yeah, this is a bug.  I filed an issue:

	https://issues.dlang.org/show_bug.cgi?id=18003


T

-- 
The peace of mind---from knowing that viruses which exploit Microsoft system vulnerabilities cannot touch Linux---is priceless. -- Frustrated system administrator.


More information about the Digitalmars-d-learn mailing list