zeromq / netmq

A 100% native C# implementation of ZeroMQ for .NET
Other
2.95k stars 744 forks source link

Publisher crashes when curve encryption is enabled and SubscribeToAnyTopic is used in the subscriber socket #910

Closed dxdjgl closed 4 years ago

dxdjgl commented 4 years ago

Environment

NetMQ Version:    4.0.1.2-pre
Operating System: Windows 10
.NET Version:     4.8

Expected behaviour

Actual behaviour

Publisher crashes when subscriber is connected

Steps to reproduce the behaviour

Run the code snippets in the attached file, it will crash when ever SubscribeToAnyTopic is used, and it will work when subscribing to a specific topic. NetMq.zip

dxdjgl commented 4 years ago

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: offset at NetMQ.Msg.Slice(Int32 offset) at NetMQ.Core.Patterns.XPub.XReadActivated(Pipe pipe) at NetMQ.Core.Patterns.XPub.XAttachPipe(Pipe pipe, Boolean icanhasall) at NetMQ.Core.Patterns.Pub.XAttachPipe(Pipe pipe, Boolean icanhasall) at NetMQ.Core.SocketBase.ProcessBind(Pipe pipe) at NetMQ.Core.ZObject.ProcessCommand(Command cmd) at NetMQ.Core.SocketBase.ProcessCommands(Int32 timeout, Boolean throttle) at NetMQ.Core.SocketBase.TrySend(Msg& msg, TimeSpan timeout, Boolean more) at NetMQ.NetMQSocket.TrySend(Msg& msg, TimeSpan timeout, Boolean more) at NetMQ.OutgoingSocketExtensions.Send(IOutgoingSocket socket, Msg& msg, Boolean more) at NetMQ.OutgoingSocketExtensions.SendFrame(IOutgoingSocket socket, Byte[] data, Int32 length, Boolean more) at NetMQ.OutgoingSocketExtensions.SendMultipartMessage(IOutgoingSocket socket, NetMQMessage message) at curveServer.Program.ServerThread(Object obj) in d:\Repo\NetMq\curveServer\Program.cs:line 44 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart(Object obj)

dxdjgl commented 4 years ago

By looking at the code I believe the bug was introduced with this commit, 8d1aee6 "Problem: we have compilation warnings" Previously the code would handle if the size became 0, however now the code blows up inside the splice method. As an experiment I have tried to change the code inside XPub.cs from

                        ? m_subscriptions.Remove(sub.Slice(1), pipe)
                        : m_subscriptions.Add(sub.Slice(1), pipe);

to ? m_subscriptions.Remove(size == 1 ? new Span(): sub.Slice(1), pipe) : m_subscriptions.Add(size == 1 ? new Span() : sub.Slice(1), pipe); Now the code is no longer crashing, I believe XSub.cs has a similar issue

dxdjgl commented 4 years ago

Fix confirmed in 4.0.1.3