0

I got a small problem in one of my scripts. I have a connection to a websocket and the messages are sent by server to my client. The format is json. I wrote a simple interpreter which is searching for the quotes.

The function header: ProcessEvent( string msg )

The function starts like this:

  size_t hasType = msg.find("type");
  size_t length = msg.size();
    /* type */
    string date, name, message, color, room, roomname;
    int pos = msg.find("type");
    if(pos!=std::string::npos) {
      msg = msg.substr(pos+7);
      int pos2 = msg.find_first_of("\"");
      string type = msg.substr(0, pos2);
      msg = msg.substr(pos2);

The basic problem is on that part:

        /* message */
        pos = msg.find("message");
        if(pos!=std::string::npos) {
          msg = msg.substr(pos+10);
          pos2 = msg.find_first_of("\"");
cout << msg << endl;
CONSOLE_Print("size: "+UTIL_ToString(msg.size())+", pos2: "+UTIL_ToString(pos2));
          message = msg.substr(0, pos2);
          msg = msg.substr(pos2);
        } else { message = "empty"; }

As you can see i tried already to debug into this problem. The problem only occurs on messages with a huge length (this is the only thing coming up from data). The return of the second debug is this:

size: 949, pos2: -1

The problem is now, that the example input into the client is this:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer

This are 300 chars Lorem Ipsum. The text if the debug print isn't 300 chars anymore:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate

That is also obviously the reason why it couldnt find my trailing quote for this message. On the first text there isnt a trailing quote also, i just used to copy the text from my input, this isnt the normal format. The main problem is not the missing quote, its that the string is stripped without a reason. The whole exception is this:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Aborted

What is the reason that the string is cut like this?

Michael Schneider
  • 496
  • 1
  • 9
  • 27
  • You should check `pos2!=std::string::npos` – quantdev Aug 18 '14 at 18:08
  • Well, you are right. But i know exactly what im sending, so atm I didn't have done it. The main problem is that my string is stripped... – Michael Schneider Aug 18 '14 at 18:09
  • You don't mention how you're downloading the data - have you made sure that the entire response has been received so you're not working with partial data? – molbdnilo Aug 18 '14 at 18:15
  • 1
    string::find() returns a size_t (unsigned) and not an int. And by the way, pos2=-1 means that it you got npos returned. The stripping of your message is not in the code above. – Christophe Aug 18 '14 at 18:18

0 Answers0