Thread overview
uncaught exceptions: stack trace truncated at NUL char
Dec 13, 2020
kdevel
Dec 13, 2020
KapyoniK
Dec 13, 2020
kdevel
Dec 13, 2020
rikki cattermole
Dec 13, 2020
kdevel
Dec 13, 2020
Paul Backus
Dec 14, 2020
kdevel
December 13, 2020
~~~char2.d
void main ()
{
   import std.stdio;
   import std.conv;
   char [2] win = [0, 'X'];
   auto ne = new Exception ("A " ~ win.to!string ~ " B");
   try throw ne;
   catch (Exception e)
      writeln ("exception caught: e.msg = <", e.msg, ">");
   throw ne;
}
~~~

Output:

exception caught: e.msg = <A X B>
object.Exception@char2.d(6): A              <--- truncated at \0
[...]

$ ./char2 | hexdump -c
0000000   e   x   c   e   p   t   i   o   n       c   a   u   g   h   t
0000010   :       e   .   m   s   g       =       <   A      \0   X
0000020   B   >  \n

$ ./char2 2>&1 1>/dev/null |hexdump -c
0000000   o   b   j   e   c   t   .   E   x   c   e   p   t   i   o   n
0000010   @   c   h   a   r   2   .   d   (   6   )   :       A      \n
0000020   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
[...]

Shall I file a bug?
December 13, 2020
On Sunday, 13 December 2020 at 11:51:19 UTC, kdevel wrote:
> ~~~char2.d
> void main ()
> {
>    import std.stdio;
>    import std.conv;
>    char [2] win = [0, 'X'];
>    auto ne = new Exception ("A " ~ win.to!string ~ " B");
>    try throw ne;
>    catch (Exception e)
>       writeln ("exception caught: e.msg = <", e.msg, ">");
>    throw ne;
> }
> ~~~
>
> [...]

Is it really a bug ? \0 truncates the string, as mentionned on this page :
https://en.wikipedia.org/wiki/Null-terminated_string
December 13, 2020
On Sunday, 13 December 2020 at 20:25:06 UTC, KapyoniK wrote:
> Is it really a bug ? \0 truncates the string, as mentionned on this page :
> https://en.wikipedia.org/wiki/Null-terminated_string

I thought the D runtime is written in D (with D strings)?!?
December 14, 2020
On 14/12/2020 9:56 AM, kdevel wrote:
> On Sunday, 13 December 2020 at 20:25:06 UTC, KapyoniK wrote:
>> Is it really a bug ? \0 truncates the string, as mentionned on this page :
>> https://en.wikipedia.org/wiki/Null-terminated_string
> 
> I thought the D runtime is written in D (with D strings)?!?

String literals are null terminated by the compiler. It is very useful for communicating with C.
December 13, 2020
On Sunday, 13 December 2020 at 20:58:42 UTC, rikki cattermole wrote:

[...]

> String literals are null terminated by the compiler. It is very useful for communicating with C.

Sure, but in the example given there is an embedded NUL which as part
of an exception msg. If caught everything works as expected, but if
the stack is unwound the information is lost due to truncation.
December 13, 2020
On Sunday, 13 December 2020 at 21:22:16 UTC, kdevel wrote:
> On Sunday, 13 December 2020 at 20:58:42 UTC, rikki cattermole wrote:
>
> [...]
>
>> String literals are null terminated by the compiler. It is very useful for communicating with C.
>
> Sure, but in the example given there is an embedded NUL which as part
> of an exception msg. If caught everything works as expected, but if
> the stack is unwound the information is lost due to truncation.

This is definitely a bug. The problem is that the D runtime uses `fprintf` to print the exception's error message, when it should be using `fwrite`:

https://github.com/dlang/druntime/blob/v2.094.2/src/rt/dmain2.d#L733
December 14, 2020
On Sunday, 13 December 2020 at 22:40:53 UTC, Paul Backus wrote:
> This is definitely a bug.

filed as Issue 21480