这是什么分段故障原因是什么? [英] What's the reason of this segmentation fault?
问题描述
这并不总是发生,但是当服务器应用程序已经运行了相当一段时间不会发生。
是什么原因,我该如何解决这个问题?
code如下:
结构hostent *主机;
Result-> sin_family = AF_INET;
Result-> sin_port = htons((无符号短)端口);主机=的gethostbyname(主机名);如果(!主机)
{
unsigned long int类型地址= inet_addr(主机名);
如果(地址!= - 1)
HOST = gethostbyaddr(安培;地址,的sizeof(地址),AF_INET); 如果(!主机)
{
如果(错误!= ETIMEDOUT)
错误号= -1; / *使用h_errno * /
的printf(未知主机服务器[%S]。,主机名);
返回(0);
}
}的memcpy((字符*)及Result-> sin_addr,属于(的char *)&主机的GT; h_addr,sizeof的(Result-> sin_addr));
核心转储:
#0 0x0000000000401913在proxy_getaddr在proxy.c(主机名= 0x7ae30be0stackoverflow.com,端口= 80,结果= 0x7ae30bd0):529529的memcpy((字符*)及Result-> sin_addr,属于(的char *)&主机的GT; h_addr,sizeof的(Result-> sin_addr));
(GDB)P *主机
$ 4 = {h_name = 0xc4ee048,h_aliases = 0xc4ee030,h_addrtype = 2,长度h_length = 4,h_addr_list = 0xc4ee038}
(GDB)打印结果
$ 5 =(结构SOCKADDR_IN *)0x7ae30bd0
(GDB)的打印结果*
$ 6 = {sin_family = 2,sin_port = 20480,sin_addr = {s_addr = 0} =的sin_zero\\ 000 \\ 000 \\ 000 \\ 000 \\ 000 \\ 000 \\ 000}(GDB)p主机的> h_addr_list [0]
$ 1 =为0x0
(GDB)p主机的> h_addr_list
$ 2 =(字符**)0x1bd9d050
由于主机和结果变量都指向内存的合法块,最可能的原因是, HOST-> h_addr
是 NULL
。这将是情况下,如果通过返回的地址列表中的gethostbyname()
或 gethostbyaddr()
是空的。
我不知道怎么会引起(我的OS X系统上的文档意味着这两个函数应返回NULL,如果没有地址可以找到)。不过,我会检查主机的方式> h_addr_list [0]
在调试器中,以确认
修改
调试信息的更新显示问题出在哪里: HOST-> h_addr
为NULL。 h_addr其实就是这样的#定义:
的#define h_addr h_addr_list [0]
其中的一个功能返回一个结构hostent
与空地址列表。
It doesn't happen always,but will happen when the server application has been running for quite a while.
What's the reason and how can I fix it?
Code as follows:
struct hostent* Host;
Result->sin_family=AF_INET;
Result->sin_port=htons((unsigned short)Port);
Host=gethostbyname(HostName);
if(!Host)
{
unsigned long int addr=inet_addr(HostName);
if(addr!=-1)
Host=gethostbyaddr(&addr,sizeof(addr),AF_INET);
if(!Host)
{
if(errno!=ETIMEDOUT)
errno=-1; /* use h_errno */
printf("Unknown host for server [%s].", HostName);
return(0);
}
}
memcpy((char*)&Result->sin_addr,(char*)Host->h_addr,sizeof(Result->sin_addr));
core dump:
#0 0x0000000000401913 in proxy_getaddr (HostName=0x7ae30be0 "stackoverflow.com", Port=80, Result=0x7ae30bd0) at proxy.c:529
529 memcpy((char*)&Result->sin_addr,(char*)Host->h_addr,sizeof(Result->sin_addr));
(gdb) p *Host
$4 = {h_name = 0xc4ee048 "", h_aliases = 0xc4ee030, h_addrtype = 2, h_length = 4, h_addr_list = 0xc4ee038}
(gdb) print Result
$5 = (struct sockaddr_in *) 0x7ae30bd0
(gdb) print *Result
$6 = {sin_family = 2, sin_port = 20480, sin_addr = {s_addr = 0}, sin_zero = "\000\000\000\000\000\000\000"}
(gdb) p Host->h_addr_list[0]
$1 = 0x0
(gdb) p Host->h_addr_list
$2 = (char **) 0x1bd9d050
Given that the Host and Result variables both point to legitimate blocks of memory, the most likely cause is that Host->h_addr
is NULL
. This would be the case if the list of addresses returned by gethostbyname()
or gethostbyaddr()
were empty.
I don't know how that could be caused (the documentation on my OS X system implies that both functions should return NULL if no addresses can be found). However, I would check Host->h_addr_list[0]
in the debugger to confirm.
Edit
The update of the debug info shows where the problem is: Host->h_addr
is NULL. h_addr is actually a #define like this:
#define h_addr h_addr_list[0]
One of the functions is returning a struct hostent
with an empty address list.
这篇关于这是什么分段故障原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!