Hi, I was trying to modify a function designed to split a string onto multiple lines and indent it. This function was written by someone else and when I came to check the end of the code I found the following:
Code:
    pBuf = strchr( buf, '\0' ) - 1;
    while( isspace( *pBuf ) )
    {
        *pBuf-- = '\0';
    }
    ++pBuf;
    *pBuf++ = '\n';
    *pBuf = '\r';

    return buf;
Now it appears to me that the code is attempting to look one character before the end of the string, then rewind back over any space characters found at the end of the string before adding a carriage return and a linefeed and returning the contents of the buffer.

What I don't understand is that if the while loop never evaluates to true it looks like the pointer gets increased again and the '\n' will be written over the '\0' and leave the string be left unterminated.

Is this correct? The reason I ask is that this code has been in use for a long time and I have never noticed any problems with strings passed to it!

Should I add *pBuf = '\0'; before the return statement?