SDLNet_TCP_Recv() returns... null socket?

Hello everyone!

Take a look:-------------------------
// Receive player’s name
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 know I close the socket if result is less than 0,
but if the closing function executes then it must return -1 also, which
means the next part of that function shouldn’t execute…

This is in a different thread, but nothing happens on the other (main)
thread, it just sits and waits for someone to connect, so that can’t be
affecting the output of this thread function.

Any reason why Recv() should null my socket?

Thanks!

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.

UliOn 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.

Ulrich Eckhardt wrote:
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.
Yes, I contemplated that possibility, but the buffer is 16 bytes… :confused:

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’.
I’ll do that. :wink:

Also don’t forget to

NUL-terminate the string.

Uli

Any more ideas? This is weird… Thanks!

Any multithreading going on? Tossin it out there just in case…> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of L-28C
Sent: Sunday, September 09, 2007 4:36 PM
To: sdl at libsdl.org
Subject: Re: [SDL] SDLNet_TCP_Recv() returns… null socket?

Ulrich Eckhardt wrote:
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.
Yes, I contemplated that possibility, but the buffer is 16 bytes… :confused:

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’.
I’ll do that. :wink:

Also don’t forget to

NUL-terminate the string.

Uli

Any more ideas? This is weird… Thanks!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Alan Wolfe wrote:

Any multithreading going on? Tossin it out there just in case…

Uh… Yes? o_O

I fixed it, I can’t remember exactly how I did it (lol) but I doubt it
had anything to do with threads… :stuck_out_tongue:

I was just thinking that perhaps another thread was changing the value of
your variable.> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Leo M. Cabrera
Sent: Tuesday, September 11, 2007 11:53 AM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] SDLNet_TCP_Recv() returns… null socket?

Alan Wolfe wrote:

Any multithreading going on? Tossin it out there just in case…

Uh… Yes? o_O

I fixed it, I can’t remember exactly how I did it (lol) but I doubt it
had anything to do with threads… :stuck_out_tongue:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org