Skip to content

Commit 95a8f89

Browse files
authored
Fix null character termination in CString abstraction. (#7785)
## Summary of changes Add null termination byte for CString abstraction. ## Reason for change Testing the new libdatadog release I noticed some errors when Cstrings were passed to the process discovery native API. ## Implementation details Make extra space for null character and add the termination character at the end of the buffer. ## Test coverage 🙈 ## Other details
1 parent febe959 commit 95a8f89

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

tracer/src/Datadog.Trace/LibDatadog/CString.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ internal CString(string? str)
2828
{
2929
var encoding = StringEncoding.UTF8;
3030
var maxBytesCount = encoding.GetMaxByteCount(str.Length);
31-
Ptr = Marshal.AllocHGlobal(maxBytesCount);
31+
Ptr = Marshal.AllocHGlobal(maxBytesCount + 1); // Make space for null character.
3232
unsafe
3333
{
3434
fixed (char* strPtr = str)
3535
{
3636
try
3737
{
38-
Length = (nuint)encoding.GetBytes(strPtr, str.Length, (byte*)Ptr, maxBytesCount);
38+
int byteCount = encoding.GetBytes(strPtr, str.Length, (byte*)Ptr, maxBytesCount);
39+
((byte*)Ptr)[byteCount] = 0;
40+
Length = (nuint)byteCount;
3941
}
4042
catch
4143
{
4244
Marshal.FreeHGlobal(Ptr);
4345
Ptr = IntPtr.Zero;
46+
Length = UIntPtr.Zero;
4447
}
4548
}
4649
}

0 commit comments

Comments
 (0)