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

2 つの構造体ポインターを memcpy しようとすると、セグメンテーション違反が発生します。

get_request_type 関数は、json_object と構造体ポインター (out パラメーター) を受け取り、結果を示す列挙型を返します。(無効または要求タイプ)。

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

わかりませんが、query_state 構造体変数を malloc しましたが、そのメンバーを個別に出力できました。何らかの理由で、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 に答える