This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Re: inet_aton 0x0 in integer in structure

Hash: SHA1

On Fri, May 12, 2017 at 11:56:40AM +0200, wrote:
> It is valid ANSI C code and it makes sense. And it is a pointer,
> like in the header:
> extern int inet_aton (const char *__cp, struct in_addr *__inp) __THROW;
> The compiler accepted it as well.

No. Sorry to be a bit blunt, but I think you haven't understood C.
I'll quote your original code with line numbers. Below, I'll comment
a bit, referring to the numbers (I shortened the TABs to keep line
length at bay):

  1  int main ()
  2  {
  3    const char *name="";
  4    struct in_addr *ipaddr;
  5    int valide;
  6    valide = inet_aton (name, ipaddr);
  7    printf ("valide: %d. \nIP-Adresse: %d", valide, (*ipaddr).s_addr );
  8    return 0;
  9  }

At line 4, you *declare* a pointer to a struct in_addr. The effect of
this is that the compiler reserves some place for this *pointer*.
Since it is a stack (automatic) variable, its content is 0 (NULL).
There is "nothing" at NULL (usually it's set up to trip a segmentation
fault to catch mistakes like this).

At line 6 you let inet_aton() dereference your pointer ipaddr. This
should cause a segfault, because ipaddr is a NULL pointer. Note that
you're still lucky: your guardian angel here was the implicit
initialization to zero of automatic variables. Without that, the
memory pointed at by ipaddr might contain random data and your results
would have depended on the moon's phase.

On line 7 you dereference (again) the NULL pointer ipaddr. Same as
line 6 applies, but I don't think the program reaches that.

TIP: always use -Wall option to your compiler. Take warnings seriously.
If I compile your program, my trusty C compiler sticks my nose into
the problem:

  tomas@rasputin:~/tmp$ gcc -Wall -o foo foo.c
  foo.c: In function ‘main’:
  foo.c:11:10: warning: ‘ipaddr’ is used uninitialized in this function [-Wuninitialized]
     valide = inet_aton (name, ipaddr);

- -- tomás
Version: GnuPG v1.4.12 (GNU/Linux)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]