diff options
| author | liuwentan <[email protected]> | 2024-01-09 11:34:11 +0800 |
|---|---|---|
| committer | liuwentan <[email protected]> | 2024-01-09 11:34:11 +0800 |
| commit | 2e87c66111cf1ada80bd9b3e46dcd4d975a9c27d (patch) | |
| tree | 8e18628b524ede4cad5e11ab6b614e7e28336e82 | |
| parent | 59fdd5b2ce187051261166c0d9c298a73e3d8175 (diff) | |
[HTTP_DECODER]bugfix for double free
| -rw-r--r-- | src/http_decoder/http_decoder.c | 14 | ||||
| -rw-r--r-- | test/http_decoder/http_decoder_gtest.cpp | 100 |
2 files changed, 72 insertions, 42 deletions
diff --git a/src/http_decoder/http_decoder.c b/src/http_decoder/http_decoder.c index b69887a..1aab5ce 100644 --- a/src/http_decoder/http_decoder.c +++ b/src/http_decoder/http_decoder.c @@ -27,10 +27,8 @@ #define HD_RESULT_QUEUE_SIZE 16 #define HD_IS_CACHE_BODY 1 - const char *http_decoder_topic = "HTTP_DECODER_MESSAGE"; const char *fs_file_name = "http_decoder.fs"; -const struct fieldstat_tag TEST_TAG_INT = {"INT_TAG ", TAG_INTEGER, {.value_longlong = 100}}; struct http_decoder_result { struct http_decoder_half_data *req_data; @@ -439,11 +437,11 @@ int http_decoder_entry(struct session *sess, int events, long long trans_cnt = http_decoder_half_trans_count(cur_half); fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_bytes_id, - &TEST_TAG_INT, 1, (long long)payload_len); + NULL, 0, payload_len); fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_pkts_id, - &TEST_TAG_INT, 1, 1); + NULL, 0, 1); fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_trans_id, - &TEST_TAG_INT, 1, trans_cnt); + NULL, 0, trans_cnt); return 0; } @@ -498,6 +496,7 @@ _http_decoder_context_free(struct http_decoder_context *ctx) FREE(ctx); } +#define FS_OUTPUT_INTERVAL_S 2 void *http_decoder_init(struct stellar *st) { struct http_decoder_context *ctx = CALLOC(struct http_decoder_context, 1); @@ -548,7 +547,8 @@ void *http_decoder_init(struct stellar *st) goto failed; } - int ret = fieldstat_easy_enable_auto_output(ctx->fse, fs_file_name, 3); + int ret = fieldstat_easy_enable_auto_output(ctx->fse, fs_file_name, + FS_OUTPUT_INTERVAL_S); if (ret < 0) { fprintf(stderr, "fieldstat_easy_enable_auto_output failed."); goto failed; @@ -575,8 +575,6 @@ void http_decoder_exit(void *decoder_ctx) (struct http_decoder_context *)decoder_ctx; _http_decoder_context_free(ctx); - - FREE(ctx); } enum http_message_type diff --git a/test/http_decoder/http_decoder_gtest.cpp b/test/http_decoder/http_decoder_gtest.cpp index 95ac3a4..72c6769 100644 --- a/test/http_decoder/http_decoder_gtest.cpp +++ b/test/http_decoder/http_decoder_gtest.cpp @@ -39,6 +39,7 @@ int g_req_exdata_idx = 0; int g_res_exdata_idx = 0; int g_topic_id = 0; + #if 0 void output_http_req_line(struct http_request_line *req_line) { @@ -72,7 +73,8 @@ void output_http_header(struct http_header *header) } #endif -void output_http_header(struct http_header *header, int res) +void +output_http_header(struct http_header *header, int res) { char key[MAX_KEY_STR_LEN] = {0}; @@ -85,7 +87,8 @@ void output_http_header(struct http_header *header, int res) } -void output_http_body(struct hstring *body, int decompress_flag) +void +output_http_body(struct hstring *body, int decompress_flag) { int counter = 0; @@ -105,7 +108,8 @@ void output_http_body(struct hstring *body, int decompress_flag) printf("\n"); } -int http_field_to_json(cJSON *object, const char *key, char *val, size_t val_len) +int +http_field_to_json(cJSON *object, const char *key, char *val, size_t val_len) { if (NULL == object || NULL == key || NULL == val || 0 == val_len) { return -1; @@ -119,27 +123,34 @@ int http_field_to_json(cJSON *object, const char *key, char *val, size_t val_len return 0; } -void req_line_to_json(cJSON *ctx, struct http_request_line *req_line) +void +req_line_to_json(cJSON *ctx, struct http_request_line *req_line) { - http_field_to_json(ctx, "method", req_line->method.str, req_line->method.str_len); + http_field_to_json(ctx, "method", req_line->method.str, + req_line->method.str_len); http_field_to_json(ctx, "uri", req_line->uri.str, req_line->uri.str_len); - http_field_to_json(ctx, "req_version", req_line->version.str, req_line->version.str_len); + http_field_to_json(ctx, "req_version", req_line->version.str, + req_line->version.str_len); cJSON_AddNumberToObject(ctx, "major_version", req_line->major_version); cJSON_AddNumberToObject(ctx, "minor_version", req_line->minor_version); } -void res_line_to_json(cJSON *ctx, struct http_response_line *res_line) +void +res_line_to_json(cJSON *ctx, struct http_response_line *res_line) { - http_field_to_json(ctx, "res_version", res_line->version.str, res_line->version.str_len); - http_field_to_json(ctx, "res_status", res_line->status.str, res_line->status.str_len); + http_field_to_json(ctx, "res_version", res_line->version.str, + res_line->version.str_len); + http_field_to_json(ctx, "res_status", res_line->status.str, + res_line->status.str_len); cJSON_AddNumberToObject(ctx, "major_version", res_line->major_version); cJSON_AddNumberToObject(ctx, "minor_version", res_line->minor_version); cJSON_AddNumberToObject(ctx, "status_code", res_line->status_code); } -void http_header_to_json(cJSON *ctx, struct http_header *header) +void +http_header_to_json(cJSON *ctx, struct http_header *header) { char key[MAX_KEY_STR_LEN] = {0}; @@ -155,14 +166,16 @@ void http_header_to_json(cJSON *ctx, struct http_header *header) } } -static int http_decoder_test_entry(struct session *sess, int topic_id, const void *data, void *cb_arg) +static int +http_decoder_test_entry(struct session *sess, int topic_id, const void *data, + void *cb_arg) { - struct http_message *msg = (struct http_message *)data; + int exdata_idx = 0; struct http_request_line req_line = {0}; struct http_response_line res_line = {0}; struct http_header header = {0}; struct hstring body = {0}; - int exdata_idx = 0; + struct http_message *msg = (struct http_message *)data; enum http_message_type msg_type = http_message_type(msg); if (msg_type == HTTP_MESSAGE_REQ_LINE || @@ -171,28 +184,29 @@ static int http_decoder_test_entry(struct session *sess, int topic_id, const voi } else { exdata_idx = g_res_exdata_idx; } - cJSON *ctx = (cJSON *)session_get_ex_data(sess, exdata_idx); + + cJSON *json = (cJSON *)session_get_ex_data(sess, exdata_idx); if (msg_type == HTTP_MESSAGE_REQ_BODY || msg_type == HTTP_MESSAGE_RES_BODY) { goto next; } - if (NULL == ctx) { - ctx = cJSON_CreateObject(); - cJSON_AddStringToObject(ctx, "Tuple4", session_get0_readable_addr(sess)); - session_set_ex_data(sess, exdata_idx, ctx); + if (NULL == json) { + json = cJSON_CreateObject(); + cJSON_AddStringToObject(json, "Tuple4", session_get0_readable_addr(sess)); + session_set_ex_data(sess, exdata_idx, json); } next: switch (msg_type) { case HTTP_MESSAGE_REQ_LINE: http_message_get_request_line(msg, &req_line); - req_line_to_json(ctx, &req_line); + req_line_to_json(json, &req_line); break; case HTTP_MESSAGE_REQ_HEADER: while (http_message_request_header_next(msg, &header) > 0) { - http_header_to_json(ctx, &header); + http_header_to_json(json, &header); } g_header_count = 1; break; @@ -205,11 +219,11 @@ next: break; case HTTP_MESSAGE_RES_LINE: http_message_get_response_line(msg, &res_line); - res_line_to_json(ctx, &res_line); + res_line_to_json(json, &res_line); break; case HTTP_MESSAGE_RES_HEADER: while (http_message_response_header_next(msg, &header) > 0) { - http_header_to_json(ctx, &header); + http_header_to_json(json, &header); } g_header_count = 1; break; @@ -227,7 +241,7 @@ next: char result_name[MAX_KEY_STR_LEN] = {0}; if (msg_type == HTTP_MESSAGE_REQ_HEADER) { sprintf(result_name, "HTTP_DECODER_RESULT_%d", g_result_count); - commit_test_result_json(ctx, result_name); + commit_test_result_json(json, result_name); // printf("req json:%s\n", cJSON_Print(ctx)); session_set_ex_data(sess, exdata_idx, NULL); g_result_count++; @@ -235,7 +249,7 @@ next: if (msg_type == HTTP_MESSAGE_RES_HEADER) { sprintf(result_name, "HTTP_DECODER_RESULT_%d", g_result_count); - commit_test_result_json(ctx, result_name); + commit_test_result_json(json, result_name); // printf("res json:%s\n", cJSON_Print(ctx)); session_set_ex_data(sess, exdata_idx, NULL); g_result_count++; @@ -244,23 +258,41 @@ next: return 0; } +void +http_decoder_test_exdata_free(struct session *sess, int idx, void *ex_ptr, + void *arg) +{ + if (ex_ptr != NULL) { + cJSON_Delete((cJSON *)ex_ptr); + } +} + extern "C" void *http_decoder_test_init(struct stellar *st) { - g_req_exdata_idx = stellar_session_get_ex_new_index(st, "HTTP_DECODER_REQ_TEST", NULL, NULL); + g_req_exdata_idx = + stellar_session_get_ex_new_index(st, "HTTP_DECODER_REQ_TEST", + http_decoder_test_exdata_free, + NULL); if (g_req_exdata_idx < 0) { - printf("http_decoder_test_init: can't get http_decoder req exdata index !!!\n"); + printf("[%s:%d]: can't get http_decoder req exdata index !!!\n", + __FUNCTION__, __LINE__); exit(-1); } - g_res_exdata_idx = stellar_session_get_ex_new_index(st, "HTTP_DECODER_RES_TEST", NULL, NULL); + g_res_exdata_idx = + stellar_session_get_ex_new_index(st, "HTTP_DECODER_RES_TEST", + http_decoder_test_exdata_free, + NULL); if (g_res_exdata_idx < 0) { - printf("http_decoder_test_init: can't get http_decoder res exdata index !!!\n"); + printf("[%s:%d]: can't get http_decoder res exdata index !!!\n", + __FUNCTION__, __LINE__); exit(-1); } g_topic_id = session_mq_get_topic_id(st, "HTTP_DECODER_MESSAGE"); if (g_topic_id < 0) { - printf("http_decoder_test_init: can't get http_decoder topic id !!!\n"); + printf("[%s:%d]: can't get http_decoder topic id !!!\n", + __FUNCTION__, __LINE__); exit(-1); } @@ -268,14 +300,14 @@ extern "C" void *http_decoder_test_init(struct stellar *st) printf("http_decoder_test_init OK!\n"); return NULL; + } -extern "C" void http_decoder_test_exit(void *ctx) +extern "C" void http_decoder_test_exit(void *test_ctx) { - if (NULL == ctx) { - return; + if (test_ctx != NULL) { + FREE(test_ctx); } - - FREE(ctx); + printf("http_decoder_test_exit OK!\n"); }
\ No newline at end of file |
