Thread View: comp.sys.next.programmer
4 messages
4 total messages
Started by John Jay Feiler
Wed, 26 Aug 1992 04:32
"bind"ing sockets, -ObjC, and memset....A puzzle
Author: John Jay Feiler
Date: Wed, 26 Aug 1992 04:32
Date: Wed, 26 Aug 1992 04:32
86 lines
2750 bytes
2750 bytes
I had a strange problem today, and I thought I'd throw it out here to see if
anyone had an explanation for it. (at least one better than mine)
Background:
I'm doing some heterogenous ipc using internet sockets, so I wrote a small
program to test my socket implementation, basically copied out of one of the
Nutshell books. Worked great. Took the same code, and put in inside a
method of an ObjC object. No dice. The call to bind failed, regardless of
what port I gave it. Hmmmm..., says I, what could be the problem? Somehow,
I got it down to the compiler flag -ObjC making the differnece. The
following (seriously incomplete) code, without the call to memset(&sin,...),
will fail to bind correctly if compiled with -ObjC, but works fine without
the -ObjC flag.
-------foo.m------------
#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <netdb.h>
#import <stdio.h>
#import <libc.h>
extern int errno;
int main()
{
char hostname[64];
register int s;
struct hostent *hp;
struct sockaddr_in sin;
int i;
gethostname(hostname,sizeof(hostname));
if((hp = gethostbyname(hostname)) == NULL)
{
fprintf(stderr,"%s: unknown host.\n",hostname);
exit (1);
}
if((s = socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("server: socket");
exit(1);
}
/********************
if this memset is not performed, bind will likely fail
with errno 49.
could not bind to address. Don't ask me why, It just
does......
********************/
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
/***********
bind the address to the socket
let's try all the ports we can think of, until we find one
we should get #1024, because 1-1023 are reserved
***********/
for(i=1;i<30000;i++)
{
sin.sin_port = htons(i);
if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0)
printf("bound to port %d\n",i);
}
close(s);
exit(0);
}
---------------------------
Now, it seems to me that bind shouldn't be looking at anything else in the
sockaddr structure, other than the fields I'm setting, but somehow the memset
call solves the problem. Any ideas as to what might be causing things to
fail without the memset and with the -ObjC flag?
Puzzled,
John
Re: "bind"ing sockets, -ObjC, and memset....A puzzle
Author: simsong Simson L. Garfinkel
Date: Fri, 28 Aug 1992 19:02
Date: Fri, 28 Aug 1992 19:02
44 lines
1236 bytes
1236 bytes
Apparently, when you include the -ObjC flag, all stack variables are
initialized to 0.
Here's a simple test program:
int foo(n)
{
int a,b;
char f[2342];
int c;
printf("a=%d b=%d c=%d\n",a,b,c);
a = n;
c = n;
}
main()
{
int a,b,c;
foo(0);
foo(2);
}
And here is the output:
altos> cc x.c
altos> a.out
a=84032826 b=212 c=0
a=212 b=57628 c=0
altos> cc -ObjC x.c
altos> a.out
a=0 b=0 c=0
a=0 b=0 c=0
altos> %
(ps: Other variables in the sin structure are used; it's always a good idea to
initialize things to 0.)
--
................................................................simson
Re: "bind"ing sockets, -ObjC, and memset....A puzzle
Author: Daryll Strauss
Date: Mon, 31 Aug 1992 16:47
Date: Mon, 31 Aug 1992 16:47
12 lines
1041 bytes
1041 bytes
In article <1992Aug28.190230.2670@next.cambridge.ma.us>, simsong (Simson L. Garfinkel) writes: |> Apparently, when you include the -ObjC flag, all stack variables are |> initialized to 0. |> It would be a really bad idea to rely on that "feature." You must clear the sockaddr structure to use it. The C spec says that stack variables have unknown values. I've need heard anything different in for ObjC. So relying on the compiler to initialize variables for you is a disaster waiting to happen. - |Daryll
Re: "bind"ing sockets, -ObjC, and memset....A puzzle
Author: Moises Oliveira
Date: Mon, 31 Aug 1992 19:22
Date: Mon, 31 Aug 1992 19:22
50 lines
2014 bytes
2014 bytes
In article <17tieoINNsr5@news.aero.org> strauss@Aero.org (Daryll Strauss) writes:
>In article <1992Aug28.190230.2670@next.cambridge.ma.us>, simsong (Simson L. Garfinkel) writes:
>|> Apparently, when you include the -ObjC flag, all stack variables are
>|> initialized to 0.
>|>
>
>It would be a really bad idea to rely on that "feature." You must clear
>the sockaddr structure to use it. The C spec says that stack variables
>have unknown values. I've need heard anything different in for ObjC. So
>relying on the compiler to initialize variables for you is a disaster
>waiting to happen.
It's especially a bad idea seeing as how it's plain out
untrue! The ObjC flag to the linker DOES NOT clear stack
variables to 0. When you pass ObjC to the linker, it pulls
in a bunch of support for ObjC. Because of this, in an
itsy-bitsy tiny example program like the one posted, a
large portion of the stack will end up filled with 0
values.
But this will only remain true for the first function
call. To see what I mean, change the program a bit:
int foo(n)
{
int a,b;
int c;
printf("a=%d b=%d c=%d\n",a,b,c);
a = n;
c = n;
}
main()
{
int a,b,c;
foo(17);
foo(13);
foo(11);
foo(7);
foo(5);
foo(3);
foo(2);
foo(1);
}
The sample program and output that Simson Garfinkel
posted works by mere coincidence.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads