Skip to content

throw ArgumentOutOfRangeException when table key is too long (> 255) #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions projects/RabbitMQ.Client/client/impl/WireFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,13 @@ public static int WriteShortstr(Memory<byte> memory, string val)
if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
{
int bytesWritten = Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset + 1);
memory.Span[0] = (byte)bytesWritten;
return bytesWritten + 1;
if (bytesWritten <= byte.MaxValue)
{
memory.Span[0] = (byte)bytesWritten;
return bytesWritten + 1;
}

throw new ArgumentOutOfRangeException(nameof(val), val, "Value exceeds the maximum allowed length of 255 bytes.");
}

throw new WireFormattingException("Unable to get array segment from memory.");
Expand Down
15 changes: 15 additions & 0 deletions projects/Unit/TestFieldTableFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;

Expand Down Expand Up @@ -127,6 +128,20 @@ public void TestTableEncoding_x()
});
}

[Test]
public void TestTableEncoding_LongKey()
{
const int TooLarge = 256;
Hashtable t = new Hashtable
{
[new string('A', TooLarge)] = null
};
int bytesNeeded = WireFormatting.GetTableByteCount(t);
byte[] bytes = new byte[bytesNeeded];

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

[Test]
public void TestQpidJmsTypes()
{
Expand Down