以下代码是使用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两个struct指针时,我遇到了分段错误。
get_request_type函数接受一个json_object和一个struct指针(一个out参数),然后返回一个显示结果的枚举。 (无效或请求类型)。
gdb backtrace显示以下内容
#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会抛出分段错误。
任何帮助都会得到帮助。
答案 0 :(得分:2)
你是memcpy
未初始化的指针。请在main
函数中尝试此操作:
query_state_t *query_param = malloc(sizeof(query_state_t));
enum request_type request_type = get_request_type(root, query_param);