WebsocketStompKit icon indicating copy to clipboard operation
WebsocketStompKit copied to clipboard

Crashing when recieving \n in STOMPFrameFromData:(NSData *)data

Open mixdux opened this issue 9 years ago • 2 comments

Hello guys, I just made my first connection to websockets using your library! I just wanted to give you a heads up since the app crashes when server sends \n as a msg. I made a hotfix by adding an if clause in the aforementioned method which checks for it. App crashes exactly at

NSString *command = [[contents objectAtIndex:0] copy];

It may be a server specific, non standard msg, but I just wanted to give you a heads up, since contents array in this case doesn't have any objects (while loop removes them) and app breaks beause of "index 0 beyond bounds for empty array". Good luck and thank you for making this great lib!

mixdux avatar Nov 07 '15 21:11 mixdux

Same here. I'm using Spring Websocket + STOMP + Rabbitmq. Rabbitmq sends as heartbeat "\n" (LF) and my app crashes.

At no point a "pong" is expected. Here is my solution:

- (void)websocket:(JFRWebSocket*)socket didReceiveData:(NSData*)data {
    serverActivity = CFAbsoluteTimeGetCurrent();

    if ([[NSData dataWithBytes:"\x0A" length:1] isEqual:data]) {
        LogDebug(@">>> PONG");
        return;
    }

    STOMPFrame *frame = [STOMPFrame STOMPFrameFromData:data];
    [self receivedFrame:frame];
}

- (void)websocket:(JFRWebSocket*)socket didReceiveMessage:(NSString *)string {
    [self websocket:socket didReceiveData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}

Tomorrow I'll try to create a pull request, when you're d'accord. Thanks!

wollodev avatar Jan 03 '16 00:01 wollodev

+1: I can confirm this crash on incoming heartbeats.

@wollodev: One comment to the solution above. According to Stomp 1.2, "it is now possible to end frame lines with carriage return plus line feed instead of only line feed". Perhaps the solution needs to check both variants. Maybe even simplified to ignore all messages <= length 2?

EDIT:

from spec: "if the sender has no real STOMP frame to send, it MUST send an end-of-line (EOL)" and EOL is defined as EOL = [CR] LF

i.e. CR is optional, but is allowed!

apanloco avatar Jan 04 '16 15:01 apanloco