Journal ryanr's Journal: Bug Puzzle #1 6
#include <stdio.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
int i = 0;
struct in_addr *inp;
int result;
if (argc < 2)
{
exit (1);
}
result = inet_aton (argv[1], inp);
printf ("%x\n", *inp);
}
[root@scutter]# gcc bug.c
[root@scutter]# ./a.out 10.1.1.1
Segmentation fault
wtf?
(I know wtf actually, the point is to give people some entertainment by giving them a programming error to solve, in the spirit of the pclint ads.)
Compiled on Red Hat 8.0 if it's important to someone, but the error should be fairly platform independent. :)
Post answers or questions below.
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
int i = 0;
struct in_addr *inp;
int result;
if (argc < 2)
{
exit (1);
}
result = inet_aton (argv[1], inp);
printf ("%x\n", *inp);
}
[root@scutter]# gcc bug.c
[root@scutter]#
Segmentation fault
wtf?
(I know wtf actually, the point is to give people some entertainment by giving them a programming error to solve, in the spirit of the pclint ads.)
Compiled on Red Hat 8.0 if it's important to someone, but the error should be fairly platform independent.
Post answers or questions below.
Uninitialized variable (Score:2)
Re:Uninitialized variable (Score:2)
Bonus question for advanced Bug Puzzlers:
#include <stdio.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
struct in_addr *inp;
int result;
if (argc < 2)
{
exit (1);
}
result = inet_aton (argv[1], inp
Re: (Score:1)
Re:Uninitialized variable (Score:2)
http://www.die.net/doc/linux/man/man3/inet_aton.3 . html [die.net]
Interesting that yours didn't crash with the first example like mine did... I'll have to be more careful with my examples.
To get the answer I was going for, you'd probably have to dig around with a debugger in the stack to show why it sometimes mysteriously works.
Re:Uninitialized variable (Score:1)
Answer (Score:2)
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
int i = 0;
struct in_addr inp;
int result;
if (argc < 2)
{
exit (1);
}
result = inet_aton (argv[1], &inp);
printf ("%x\n", inp);
}
As Krelnik was the first to point out, the original bug was that I was using an uninitialized pointer. The inet_aton function takes a pointer to a stucture (which is reall