Skip to content

Commit 66c2e4b

Browse files
[sdk] Support split reads in OTEL_DIAGNOSTICS.json config file parser logic (open-telemetry#5884)
Co-authored-by: Piotr Kiełkowicz <[email protected]>
1 parent a55a5ac commit 66c2e4b

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/OpenTelemetry/Internal/SelfDiagnosticsConfigParser.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,21 @@ public bool TryGetConfiguration(
6666
this.configBuffer = buffer;
6767
}
6868

69-
// TODO: Fix CA2022 - Avoid inexact read with 'System.IO.FileStream.Read(byte[], int, int)'
70-
// Added _ = as a workaround to suppress the warning
71-
_ = file.Read(buffer, 0, buffer.Length);
72-
string configJson = Encoding.UTF8.GetString(buffer);
69+
int bytesRead = 0;
70+
int totalBytesRead = 0;
71+
72+
while (totalBytesRead < buffer.Length)
73+
{
74+
bytesRead = file.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead);
75+
if (bytesRead == 0)
76+
{
77+
break;
78+
}
79+
80+
totalBytesRead += bytesRead;
81+
}
82+
83+
string configJson = Encoding.UTF8.GetString(buffer, 0, totalBytesRead);
7384

7485
if (!TryParseLogDirectory(configJson, out logDirectory))
7586
{

test/OpenTelemetry.Tests/Internal/SelfDiagnosticsEventListenerTest.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,20 @@ public void SelfDiagnosticsEventListener_EmitEvent_OmitAsConfigured()
125125
using FileStream file = File.Open(LOGFILEPATH, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
126126
var buffer = new byte[256];
127127

128-
// Suppress CA2022 error: Avoid inexact read with 'System.IO.FileStream.Read(byte[], int, int)'
129-
_ = file.Read(buffer, 0, buffer.Length);
128+
int bytesRead = 0;
129+
int totalBytesRead = 0;
130+
131+
while (totalBytesRead < buffer.Length)
132+
{
133+
bytesRead = file.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead);
134+
if (bytesRead == 0)
135+
{
136+
break;
137+
}
138+
139+
totalBytesRead += bytesRead;
140+
}
141+
130142
Assert.Equal('\0', (char)buffer[0]);
131143
}
132144

@@ -259,9 +271,21 @@ private static void AssertFileOutput(string filePath, string eventMessage)
259271
using FileStream file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
260272
var buffer = new byte[256];
261273

262-
// Suppress CA2022 error: Avoid inexact read with 'System.IO.FileStream.Read(byte[], int, int)'
263-
_ = file.Read(buffer, 0, buffer.Length);
264-
string logLine = Encoding.UTF8.GetString(buffer);
274+
int bytesRead = 0;
275+
int totalBytesRead = 0;
276+
277+
while (totalBytesRead < buffer.Length)
278+
{
279+
bytesRead = file.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead);
280+
if (bytesRead == 0)
281+
{
282+
break;
283+
}
284+
285+
totalBytesRead += bytesRead;
286+
}
287+
288+
string logLine = Encoding.UTF8.GetString(buffer, 0, totalBytesRead);
265289
string logMessage = ParseLogMessage(logLine);
266290
Assert.StartsWith(eventMessage, logMessage);
267291
}

0 commit comments

Comments
 (0)