Skip to content

Commit 545191f

Browse files
luhenrymarek-safar
authored andcommitted
Pass null-terminated string for logging
1 parent 2c1f45f commit 545191f

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

mcs/class/corlib/System.IO/LogcatTextWriter.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LogcatTextWriter : TextWriter {
2020

2121
public LogcatTextWriter (string appname, TextWriter stdout)
2222
{
23-
this.appname = Encoding.UTF8.GetBytes (appname);
23+
this.appname = Encoding.UTF8.GetBytes (appname + '\0');
2424
this.stdout = stdout;
2525
}
2626

@@ -48,13 +48,43 @@ public override void WriteLine ()
4848
var o = line.ToString ();
4949
line.Clear ();
5050

51+
Log (o);
52+
stdout.WriteLine (o);
53+
}
54+
55+
void Log (string message)
56+
{
57+
const int buffer_size = 512;
58+
5159
unsafe {
52-
fixed (byte *b_appname = appname)
53-
fixed (byte *b_message = Encoding.UTF8.GetBytes(o)) {
54-
Log (b_appname, 1 << 5 /* G_LOG_LEVEL_MESSAGE */, b_message);
60+
if (Encoding.UTF8.GetByteCount (message) < buffer_size) {
61+
try {
62+
fixed (char *b_message = message) {
63+
byte* buffer = stackalloc byte[buffer_size];
64+
int written = Encoding.UTF8.GetBytes (b_message, message.Length, buffer, buffer_size - 1);
65+
buffer [written] = (byte)'\0';
66+
67+
Log (buffer);
68+
}
69+
70+
return;
71+
} catch (ArgumentException) {
72+
/* It might be due to a failure to encode a character, or due to a smaller than necessary buffer. In the
73+
* secode case, we want to fallback to simply allocating on the heap. */
74+
}
75+
}
76+
77+
fixed (byte *b_message = Encoding.UTF8.GetBytes(message + '\0')) {
78+
Log (b_message);
5579
}
5680
}
57-
stdout.WriteLine (o);
81+
}
82+
83+
unsafe void Log (byte* b_message)
84+
{
85+
fixed (byte *b_appname = appname) {
86+
Log (b_appname, 1 << 5 /* G_LOG_LEVEL_MESSAGE */, b_message);
87+
}
5888
}
5989

6090
public static bool IsRunningOnAndroid ()

0 commit comments

Comments
 (0)