0

to all the specialists here ...

i've much trouble with a selfwritten readline method in c++ on linux. From time to time it throws an exception, SEGMATATION FAULT. I knew that it seems the readline writes to not allowed memory, it happens when the line from the tcp stream is broken. Can someone help me to correct the readline to make it more safe but it should not be much slower i need the performance in reading lines splitted at \r\n from the stream. The other possibility is to correct the readline to not throw SEG FAULT or able to catch SEG FAULT without killing the Application (like try catch in windows).

Here is the code of the Readline :

int readLineChatPri(std::string* line, int sock)
{
    static std::string buffer1;

    std::string::iterator pos;

    int n = 0;

    try
    {
        char buf[MAXRECV];

        while ((pos = find(buffer1.begin(), buffer1.end(), '\n')) == buffer1.end())
        {
            n = read(sock, buf, MAXRECV - 1);
            if (n == 0)
                return n;
            if (n == -1)
            {
                *line = buffer1;
                buffer1 = "";
                return n; // Error Handling
            }

            buf [n] = 0;
            buffer1 += buf;
        }

        *line = string(buffer1.begin(), pos); // pos + 1 with \r\n pos with \n pos -1 without \r\n
        buffer1 = string(pos + 1, buffer1.end());

        return n;
    }
    catch (exception& e)
    {
        logErr.WriteLogEntry("Error in <readLineChatPri 1000> : " + boost::lexical_cast<string>(e.what()));
        return -1;
    }
}

Hope someone can help me if you need further informations i will provide them.

Thanx Sascha

Sascha B
  • 1
  • 1
  • 1
    Have you tried running it in a debugger so you can see exactly where the problem is and what the conditions are at the time of the crash? – Retired Ninja Dec 03 '13 at 05:00
  • Have added debug Informations – Sascha B Dec 03 '13 at 19:39
  • Why did you post the code of `readLineChatPri()` when your program faults in `readLineChatSec()`? – Armali Sep 29 '16 at 10:58
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) - also check out https://rr-project.org/ – Jesper Juhl Apr 21 '23 at 02:52

0 Answers0