changeset 3031:98a8336c5b7c

fix memory leak if GeoIP City database was used
author Igor Sysoev <igor@sysoev.ru>
date Wed, 19 Aug 2009 17:44:33 +0000
parents 3e507aa47097
children 524ba56ba9f7
files src/http/modules/ngx_http_geoip_module.c
diffstat 1 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/modules/ngx_http_geoip_module.c
+++ b/src/http/modules/ngx_http_geoip_module.c
@@ -181,6 +181,7 @@ ngx_http_geoip_city_variable(ngx_http_re
 {
     u_long                  addr;
     char                   *val;
+    size_t                  len;
     GeoIPRecord            *gr;
     struct sockaddr_in     *sin;
     ngx_http_geoip_conf_t  *gcf;
@@ -207,17 +208,32 @@ ngx_http_geoip_city_variable(ngx_http_re
     val = *(char **) ((char *) gr + data);
 
     if (val == NULL) {
-        goto not_found;
+        goto no_value;
     }
 
-    v->len = ngx_strlen(val);
+    len = ngx_strlen(val);
+    v->data = ngx_pnalloc(r->pool, len);
+
+    if (v->data == NULL) {
+        GeoIPRecord_delete(gr);
+        return NGX_ERROR;
+    }
+
+    ngx_memcpy(v->data, val, len);
+
+    v->len = len;
     v->valid = 1;
     v->no_cacheable = 0;
     v->not_found = 0;
-    v->data = (u_char *) val;
+
+    GeoIPRecord_delete(gr);
 
     return NGX_OK;
 
+no_value:
+
+    GeoIPRecord_delete(gr);
+
 not_found:
 
     v->not_found = 1;