[PATCH 1 of 2] clear err when ngx_regex_compile has allocation failure

Maxim Dounin mdounin at mdounin.ru
Fri Sep 5 18:25:18 UTC 2014


Hello!

On Sun, Aug 17, 2014 at 11:00:29PM +0300, Markus Linnala wrote:

> # HG changeset patch
> # User Markus Linnala <Markus.Linnala at cybercom.com>
> # Date 1408303316 -10800
> #      Sun Aug 17 22:21:56 2014 +0300
> # Node ID d350d288cffef0e6395ae1f412842c3b55bc8d42
> # Parent  0719145489f842b14765506f8856798c2203e3cc
> clear err when ngx_regex_compile has allocation failure
> 
> diff -r 0719145489f8 -r d350d288cffe src/core/ngx_regex.c
> --- a/src/core/ngx_regex.c	Sun Aug 17 21:59:38 2014 +0300
> +++ b/src/core/ngx_regex.c	Sun Aug 17 22:21:56 2014 +0300
> @@ -149,6 +149,7 @@
>  
>      rc->regex = ngx_pcalloc(rc->pool, sizeof(ngx_regex_t));
>      if (rc->regex == NULL) {
> +        rc->err.len = 0;
>          return NGX_ERROR;
>      }
>  
> @@ -159,6 +160,7 @@
>      if (ngx_pcre_studies != NULL) {
>          elt = ngx_list_push(ngx_pcre_studies);
>          if (elt == NULL) {
> +            rc->err.len = 0;
>              return NGX_ERROR;
>          }

This doesn't looks good, as it will result in empty errors being 
logged in such cases.

Something like this should be more user-friendly (and it also 
fixes another long-standing bug):

# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1409938779 -14400
#      Fri Sep 05 21:39:39 2014 +0400
# Node ID f43551f64d028de939332ab9b66c3620b4259e08
# Parent  063f7e75f9efd56f3aaa3d9c24c98ed3f42348ea
Core: ngx_regex_compile() error handling fixes.

Now we actually return NGX_ERROR on errors, and provide an error
string for memory allocation errors.

Reported by Markus Linnala.

diff --git a/src/core/ngx_regex.c b/src/core/ngx_regex.c
--- a/src/core/ngx_regex.c
+++ b/src/core/ngx_regex.c
@@ -149,7 +149,8 @@ ngx_regex_compile(ngx_regex_compile_t *r
 
     rc->regex = ngx_pcalloc(rc->pool, sizeof(ngx_regex_t));
     if (rc->regex == NULL) {
-        return NGX_ERROR;
+        p = "regex \"%V\" compilation failed: no memory";
+        goto failed;
     }
 
     rc->regex->code = re;
@@ -159,7 +160,8 @@ ngx_regex_compile(ngx_regex_compile_t *r
     if (ngx_pcre_studies != NULL) {
         elt = ngx_list_push(ngx_pcre_studies);
         if (elt == NULL) {
-            return NGX_ERROR;
+            p = "regex \"%V\" compilation failed: no memory";
+            goto failed;
         }
 
         elt->regex = rc->regex;
@@ -204,7 +206,7 @@ failed:
 
     rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, p, &rc->pattern, n)
                   - rc->err.data;
-    return NGX_OK;
+    return NGX_ERROR;
 }
 
 

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx-devel mailing list