这个分段错误的原因是什么?

发布于 2024-11-08 22:26:47 字数 1320 浏览 5 评论 0原文

它并不总是发生,但当服务器应用程序运行相当长一段时间时就会​​发生。

原因是什么以及如何解决?

代码如下:

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));

核心转储:

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-11-15 22:26:47

鉴于 Host 和 Result 变量都指向合法的内存块,最可能的原因是 Host->h_addrNULL。如果 gethostbyname()gethostbyaddr() 返回的地址列表为空,就会出现这种情况。

我不知道这是怎么造成的(我的 OS X 系统上的文档暗示如果找不到地址,两个函数都应该返回 NULL)。但是,我会在调试器中检查 Host->h_addr_list[0] 进行确认。

编辑

调试信息的更新显示了问题所在:Host->h_addr为NULL。 h_addr 实际上是一个像这样的#define:

#define h_addr h_addr_list[0]

其中一个函数返回一个带有空地址列表的struct hostent

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.

梅倚清风 2024-11-15 22:26:47

也许源内存区域和目标内存区域重叠? IE

&Result->sin_addr >= Host->h_addr >= &Result->sin_addr + sizeof(Result->sin_addr)

Maybe, source and destination memory areas overlap? i.e.

&Result->sin_addr >= Host->h_addr >= &Result->sin_addr + sizeof(Result->sin_addr)
中二柚 2024-11-15 22:26:47

memcpy 失败。

这可能是由于
(1) sin_addr 和 h_addr 大小不同或
(2) sin_addr 是一个指针,并且您没有为其分配内存。

请提供结果的定义和初始化以获取更多信息。

The memcpy is failing.

This could be caused by
(1) sin_addr and h_addr are not the same sizes or
(2) sin_addr is a pointer and you have not malloc'd memory for it.

Please provide the definition and initialisation of Result for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文