problem : strtok,strcpy,crashes when garbage strings are handled

This is a discussion on problem : strtok,strcpy,crashes when garbage strings are handled within the C++ Programming forums, part of the General Programming Boards category; strtok,strcpy,crashes when garbage strings are handled this is especially so in winsock applications in my application, i expect " value1^value2^value^&############" ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    problem : strtok,strcpy,crashes when garbage strings are handled

    strtok,strcpy,crashes when garbage strings are handled

    this is especially so in winsock applications
    in my application, i expect " value1^value2^value^&############"
    and use strtok to extract value 1,2 and 3
    and "&" indicates that there are no more values to be collected

    where #### = garbaage
    others can send a garbage long string like
    "##################################"

    and my application would crash

    is there anyway to tell strtok/strcpy to handle this?

    will a check on strlen help?

    please assist

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    more info

    Code:
    struct broken_msg
    {
    	void break_msg(char* full_msg)
    	{
    		char seps[] = "^";
    		char *token;
    		token = strtok( full_msg, seps ); <== CRASHES HERE
    		if(strlen(token) < 150)
    		{
    			strcpy(parm0,token);
    		}
    
    		if(  stricmp("&" ,token )   )
    		{
    			token = strtok( NULL, seps );
    			if(strlen(token) < 150)
    			{
    				strcpy(parm1,token);
    			}
    		}
    		if(  stricmp("&" ,token )   )
    		{
    			token = strtok( NULL, seps );
    			if(strlen(token) < 150)
    			{
    				strcpy(parm2,token);
    			}
    
    		}
    		if(  stricmp("&" ,token )   )
    		{
    			token = strtok( NULL, seps ); 
    			if(strlen(token) < 150)
    			{
    				strcpy(parm3,token);
    			}
    
    		}
    	}
    
    
    	char parm0[150];
    	char parm1[150];
    	char parm2[150];
    	char parm3[150];
    };
    Last edited by hanhao; 07-08-2005 at 12:11 AM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    socket functions just send series of bytes.
    The str functions always expect that their input is zero terminated.
    You need to either come up with something that zero terminates your arrays (keep track of the number of received bytes) or dont use the str functions.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > is there anyway to tell strtok/strcpy to handle this?
    Yes, you make sure that whatever is reading character data from the outside world makes proper strings out of the data by appending a \0 in the correct place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    Quote Originally Posted by Salem
    > is there anyway to tell strtok/strcpy to handle this?
    Yes, you make sure that whatever is reading character data from the outside world makes proper strings out of the data by appending a \0 in the correct place.
    how?
    strcat crashes when i tried to use it on the garbage string
    is there a workable way to add '\0' ?
    please advise

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If your "string" doesn't have a null on it, it's not a string. It's just an array of characters. It's up to you, if you plan on using any string functions, to make sure you actually have a string, and not just an array of characters. If you're just reading stuff in, you'd better keep track of how much is the valid string, and then manually go to that length + 1 and put a null there. (Assuming you have the space to do it.) If you can't understand that, go back and read Array Basics 101 for Newbies.


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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > is there a workable way to add '\0' ?
    Yes, most functions like recv() and read() return a COUNT of the number of bytes they copied.

    So simply put, you do this
    n = recv( buff, maxsize-1, fd ); // or whatever
    buff[n] = '\0'; // NOW it's a string you can use str... functions on
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    Quote Originally Posted by hanhao
    how?
    strcat crashes when i tried to use it on the garbage string
    is there a workable way to add '\0' ?
    please advise
    guys thank you for your answers

    i discovered that the only way to add '\0' at the end is to use Zeromemory() function on the buffer before recv()

    instead of using strcat()

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > i discovered that the only way to add '\0' at the end is to use Zeromemory() function
    Or use the return result of recv() like I suggested, which is rather more efficient than clearing the whole block of memory just to get a \0 in the right place.

    You also need to check the return result of recv() for other reasons as well, so it's no biggie IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  2. Delphi DLL Problem, C++ hates Delphi strings
    By Cogman in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2008, 07:32 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. adding strings hard problem...
    By qubit67 in forum C Programming
    Replies: 28
    Last Post: 04-22-2007, 02:02 AM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21