Thread: send( accessing beyond buffer?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    send( accessing beyond buffer?

    I've been working on a game.
    One of the things the client-side code has to do is send the name of the player to the server. If I type anything that's more than 3 characters for the name ("Dude" for example), I get error box from Windows saying "Run-Time Check Failure #2 - Stack around the variable 'buf' was corrupted."
    I did a little research, and this error only happens when an array is getting accessed outside of it's space.
    So my question is, why is send( trying to access beyond 'buf'?

    Code:
    int SockSend(SOCKET *tsock, char* buf, int len)
    {
    	int nError;
    
    	nError = send(*tsock, buf, len, 0); // Error triggers here; does not stop execution, though
    	if(nError == SOCKET_ERROR)
    	{
    		printf("SockSend: send( failed: %d\n", WSAGetLastError());
    		closesocket(*tsock);
    		WSACleanup();
    		return 0;
    	}
    
    	return nError;
    }
    Where 'buf' would contain "S04Dude" and len is 7.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How is buf defined? Does it, in fact, have room for seven characters?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    The calling code is
    Code:
    SockSend(&f.psock, tbuf, strlen(tbuf))
    where tbuf is a char[85].

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That seems reasonable. You could check, by printing out before the send call, whether len is correct (if tbuf is not null-terminated, strlen(tbuf) could be just about anything).

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    tbuf is null-terminated and len is 7.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is this a full crash? Does data reach the other side? Is it possible that the error message is from the receiver? (If you don't have enough room on the other side, then you would get that message from trashing that piece of memory.)

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Ooh, looks like the error was coming from the receiving side. Didn't think of that.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print out a buffer
    By SwarfEye in forum C Programming
    Replies: 4
    Last Post: 09-08-2006, 09:32 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. Socket or send() problem?
    By Achy in forum C Programming
    Replies: 5
    Last Post: 06-09-2006, 01:09 PM
  4. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM