Skip to content

Commit 3b52168

Browse files
committed
Check bounds.
1 parent f0ef529 commit 3b52168

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ internal CString(string? str)
3535
{
3636
try
3737
{
38-
Length = (nuint)encoding.GetBytes(strPtr, str.Length, (byte*)Ptr, maxBytesCount);
38+
int bytesWritten = (nuint)encoding.GetBytes(strPtr, str.Length, (byte*)Ptr, maxBytesCount);
39+
if (bytesWritten < 0 || bytesWritten > maxBytesCount)
40+
{
41+
Marshal.FreeHGlobal(Ptr);
42+
Ptr = IntPtr.Zero;
43+
Length = 0;
44+
return;
45+
}
46+
47+
Length = (nuint)bytesWritten
3948
*((byte*)Ptr + Length) = 0; // Add null terminator
4049
}
4150
catch
4251
{
4352
Marshal.FreeHGlobal(Ptr);
4453
Ptr = IntPtr.Zero;
54+
Length = 0;
4555
}
4656
}
4757
}

0 commit comments

Comments
 (0)