[SDL] SDLNet_TCP_Recv() returns... null socket?
Ulrich Eckhardt
doomster at knuut.de
Sun Sep 9 01:23:05 PDT 2007
On Saturday 08 September 2007 04:55:20 L-28C wrote:
> result = SDLNet_TCP_Recv(me->Socket, me->Name, 16);
> if (result < 0)
> {
> fprintf(stderr, "Couldn't receive player's name: %s.\r\nClosing
> connection", SDLNet_GetError());
> SDLNet_TCP_Close(me->Socket);
> return -1;
> }
> -------------------------
>
> I used GDB to find out that, before that snippet, me->Socket is valid,
> but after it's null.
I bet that you have a buffer overrun here. The receive function does
definitely not modify the value of the first argument passed to it, because
it can't. However, you are passing a related pointer (me->Name) to it, and if
that buffer is too small, it will happily overwrite anything related there.
Also, I wouldn't hard-code the size 16 there, because it means that you have
to keep several places where this constant is used in sync. Use a constant
with a name, or maybe a macro, or use 'sizeof me->Name'. Also don't forget to
NUL-terminate the string.
Uli
More information about the SDL
mailing list