From 80659b1bee6f54d3063478a094208db5cc5d9ca9 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Thu, 11 Oct 2018 16:59:37 +0300 Subject: [PATCH] Store string variables from libmaxmind in request pool Using direct references may cause SIGSEGV on database reload (when auto_reload parameter is enabled). --- ngx_http_geoip2_module.c | 12 ++++++++++-- ngx_stream_geoip2_module.c | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ngx_http_geoip2_module.c b/ngx_http_geoip2_module.c index 0d76e34..d94bd42 100644 --- a/ngx_http_geoip2_module.c +++ b/ngx_http_geoip2_module.c @@ -226,12 +226,20 @@ ngx_http_geoip2_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, FORMAT("%d", entry_data.boolean); break; case MMDB_DATA_TYPE_UTF8_STRING: - v->data = (u_char *) entry_data.utf8_string; v->len = entry_data.data_size; + v->data = ngx_pnalloc(r->pool, v->len); + if (v->data == NULL) { + return NGX_ERROR; + } + ngx_memcpy(v->data, (u_char *) entry_data.utf8_string, v->len); break; case MMDB_DATA_TYPE_BYTES: - v->data = (u_char *) entry_data.bytes; v->len = entry_data.data_size; + v->data = ngx_pnalloc(r->pool, v->len); + if (v->data == NULL) { + return NGX_ERROR; + } + ngx_memcpy(v->data, (u_char *) entry_data.bytes, v->len); break; case MMDB_DATA_TYPE_FLOAT: FORMAT("%.5f", entry_data.float_value); diff --git a/ngx_stream_geoip2_module.c b/ngx_stream_geoip2_module.c index dfd94ad..ad97ac6 100644 --- a/ngx_stream_geoip2_module.c +++ b/ngx_stream_geoip2_module.c @@ -195,12 +195,20 @@ ngx_stream_geoip2_variable(ngx_stream_session_t *s, ngx_stream_variable_value_t FORMAT("%d", entry_data.boolean); break; case MMDB_DATA_TYPE_UTF8_STRING: - v->data = (u_char *) entry_data.utf8_string; v->len = entry_data.data_size; + v->data = ngx_pnalloc(r->pool, v->len); + if (v->data == NULL) { + return NGX_ERROR; + } + ngx_memcpy(v->data, (u_char *) entry_data.utf8_string, v->len); break; case MMDB_DATA_TYPE_BYTES: - v->data = (u_char *) entry_data.bytes; v->len = entry_data.data_size; + v->data = ngx_pnalloc(r->pool, v->len); + if (v->data == NULL) { + return NGX_ERROR; + } + ngx_memcpy(v->data, (u_char *) entry_data.bytes, v->len); break; case MMDB_DATA_TYPE_FLOAT: FORMAT("%.5f", entry_data.float_value);