瀏覽代碼

Fixed possible memory leak

tags/v0.5
Brice Figureau 17 年之前
父節點
當前提交
33a331dea8
共有 1 個檔案被更改,包括 20 行新增4 行删除
  1. 20
    4
      ngx_http_uploadprogress_module.c

+ 20
- 4
ngx_http_uploadprogress_module.c 查看文件

@@ -153,7 +153,7 @@ get_tracking_id(ngx_http_request_t * r)
153 153
         if (header[i].key.len == x_progress_id.len
154 154
             && ngx_strncasecmp(header[i].key.data, x_progress_id.data,
155 155
                            header[i].key.len) == 0) {
156
-            ret = ngx_pcalloc(r->pool, sizeof(ngx_str_t));
156
+            ret = ngx_calloc(r->pool, sizeof(ngx_str_t));
157 157
             ret->data = header[i].value.data;
158 158
             ret->len = header[i].value.len;
159 159
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 
@@ -192,7 +192,7 @@ get_tracking_id(ngx_http_request_t * r)
192 192
                 }
193 193
             }
194 194
 
195
-            ret = ngx_pcalloc(r->pool, sizeof(ngx_str_t));
195
+            ret = ngx_calloc(r->pool, sizeof(ngx_str_t));
196 196
             ret->data = start_p;
197 197
             ret->len = p - start_p;
198 198
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 
@@ -306,7 +306,7 @@ static void ngx_http_uploadprogress_strdupfree(ngx_str_t *str)
306 306
 
307 307
 static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
308 308
 {
309
-    ngx_str_t                                   *id;
309
+    ngx_str_t                                   *id, *oldid;
310 310
     ngx_slab_pool_t                             *shpool;
311 311
     ngx_connection_t                            *c;
312 312
     ngx_shm_zone_t                              *shm_zone;
@@ -320,11 +320,13 @@ static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
320 320
     c = r->connection;
321 321
 
322 322
     /* find node, update rest */
323
-    id = get_tracking_id(r);
323
+    oldid = id = get_tracking_id(r);
324 324
     
325 325
     /* perform a deep copy of id */
326 326
     id = ngx_http_uploadprogress_strdup(id, r->connection->log);
327 327
     
328
+    ngx_free(oldid);
329
+		
328 330
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
329 331
                    "upload-progress: read_event_handler found id: %V", id);
330 332
     upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
@@ -419,6 +421,7 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
419 421
     if (upcf->shm_zone == NULL) {
420 422
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
421 423
                        "reportuploads no shm_zone for id: %V", id);
424
+        ngx_free(id);
422 425
         return NGX_DECLINED;
423 426
     }
424 427
 
@@ -443,6 +446,7 @@ ngx_http_reportuploads_handler(ngx_http_request_t * r)
443 446
                        "reportuploads not found: %V", id);
444 447
     }
445 448
     ngx_shmtx_unlock(&shpool->mutex);
449
+	ngx_free(id);
446 450
 
447 451
     /* send the output */
448 452
     r->headers_out.content_type.len = sizeof("text/javascript") - 1;
@@ -627,12 +631,14 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
627 631
     if (!upcf->track) {
628 632
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
629 633
                        "trackuploads not tracking in this location for id: %V", id);
634
+        ngx_free(id);
630 635
         return NGX_DECLINED;
631 636
     }
632 637
 
633 638
     if (upcf->shm_zone == NULL) {
634 639
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
635 640
                        "trackuploads no shm_zone for id: %V", id);
641
+        ngx_free(id);
636 642
         return NGX_DECLINED;
637 643
     }
638 644
 
@@ -653,12 +659,14 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
653 659
         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
654 660
                       "upload_progress: tracking already registered id: %V", id);
655 661
 
662
+        ngx_free(id);
656 663
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
657 664
     }
658 665
 
659 666
     cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
660 667
     if (cln == NULL) {
661 668
         ngx_shmtx_unlock(&shpool->mutex);
669
+        ngx_free(id);
662 670
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
663 671
     }
664 672
 
@@ -668,6 +676,7 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
668 676
     node = ngx_slab_alloc_locked(shpool, n);
669 677
     if (node == NULL) {
670 678
         ngx_shmtx_unlock(&shpool->mutex);
679
+        ngx_free(id);
671 680
         return NGX_HTTP_SERVICE_UNAVAILABLE;
672 681
     }
673 682
 
@@ -709,6 +718,8 @@ ngx_http_uploadprogress_handler(ngx_http_request_t * r)
709 718
     upcln->timeout = upcf->timeout;
710 719
     upcln->r = r;
711 720
 
721
+    ngx_free(id);
722
+
712 723
     ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_module_ctx_t));
713 724
     if (ctx == NULL) {
714 725
       return NGX_ERROR;
@@ -957,6 +968,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
957 968
         if (upcf->shm_zone == NULL) {
958 969
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
959 970
                            "trackuploads no shm_zone for id: %V", id);
971
+            ngx_free(id);							 
960 972
             goto finish;
961 973
         }
962 974
 
@@ -977,6 +989,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
977 989
                            "trackuploads error-tracking found node for id: %V", id);
978 990
             up->err_status = r->err_status;
979 991
             ngx_shmtx_unlock(&shpool->mutex);
992
+            ngx_free(id);							 
980 993
             goto finish;
981 994
         }
982 995
 
@@ -986,6 +999,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
986 999
         cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
987 1000
         if (cln == NULL) {
988 1001
             ngx_shmtx_unlock(&shpool->mutex);
1002
+            ngx_free(id);							 
989 1003
             goto finish;
990 1004
         }
991 1005
 
@@ -993,6 +1007,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
993 1007
         node = ngx_slab_alloc_locked(shpool, n);
994 1008
         if (node == NULL) {
995 1009
             ngx_shmtx_unlock(&shpool->mutex);
1010
+            ngx_free(id);							 
996 1011
             goto finish;
997 1012
         }
998 1013
 
@@ -1029,6 +1044,7 @@ ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
1029 1044
 
1030 1045
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1031 1046
                        "trackuploads error-tracking adding: %08XD", node->key);
1047
+        ngx_free(id);							 
1032 1048
     }
1033 1049
 
1034 1050
   finish:

Loading…
取消
儲存