Skip to content

Commit 6d09504

Browse files
Merge pull request #908 from tmasternak/master
More descriptive exception in WriteShortstr (cherry picked from commit 19e30bf)
1 parent 49d60dc commit 6d09504

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

projects/RabbitMQ.Client/client/impl/WireFormatting.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,17 @@ public static unsafe int WriteShortstr(Span<byte> span, string val)
435435
fixed (char* chars = val)
436436
fixed (byte* bytes = &span.Slice(1).GetPinnableReference())
437437
{
438-
int bytesWritten = Encoding.UTF8.GetBytes(chars, val.Length, bytes, maxLength);
439-
span[0] = (byte)bytesWritten;
440-
return bytesWritten + 1;
438+
try
439+
{
440+
int bytesWritten = Encoding.UTF8.GetBytes(chars, val.Length, bytes, maxLength);
441+
span[0] = (byte)bytesWritten;
442+
return bytesWritten + 1;
443+
}
444+
catch (ArgumentException)
445+
{
446+
throw new ArgumentOutOfRangeException(nameof(val), val, $"Value exceeds the maximum allowed length of {maxLength} bytes.");
447+
}
448+
441449
}
442450
}
443451

projects/Unit/TestFieldTableFormatting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ [new string('A', TooLarge)] = null
139139
int bytesNeeded = WireFormatting.GetTableByteCount(t);
140140
byte[] bytes = new byte[bytesNeeded];
141141

142-
Assert.Throws<ArgumentException>(() => WireFormatting.WriteTable(bytes, t));
142+
Assert.Throws<ArgumentOutOfRangeException>(() => WireFormatting.WriteTable(bytes, t));
143143
}
144144

145145
[Test]

0 commit comments

Comments
 (0)