Skip to content

Commit b6d8e73

Browse files
committed
🐛 Use memory pool to create large array
1 parent 3c388c8 commit b6d8e73

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

inc/ngx_http_waf_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#define TRUE (1)
2525
#define FALSE (0)
2626

27+
28+
#define RULE_MAX_LEN (256 * 4 * 8)
2729
#define INITIAL_SIZE (sizeof(hash_table_item_int_ulong_t) * 60000)
2830

2931
/* 检查对应文件是否存在,如果存在则根据 mode 的值将数据处理后存入数组中 */

src/ngx_http_waf_module.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static char* ngx_http_waf_rule_path_conf(ngx_conf_t* cf, ngx_command_t* cmd, voi
9292
return NGX_CONF_ERROR;
9393
}
9494

95-
char full_path[256 * 4 * 8];
95+
char* full_path = ngx_palloc(cf->pool, sizeof(char) * RULE_MAX_LEN);
9696
char* end = to_c_str((u_char*)full_path, srv_conf->ngx_waf_rule_path);
9797

9898
CHECK_AND_LOAD_CONF(cf, full_path, end, IPV4_FILE, srv_conf->block_ipv4, 1);
@@ -104,6 +104,7 @@ static char* ngx_http_waf_rule_path_conf(ngx_conf_t* cf, ngx_command_t* cmd, voi
104104
CHECK_AND_LOAD_CONF(cf, full_path, end, WHITE_URL_FILE, srv_conf->white_url, 0);
105105
CHECK_AND_LOAD_CONF(cf, full_path, end, WHITE_REFERER_FILE, srv_conf->white_referer, 0);
106106

107+
ngx_pfree(cf->pool, full_path);
107108
return NGX_CONF_OK;
108109
}
109110

@@ -303,6 +304,7 @@ static ngx_int_t check_cc_ipv4(ngx_http_request_t* r, ngx_http_waf_srv_conf_t* s
303304
return FAIL;
304305
}
305306

307+
306308
static ngx_int_t free_hash_table(ngx_http_request_t* r, ngx_http_waf_srv_conf_t* srv_conf) {
307309
hash_table_item_int_ulong_t* p = NULL;
308310
int count = 0;
@@ -413,11 +415,11 @@ static ngx_int_t check_ipv4(unsigned long ip, ngx_array_t* a) {
413415
static ngx_int_t load_into_array(ngx_conf_t* cf, const char* file_name, ngx_array_t* ngx_array, ngx_int_t mode) {
414416
FILE* fp = fopen(file_name, "r");
415417
ngx_str_t line;
416-
char str[256 * 4 * 8];
418+
char* str = ngx_palloc(cf->pool, sizeof(char) * RULE_MAX_LEN);
417419
if (fp == NULL) {
418420
return FAIL;
419421
}
420-
while (fgets(str, 256 * 4 * 8, fp) != NULL) {
422+
while (fgets(str, RULE_MAX_LEN - 16, fp) != NULL) {
421423
ngx_regex_compile_t rc;
422424
u_char errstr[NGX_MAX_CONF_ERRSTR];
423425
ngx_regex_elt_t* ngx_regex_elt;
@@ -450,6 +452,7 @@ static ngx_int_t load_into_array(ngx_conf_t* cf, const char* file_name, ngx_arra
450452
}
451453
}
452454
fclose(fp);
455+
ngx_pfree(cf->pool, str);
453456
return SUCCESS;
454457
}
455458

0 commit comments

Comments
 (0)