C - addrinfo - 指针分割故障

时间:2015-11-26 12:17:42

标签: c sockets

C - addrinfo - 指针分段错误

我遇到了分段错误;我做了一个奇怪的修复,但我仍然担心,因为我无法弄清问题是什么。任何见解都会令人惊叹!工作"修复"在最后一个代码块中!

尝试使用target_addrinfo进行绑定时

分段错误:

struct addrinfo *my_server_res;
struct addrinfo *their_server_res;

int main(int argc, char **argv) {
//........
    set_addrinfo();
    handle_binding();
}

void set_addrinfo() {
    int status;
    struct addrinfo hints;
    struct addrinfo *target_addrinfo; // fix (1/3): **target_addrinfo
    char ipstr[INET6_ADDRSTRLEN];

    // THIS IS TRUE, takes my_server_res
    // fix(2/3): (l ? &my_server_res : &their_server_res)
    target_addrinfo = (l ? my_server_res : their_server_res); 

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    char *node;
    node = (l ? "localhost" : host_name);

    // fix (3/3) 3rd argument: target_addrinfo
    if ((status = getaddrinfo(node, port, &hints, &target_addrinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

}

void handle_binding() {
    struct addrinfo *target_addrinfo;
    // THIS IS TRUE, takes my_server_res
    target_addrinfo = (l ? my_server_res : their_server_res);
    int b_res = bind(sockfd, target_addrinfo->ai_addr, target_addrinfo->ai_addrlen);
    if (b_res == -1) {
        perror("__Bind");
        exit(1);
    }
}

堆栈跟踪:尝试再次使用my_server_res时出现问题?:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401e16 in handle_binding () at ncTh.c:385
385     int b_res = bind(sockfd, target_addrinfo->ai_addr, target_addrinfo->ai_addrlen);
(gdb) backtrace
#0  0x0000000000401e16 in handle_binding () at ncTh.c:385
#1  0x0000000000401cad in setup_socket () at ncTh.c:330
#2  0x0000000000401ad0 in main (argc=6, argv=0x7fffffffdea8) at ncTh.c:266
(gdb) frame 0
#0  0x0000000000401e16 in handle_binding () at ncTh.c:385
385     int b_res = bind(sockfd, target_addrinfo->ai_addr, target_addrinfo->ai_addrlen);
(gdb) 

工作"修复",更改为set_addrinfo():

struct addrinfo **target_addrinfo; // was *target_addrinfo
char ipstr[INET6_ADDRSTRLEN];

target_addrinfo = (l ? &my_server_res : &their_server_res); // was my_server_res : their_server_res

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

char *node;
node = (l ? "localhost" : host_name);
                               // 3rd argument was &target_addrinfo :
if ((status = getaddrinfo(node, port, &hints, target_addrinfo)) != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
}

2 个答案:

答案 0 :(得分:2)

在原始代码中:

struct addrinfo *target_addrinfo;
target_addrinfo = (l ? my_server_res : their_server_res);

这会将指针my_server_res的值复制到target_addrinfo

getaddrinfo(node, port, &hints, &target_addrinfo)

这会将指向target_addrinfo(指向指针的指针)的指针传递给函数getaddrinfo,然后函数*&target_addrinfo = new_ptr执行等效的getaddrinfo。这个新值是指向my_server_res正在为您检索的信息的指针。

所有这些对struct addrinfo **target_addrinfo; target_addrinfo = (l ? &my_server_res : &their_server_res); 没有影响,my_server_res保留其原始值,可能为NULL。

在新代码中:

target_addrinfo

这将指向getaddrinfo(node, port, &hints, target_addrinfo) 的指针复制到target_addrinfo(现在是指向指针的指针)。

my_server_res

这会将getaddrinfo(当前是指向*target_addrinfo = new_ptr的指针)的值传递给函数my_server_res,然后函数getaddrinfo为其分配一个新值,相当于{{ 1}}。 TextView现在包含指向Article正在为您检索的信息的指针。

答案 1 :(得分:1)

你可能会解除target_addrinfo问题的解除引用,我发布了这段代码。 (最好迟到然后永远:))

struct addrinfo *my_server_res;
struct addrinfo *their_server_res;

struct addrinfo **target_addrinfo; //this should be global 

int main(int argc, char **argv) {
//........
    set_addrinfo();
    handle_binding();
}

void set_addrinfo() {
    int status;
    struct addrinfo hints;

    char ipstr[INET6_ADDRSTRLEN];

    // THIS IS TRUE, takes my_server_res
    target_addrinfo = (l ? &my_server_res : &their_server_res); //set it once here

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    char *node;
    node = (l ? "localhost" : host_name);
    if ((status = getaddrinfo(node, port, &hints, &target_addrinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

}

void handle_binding() {
    //target_addrinfo should be initialized in set_addrinfo()
    int b_res = bind(sockfd, 
        (*target_addrinfo)->ai_addr,    // don't forget to dereference target_addrinfo, here
        (*target_addrinfo)->ai_addrlen); // and here

    if (b_res == -1) {
        perror("__Bind");
        exit(1);
    }
}