mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-11-27 10:00:57 +00:00
Guard serial reads with timeout-based termination and cap
This commit is contained in:
parent
9f4219e971
commit
4489c31509
|
|
@ -212,7 +212,7 @@ namespace WelsonJS.Launcher
|
||||||
using (var stream = new MemoryStream())
|
using (var stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
var buffer = new byte[bufferSize];
|
var buffer = new byte[bufferSize];
|
||||||
|
const int MaxResponseBytes = 1 * 1024 * 1024; // 1 MiB safety cap
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -220,18 +220,10 @@ namespace WelsonJS.Launcher
|
||||||
int read = await Task.Run(() => port.Read(buffer, 0, buffer.Length), token).ConfigureAwait(false);
|
int read = await Task.Run(() => port.Read(buffer, 0, buffer.Length), token).ConfigureAwait(false);
|
||||||
if (read > 0)
|
if (read > 0)
|
||||||
{
|
{
|
||||||
|
if (stream.Length + read > MaxResponseBytes)
|
||||||
|
throw new InvalidOperationException("Serial response exceeded maximum allowed size.");
|
||||||
stream.Write(buffer, 0, read);
|
stream.Write(buffer, 0, read);
|
||||||
if (port.BytesToRead == 0)
|
continue; // keep reading until idle timeout
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (port.BytesToRead == 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (TimeoutException)
|
catch (TimeoutException)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user