Thread: Question regarding pointer manipulation

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    13

    Question regarding pointer manipulation

    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?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep. If the string passed doesn't end in whitespace, you're screwed. Furthermore, if it ends in one whitespace, it will back up to the white space, place a \n there, move to the null, and place the \r there. Again, killing your null. So the only time this will ever work right, is if the string ends in two or more whitespace, excluding the null.

    No, you shouldn't add the mentioned line before the return. You should instead completely rewrite this. Basicly you have to rethink this so that you can handle the two cases mentioned. It's very possible for a string to be passed as described, which simply won't have room for you to add the "'\n\r".

    You need to redesign / replan this.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    Thanks for the help Quzah, I thought this was the case.

    In this case I don't think I have to worry about the size of the string as all of the calls to the function pass a buffer that has a significant amount of overhead over the maximum string length that can be used within that buffer, to allow plenty of room for additional formating to be added.

    I still cannot believe this has never caused a problem before as the buffer being passed is not padded in any way, I will fix this straight away.

    Thanks again for your help.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Depending on how the buffer is initialized, it could be set to nulls in the first place, which means everything beyond the null at the end of the string is also null, so you don't have the problem. However, if the function is used with a buffer that's not fully initialized to nulls first, you will have the problem.

    So, if you're sure the buffer will always have room, then sure, go ahead and add the null assignment right before the return.
    Code:
        *pBuf++ = '\n';
        *pBuf++ = '\r';
        *pBuf = '\0';
    
        return buf;
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    Thanks for replying so quickly to this! I have made the change so hopefully this should completely remove the possibility of an unterminated string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  5. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM