Skip to content

Commit

Permalink
Merge pull request #908 from tmasternak/master
Browse files Browse the repository at this point in the history
More descriptive exception in WriteShortstr

(cherry picked from commit 19e30bf)
  • Loading branch information
michaelklishin committed Jul 10, 2020
1 parent 49d60dc commit 6d09504
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions projects/RabbitMQ.Client/client/impl/WireFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,17 @@ public static unsafe int WriteShortstr(Span<byte> span, string val)
fixed (char* chars = val)
fixed (byte* bytes = &span.Slice(1).GetPinnableReference())
{
int bytesWritten = Encoding.UTF8.GetBytes(chars, val.Length, bytes, maxLength);
span[0] = (byte)bytesWritten;
return bytesWritten + 1;
try
{
int bytesWritten = Encoding.UTF8.GetBytes(chars, val.Length, bytes, maxLength);
span[0] = (byte)bytesWritten;
return bytesWritten + 1;
}
catch (ArgumentException)
{
throw new ArgumentOutOfRangeException(nameof(val), val, $"Value exceeds the maximum allowed length of {maxLength} bytes.");
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion projects/Unit/TestFieldTableFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ [new string('A', TooLarge)] = null
int bytesNeeded = WireFormatting.GetTableByteCount(t);
byte[] bytes = new byte[bytesNeeded];

Assert.Throws<ArgumentException>(() => WireFormatting.WriteTable(bytes, t));
Assert.Throws<ArgumentOutOfRangeException>(() => WireFormatting.WriteTable(bytes, t));
}

[Test]
Expand Down

0 comments on commit 6d09504

Please sign in to comment.