Skip to content

Commit 40504cd

Browse files
authored
Fix issue where message is assumed to contain correct length byte (leading to process exit) (#1050)
Do not assume that message contains correct length to prevent calling GetString with invalid parameters.
1 parent 40631c0 commit 40504cd

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/NetMQ.Tests/MechanismTests.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Text;
2+
using NetMQ.Core.Mechanisms;
3+
using Xunit;
4+
5+
namespace NetMQ.Tests
6+
{
7+
public class MechanismTests
8+
{
9+
private Msg CreateMsg(string data, int lengthDiff)
10+
{
11+
Assert.NotNull(data);
12+
Assert.True(data.Length > 1);
13+
var length = data.Length + lengthDiff;
14+
Assert.True(length > 0);
15+
Assert.True(length < byte.MaxValue - 1);
16+
17+
var msg = new Msg();
18+
msg.InitGC(new byte[data.Length + 1], 0, data.Length + 1);
19+
msg.SetFlags(MsgFlags.Command);
20+
msg.Put((byte)length);
21+
msg.Put(Encoding.ASCII, data, 1);
22+
return msg;
23+
}
24+
25+
[Fact]
26+
public void IsCommandShouldReturnTrueForValidCommand()
27+
{
28+
var mechanism = new NullMechanism(null!, null!);
29+
var msg = CreateMsg("READY", 0);
30+
Assert.True(mechanism.IsCommand("READY", ref msg));
31+
}
32+
33+
[Fact]
34+
public void IsCommandShouldReturnFalseForInvalidCommand()
35+
{
36+
var mechanism = new NullMechanism(null!, null!);
37+
var msg = CreateMsg("READY", -1);
38+
Assert.False(mechanism.IsCommand("READY", ref msg));
39+
msg = CreateMsg("READY", 1);
40+
Assert.False(mechanism.IsCommand("READY", ref msg));
41+
// this test case would fail due to an exception being throw (in 4.0.1.10 and prior)
42+
msg = CreateMsg("READY", 2);
43+
Assert.False(mechanism.IsCommand("READY", ref msg));
44+
}
45+
46+
// this test was used to validate the behavior prior to changing the validation logic in Mechanism.IsCommand
47+
// [Fact]
48+
// public void IsCommandShouldThrowWhenLengthByteExceedsSize()
49+
// {
50+
// var mechanism = new NullMechanism(null, null);
51+
// var msg = CreateMsg("READY", 2);
52+
// Assert.Throws<ArgumentOutOfRangeException>(() => mechanism.IsCommand("READY", ref msg));
53+
// }
54+
}
55+
}

src/NetMQ/Core/Mechanisms/Mechanism.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,13 @@ protected bool CheckBasicCommandStructure(ref Msg msg)
348348
return true;
349349
}
350350

351-
protected bool IsCommand(string command, ref Msg msg)
351+
internal protected bool IsCommand(string command, ref Msg msg)
352352
{
353-
if (msg.Size >= command.Length + 1)
353+
if (msg.Size >= command.Length + 1 && msg[0] == command.Length)
354354
{
355-
string msgCommand = msg.GetString(Encoding.ASCII, 1, msg[0]);
356-
return msgCommand == command && msg[0] == command.Length;
355+
return msg.GetString(Encoding.ASCII, 1, msg[0]) == command;
357356
}
358-
359357
return false;
360358
}
361359
}
362-
}
360+
}

0 commit comments

Comments
 (0)