0

以下代码使用 gcc 编译。

typedef struct {
    char *device_id;
    char *device_type;
    char *home_id;
    char *op_code;
    char *arg_name;
    char *arg_value;
} query_state_t; 


enum request_type { INVALID, GET_DEVICE_TEMP};


enum request_type get_request_type(const json_t *root_obj, query_state_t *query_state_out) {
    json_t *query = json_object_get(root_obj,"query");

    if (!query || !json_is_object(query)) {
        return INVALID;
    }

    ...

    const unsigned char *request_type = json_string_value(op_code_str);
    if (strcmp(request_type, "get_DeviceTemp") == 0) {
        json_t *arg_name = json_object_get(op_code, "argName");
        json_t *arg_value = json_object_get(op_code, "argValue");
        if (!arg_name || !json_is_string(arg_name)) {
            return INVALID;
        }
        if (!arg_value || !json_is_string(arg_value)) {
            return INVALID;
        }
        query_state_t *query_state = malloc(sizeof(query_state_t));
        query_state->device_id = (char *)json_string_value(device_id);
        query_state->device_type = (char *)json_string_value(device_type);
        query_state->home_id = (char *)json_string_value(home_id);
        query_state->arg_name = (char *)json_string_value(arg_name);
        query_state->arg_value = (char *)json_string_value(arg_value);
        query_state->op_code = (char *)request_type;

        memcpy(query_state_out, query_state, sizeof(query_state_t)); //Segmentation fault (SIGSEGV)

        return GET_DEVICE_TEMP;
    }
    else {
        return INVALID;
    }
}

...

int main() {

...
            query_state_t *query_param;
            enum request_type request_type = get_request_type(root, query_param);
}

当我尝试 memcpy 两个结构指针时,我遇到了分段错误。

get_request_type 函数接受一个 json_object 和一个结构指针(一个输出参数),然后返回一个显示结果的枚举。(无效或请求类型)。

gdb 回溯显示以下内容

#0  0x00007ffff77432a7 in ?? () from /lib/x86_64-linux-gnu/libc.so.6                                      │~                                                                                                         
#1  0x0000000000401206 in get_request_type (root_obj=0x6263f0, query_state_out=0x7c00000077)              │~                                                                                                         
    at websocketserver.c:412                                                                              │~                                                                                                         
#2  0x00000000004013f2 in callback_web_socket (this=0x603010, wsi=0x625b50, reason=LWS_CALLBACK_RECEIVE,  │~                                                                                                         
    user=0x0, in=0x6262c2, len=161) at websocketserver.c:473                                              │~                                                                                                         
#3  0x00007ffff79bfd1c in user_callback_handle_rxflow () from /usr/local/lib/libwebsockets.so.4.0.0       │~                                                                                                         
#4  0x00007ffff79c39d0 in libwebsocket_rx_sm () from /usr/local/lib/libwebsockets.so.4.0.0                │~                                                                                                         
#5  0x00007ffff79c40f9 in libwebsocket_interpret_incoming_packet ()                                       │~                                                                                                         
   from /usr/local/lib/libwebsockets.so.4.0.0                                                             │~                                                                                                         
#6  0x00007ffff79bead4 in libwebsocket_read () from /usr/local/lib/libwebsockets.so.4.0.0                 │~                                                                                                         
#7  0x00007ffff79c1b20 in libwebsocket_service_fd () from /usr/local/lib/libwebsockets.so.4.0.0           │~                                                                                                         
#8  0x00007ffff79c1c0a in libwebsocket_service () from /usr/local/lib/libwebsockets.so.4.0.0              │~                                                                                                         
#9  0x0000000000401586 in main () at websocketserver.c:641 

显然,第 1 帧是有问题的帧。这就是我得到的:

(gdb) frame 1                                                                                             │~                                                                                                         
#1  0x0000000000401206 in get_request_type (root_obj=0x6263f0, query_state_out=0x7c00000077)              │~                                                                                                         
    at websocketserver.c:412                                                                              │~                                                                                                         
412                     memcpy(query_state_out, query_state, sizeof(query_state_t));

不过我不明白,我已经 malloc'd query_state 结构变量,我可以单独打印它的成员。出于某种原因,memcpy 会引发分段错误。

任何帮助都会得到帮助。

4

1 回答 1

2

您正在memcpy使用未初始化的指针。main在你的函数中试试这个:

        query_state_t *query_param = malloc(sizeof(query_state_t));
        enum request_type request_type = get_request_type(root, query_param);
于 2013-11-10T02:04:08.183 回答