www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - uncaught exceptions: stack trace truncated at NUL char

reply kdevel <kdevel vogtner.de> writes:
~~~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?
Dec 13 2020
parent reply KapyoniK <kapyonik gmail.com> writes:
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
Dec 13 2020
parent reply kdevel <kdevel vogtner.de> writes:
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)?!?
Dec 13 2020
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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.
Dec 13 2020
parent reply kdevel <kdevel vogtner.de> writes:
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.
Dec 13 2020
parent reply Paul Backus <snarwin gmail.com> writes:
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
Dec 13 2020
parent kdevel <kdevel vogtner.de> writes:
On Sunday, 13 December 2020 at 22:40:53 UTC, Paul Backus wrote:
 This is definitely a bug.
filed as Issue 21480
Dec 13 2020