This is the mail archive of the libc-help@sourceware.org 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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, May 12, 2017 at 11:56:40AM +0200, alleswirdbesser@posteo.de 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="201.23.23.44";
  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);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlkVllQACgkQBcgs9XrR2ka+lACfVEYVreVuqZN1MBgzBIRdJPO1
tDIAnA0gS/lO3IQZLxmY+7Z7EYn3e54F
=WN35
-----END PGP SIGNATURE-----


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