comparison src/http/modules/ngx_http_geo_module.c @ 7213:c69c13f10502

Geo: fixed memory allocation error handling (closes #1482). If during configuration parsing of the geo directive the memory allocation has failed, pool used to parse configuration inside the block, and sometimes the temporary pool were not destroyed.
author Ruslan Ermilov <ru@nginx.com>
date Wed, 21 Feb 2018 15:50:42 +0300
parents 47b7ffc3339d
children 88aad69eccef
comparison
equal deleted inserted replaced
7212:0237af43d409 7213:c69c13f10502
437 437
438 ngx_memzero(&ctx, sizeof(ngx_http_geo_conf_ctx_t)); 438 ngx_memzero(&ctx, sizeof(ngx_http_geo_conf_ctx_t));
439 439
440 ctx.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log); 440 ctx.temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log);
441 if (ctx.temp_pool == NULL) { 441 if (ctx.temp_pool == NULL) {
442 ngx_destroy_pool(pool);
442 return NGX_CONF_ERROR; 443 return NGX_CONF_ERROR;
443 } 444 }
444 445
445 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value); 446 ngx_rbtree_init(&ctx.rbtree, &ctx.sentinel, ngx_str_rbtree_insert_value);
446 447
480 481
481 len = a->nelts * sizeof(ngx_http_geo_range_t); 482 len = a->nelts * sizeof(ngx_http_geo_range_t);
482 483
483 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *)); 484 ctx.high.low[i] = ngx_palloc(cf->pool, len + sizeof(void *));
484 if (ctx.high.low[i] == NULL) { 485 if (ctx.high.low[i] == NULL) {
485 return NGX_CONF_ERROR; 486 goto failed;
486 } 487 }
487 488
488 ngx_memcpy(ctx.high.low[i], a->elts, len); 489 ngx_memcpy(ctx.high.low[i], a->elts, len);
489 ctx.high.low[i][a->nelts].value = NULL; 490 ctx.high.low[i][a->nelts].value = NULL;
490 ctx.data_size += len + sizeof(void *); 491 ctx.data_size += len + sizeof(void *);
506 geo->u.high = ctx.high; 507 geo->u.high = ctx.high;
507 508
508 var->get_handler = ngx_http_geo_range_variable; 509 var->get_handler = ngx_http_geo_range_variable;
509 var->data = (uintptr_t) geo; 510 var->data = (uintptr_t) geo;
510 511
511 ngx_destroy_pool(ctx.temp_pool);
512 ngx_destroy_pool(pool);
513
514 } else { 512 } else {
515 if (ctx.tree == NULL) { 513 if (ctx.tree == NULL) {
516 ctx.tree = ngx_radix_tree_create(cf->pool, -1); 514 ctx.tree = ngx_radix_tree_create(cf->pool, -1);
517 if (ctx.tree == NULL) { 515 if (ctx.tree == NULL) {
518 return NGX_CONF_ERROR; 516 goto failed;
519 } 517 }
520 } 518 }
521 519
522 geo->u.trees.tree = ctx.tree; 520 geo->u.trees.tree = ctx.tree;
523 521
524 #if (NGX_HAVE_INET6) 522 #if (NGX_HAVE_INET6)
525 if (ctx.tree6 == NULL) { 523 if (ctx.tree6 == NULL) {
526 ctx.tree6 = ngx_radix_tree_create(cf->pool, -1); 524 ctx.tree6 = ngx_radix_tree_create(cf->pool, -1);
527 if (ctx.tree6 == NULL) { 525 if (ctx.tree6 == NULL) {
528 return NGX_CONF_ERROR; 526 goto failed;
529 } 527 }
530 } 528 }
531 529
532 geo->u.trees.tree6 = ctx.tree6; 530 geo->u.trees.tree6 = ctx.tree6;
533 #endif 531 #endif
534 532
535 var->get_handler = ngx_http_geo_cidr_variable; 533 var->get_handler = ngx_http_geo_cidr_variable;
536 var->data = (uintptr_t) geo; 534 var->data = (uintptr_t) geo;
537
538 ngx_destroy_pool(ctx.temp_pool);
539 ngx_destroy_pool(pool);
540 535
541 if (ngx_radix32tree_insert(ctx.tree, 0, 0, 536 if (ngx_radix32tree_insert(ctx.tree, 0, 0,
542 (uintptr_t) &ngx_http_variable_null_value) 537 (uintptr_t) &ngx_http_variable_null_value)
543 == NGX_ERROR) 538 == NGX_ERROR)
544 { 539 {
545 return NGX_CONF_ERROR; 540 goto failed;
546 } 541 }
547 542
548 /* NGX_BUSY is okay (default was set explicitly) */ 543 /* NGX_BUSY is okay (default was set explicitly) */
549 544
550 #if (NGX_HAVE_INET6) 545 #if (NGX_HAVE_INET6)
551 if (ngx_radix128tree_insert(ctx.tree6, zero.s6_addr, zero.s6_addr, 546 if (ngx_radix128tree_insert(ctx.tree6, zero.s6_addr, zero.s6_addr,
552 (uintptr_t) &ngx_http_variable_null_value) 547 (uintptr_t) &ngx_http_variable_null_value)
553 == NGX_ERROR) 548 == NGX_ERROR)
554 { 549 {
555 return NGX_CONF_ERROR; 550 goto failed;
556 } 551 }
557 #endif 552 #endif
558 } 553 }
559 554
555 ngx_destroy_pool(ctx.temp_pool);
556 ngx_destroy_pool(pool);
557
560 return rv; 558 return rv;
559
560 failed:
561
562 ngx_destroy_pool(ctx.temp_pool);
563 ngx_destroy_pool(pool);
564
565 return NGX_CONF_ERROR;
561 } 566 }
562 567
563 568
564 static char * 569 static char *
565 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) 570 ngx_http_geo(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)