dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.12k stars 4.7k forks source link

Trace.Write cuts off at '\0' #30610

Closed Symbai closed 4 years ago

Symbai commented 5 years ago

I'm not sure if this is intended or not but it Trace.Write and WriteLine cuts off text as soon as char '\0' appears. No error or exception, its just missing the rest of the text.

Repro:

char[] bla = new char[] { 'b', 'l', 'a', '\0' };
Trace.Write($"abc {new string(bla)}, rest of text");

results in output window text: abc bla

svick commented 5 years ago

Are you talking about the Output window in Visual Studio? I can reproduce this issue there, but not when using a ConsoleTraceListener.

Based on that and the fact that Debugger.Log(0, null, "abc bla\0, rest of text"); (which is used by DefaultTraceListener) also exhibits this behavior, I think the issue is either in Debugger.Log or in Visual Studio.

stephentoub commented 5 years ago

Win32's OutputDebugString just takes a string pointer, no length, so it will only consume up to a null terminator.

svick commented 5 years ago

@stephentoub It seems CoreCLR also intentionally truncates strings with embedded NULs when they're logged using DebugInterface::SendLogMessage. I don't know if VS uses that or OutputDebugString, but I do wonder what is the reason behind truncating the strings there.

Symbai commented 5 years ago

@svick Yes I'm talking about the output window in VS. It has cost me a bit of time figuring out why a line didn't contained the information it should have until I noticed a char array had a '\0' char in it. Personally I feel like it shouldn't be the case that it cuts off the text at this point there.

tarekgh commented 5 years ago

I think whatever way used for debugger logging, either will be ended calling OutDebugString as @stephentoub pointed out or it will truncate the message at the null terminator before sending it to the debugger interface.

https://source.dot.net/#System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Windows.cs,65 https://github.com/dotnet/coreclr/blob/46d98155f74fe3ff5ce82da033ead9fe108dda8f/src/vm/debugdebugger.cpp#L301

So this is by design I guess.

Symbai commented 5 years ago

In this case I'm closing the issue then.