What's the reason of this segmentation fault?

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 = "00000000000000"} (gdb) p Host->h_addr_list[0] $1 = 0x0 (gdb) p Host->h_addr_list $2 = (char **) 0x1bd9d050

-------------Problems Reply------------

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.

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

&Result->sin_addr >= Host->h_addr >= &Result->sin_addr + sizeof(Result->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.

Category:c# Views:0 Time:2011-05-23
Tags: coredump

Related post

  • Why does strcpy trigger a segmentation fault with global variables? 2008-09-23

    So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of the "char*"s here causes a segfault */ void main() { char* path = "/temp"; char* temp; strcpy(temp, path); } This compiles, runs, and behaves as it looks. H

  • Why do I get a segmentation fault when writing to a string? 2008-10-02

    The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s", str); Tested with MSVC and GCC. --------------Solutions--------

  • Segmentation Fault in TCP communication 2008-11-21

    I'm getting a segmentation fault in the following C code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include

  • Weird ruby segmentation fault with DBI and MySQL 2009-01-12

    The following code gives a segmentation fault on the last line require 'rubygems' gem 'mysql' gem 'dbi' require 'dbi' require 'mysql' dsn = "DBI:Mysql:DATABASE:www.HOST.net" # redacted dbh = DBI.connect(dsn, "USERNAME", "PASSWORD") # redacted sth = d

  • Apache w/mod_rails segmentation fault 2009-01-19

    I am running Redmine on Apache 2 with mod_rails (passenger) 2.0.3 and Enterprise Ruby 1.8.6. Every so often I get a segfault from Apache when I try to login. Anyone know how I can debug this issue? I see something like this in Apache's error.log: [Mo

  • Line number of segmentation fault 2009-02-02

    Is there any gcc option I can set that will give me the line number of the segmentation fault? I know I can: Debug line by line Put printfs in the code to narrow down. Edits: bt / where on gdb give No stack. Helpful suggestion --------------Solutions

  • Try/Catch a segmentation fault on Linux 2009-02-05

    I have a Linux C++ application and I'd like to test an object pointer for validity before dereferencing it. However try/catch doesn't work for this on Linux because of the segmentation fault. How can this be done? --------------Solutions-------------

  • C program compiled with cygwin in Windows works, segmentation fault under Linux. Is cygwin GCC 'bad'? 2009-02-09

    For my Programming 102 class we are asked to deliver C code that compiles and runs under Linux. I don't have enough spare space on my hard drive to install Linux alongside Windows, and so I use cygwin to compile my programs. The most recent program I

  • long integer as array index in C gives segmentation fault 2009-03-16

    The following C Code gives a segmentation fault: #include <stdio.h> #include <stdint.h> int main(){ uint32_t *a; uint32_t idx=1233245613; a[idx]=1233; return 0; } How can I use uint32_t as index of an array in C? Or how can I use array li

  • How can I avoid a segmentation fault while resize a vector like this 2009-03-18

    I think it is a simple question for you....i am pretty new in c++..... So i have a vector defined like this: vector<vector<float> > big_vector; I read a file and initialized this vector, then the big_wector has about 200,000 elements in i

  • Why is my C++ code causing a segmentation fault well after using the read(-) function? 2009-03-24

    My application is suspending on a line of code that appears to have nothing wrong with it, however my IDE appears to be suspending on that line with the error: gdb/mi (24/03/09 13:36) (Exited. Signal 'SIGSEGV' received. Description: Segmentation faul

  • Why do I get a segmentation fault when calling a virtual method in this code? 2009-04-02

    I'm still learning C++; I was trying out how polymorphism works and I got a segmentation fault when calling a virtual method. (Note: I didn't mark the destructor as virtual, I was just trying out to see what happens.) Here's the code: #include <io

  • Segmentation fault in strcpy 2009-04-05

    consider the program below char str[5]; strcpy(str,"Hello12345678"); printf("%s",str); When run this program gives segmentation fault. But when strcpy is replaced with following, program runs fine. strcpy(str,"Hello1234567"); So question is it should

  • Why am I getting a segmentation fault with this code? 2009-04-16

    I am getting a segmentation fault while running this code. I can't work out why this is happening - can anyone see a possible reason? (I have already got and initialized the semaphore's shared memory.) My code: #include<stdlib.h> #include<sy

  • Ruby monitor segmentation fault 2009-04-18

    I followed the example from http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html and modified the code a bit: require 'monitor.rb' buf = [] buf.extend(MonitorMixin) empty_cond = buf.new_cond producer = Thread.start do # producer line = "prod

  • Segmentation Fault with pointer-to-pointer with C / Linux 2009-04-25

    In the following code I get a segmentation fault: Set *getpar() {...} char function(...) { Set **S; *S = getpar(); /* Segmentation Fault */ ... } But the bizarre thing is that with few changes there is no segmentation fault: Set *getpar() {...} ... c

  • Bus error vs Segmentation fault 2009-05-08

    Difference between a bus error and a segmentation fault? Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ? --------------Solutions------------- On most architectures

  • Large 2D array gives segmentation fault 2009-05-12

    I am writing some C++ code in Linux where I have declared a few 2D arrays like so: double x[5000][500], y[5000][500], z[5000][500]; During compilation there is no error. When I execute it says "segmentation fault". Wen I reduce the size of the array

  • Compiling C++ using -pthreads for Openwrt Linux-Get segmentation fault 2009-05-16

    I´m pretty new to programming in C++ and I´m using pthreads. I´m cross compiling my code for OpenWRT but for some reason I get segmentation fault when I run the program on my board but it runs fine on my PC. I suspect that the error occurs in the lin

  • C++ std::vector of pointers deletion and segmentation faults 2009-05-21

    I have a vector of pointers to a class. I need to call their destructors and free their memory. Since they are vector of pointers vector.clear() does not do the job.So I went on to do it manually like so : void Population::clearPool(std::vector<Ch

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.074 (s). 11 q(s)