Skip to content

Commit

Permalink
Store "remaining" byte count before using it in loop
Browse files Browse the repository at this point in the history
Fixes bug where arrays that relied on remaining data size would only read half the available data, due to the changing value of "remaining" as more data was read.
  • Loading branch information
ethanmoffat committed May 29, 2024
1 parent a0d5a3c commit 24136f0
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ProtocolGenerator/Model/Protocol/ArrayInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override void GenerateDeserialize(GeneratorState state, IReadOnlyList<IPr
{
var delimited = _xmlArrayInstruction.Delimited.HasValue && _xmlArrayInstruction.Delimited.Value;

var storeRemaining = false;
string loopCondition;
if (!string.IsNullOrWhiteSpace(_xmlArrayInstruction.Length))
{
Expand All @@ -85,8 +86,9 @@ public override void GenerateDeserialize(GeneratorState state, IReadOnlyList<IPr
try
{
var typeSize = TypeInfo.CalculateByteSize();
loopCondition = typeSize > 1
? $"ndx < reader.Remaining / {typeSize}"
storeRemaining = typeSize > 1;
loopCondition = storeRemaining
? $"ndx < remainingFor{Name} / {typeSize}"
: "reader.Remaining > 0";
}
catch
Expand All @@ -99,6 +101,12 @@ public override void GenerateDeserialize(GeneratorState state, IReadOnlyList<IPr
loopCondition = "reader.Remaining > 0";
}

if (storeRemaining)
{
state.Text($"var remainingFor{Name} = reader.Remaining;", indented: true);
state.NewLine();
}

state.For("int ndx = 0", loopCondition, "ndx++");
state.BeginBlock();

Expand Down

0 comments on commit 24136f0

Please sign in to comment.