Skip to content

Commit 70db583

Browse files
bollhalsmichaelklishin
authored andcommitted
throw ArgumentOutOfRangeException when table key is too long (> 255)
1 parent de0f449 commit 70db583

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,13 @@ public static int WriteShortstr(Memory<byte> memory, string val)
432432
if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
433433
{
434434
int bytesWritten = Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset + 1);
435-
memory.Span[0] = (byte)bytesWritten;
436-
return bytesWritten + 1;
435+
if (bytesWritten <= byte.MaxValue)
436+
{
437+
memory.Span[0] = (byte)bytesWritten;
438+
return bytesWritten + 1;
439+
}
440+
441+
throw new ArgumentOutOfRangeException(nameof(val), val, "Value exceeds the maximum allowed length of 255 bytes.");
437442
}
438443

439444
throw new WireFormattingException("Unable to get array segment from memory.");

projects/Unit/TestFieldTableFormatting.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3939
//---------------------------------------------------------------------------
4040

41+
using System;
4142
using System.Collections;
4243
using System.Text;
4344

@@ -127,6 +128,20 @@ public void TestTableEncoding_x()
127128
});
128129
}
129130

131+
[Test]
132+
public void TestTableEncoding_LongKey()
133+
{
134+
const int TooLarge = 256;
135+
Hashtable t = new Hashtable
136+
{
137+
[new string('A', TooLarge)] = null
138+
};
139+
int bytesNeeded = WireFormatting.GetTableByteCount(t);
140+
byte[] bytes = new byte[bytesNeeded];
141+
142+
Assert.Throws<ArgumentOutOfRangeException>(() => WireFormatting.WriteTable(bytes, t));
143+
}
144+
130145
[Test]
131146
public void TestQpidJmsTypes()
132147
{

0 commit comments

Comments
 (0)