Patch subject is complete summary.

 src/core/ngx_cycle.c                     |  10 ++++++----
 src/core/ngx_resolver.c                  |   2 +-
 src/core/ngx_string.c                    |  15 +++++++++++++++
 src/http/modules/ngx_http_proxy_module.c |   4 ++--
 src/http/ngx_http_file_cache.c           |   4 +++-
 src/http/ngx_http_variables.c            |   3 +++
 src/mail/ngx_mail_auth_http_module.c     |  12 +++++++++---
 src/stream/ngx_stream_script.c           |   4 +++-
 8 files changed, 42 insertions(+), 12 deletions(-)


# HG changeset patch
# User Vladimir Khomutov <v...@wbsrv.ru>
# Date 1698407658 -10800
#      Fri Oct 27 14:54:18 2023 +0300
# Node ID ef9f124b156aff0e9f66057e438af835bd7a60d2
# Parent  ea1f29c2010cda4940b741976f103d547308815a
Core: avoid calling memcpy() in edge cases.

diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -115,10 +115,12 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
                 old_cycle->conf_file.len + 1);
 
     cycle->conf_param.len = old_cycle->conf_param.len;
-    cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
-    if (cycle->conf_param.data == NULL) {
-        ngx_destroy_pool(pool);
-        return NULL;
+    if (cycle->conf_param.len) {
+        cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
+        if (cycle->conf_param.data == NULL) {
+            ngx_destroy_pool(pool);
+            return NULL;
+        }
     }
 
 
diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c
+++ b/src/core/ngx_resolver.c
@@ -4206,7 +4206,7 @@ ngx_resolver_dup(ngx_resolver_t *r, void
 
     dst = ngx_resolver_alloc(r, size);
 
-    if (dst == NULL) {
+    if (dst == NULL || size == 0 || src == NULL) {
         return dst;
     }
 
diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -252,6 +252,11 @@ ngx_vslprintf(u_char *buf, u_char *last,
             case 'V':
                 v = va_arg(args, ngx_str_t *);
 
+                if (v->len == 0 || v->data == NULL) {
+                    fmt++;
+                    continue;
+                }
+
                 buf = ngx_sprintf_str(buf, last, v->data, v->len, hex);
                 fmt++;
 
@@ -260,6 +265,11 @@ ngx_vslprintf(u_char *buf, u_char *last,
             case 'v':
                 vv = va_arg(args, ngx_variable_value_t *);
 
+                if (vv->len == 0 || vv->data == NULL) {
+                    fmt++;
+                    continue;
+                }
+
                 buf = ngx_sprintf_str(buf, last, vv->data, vv->len, hex);
                 fmt++;
 
@@ -268,6 +278,11 @@ ngx_vslprintf(u_char *buf, u_char *last,
             case 's':
                 p = va_arg(args, u_char *);
 
+                if (slen == 0 || p == NULL) {
+                    fmt++;
+                    continue;
+                }
+
                 buf = ngx_sprintf_str(buf, last, p, slen, hex);
                 fmt++;
 
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -1205,7 +1205,7 @@ ngx_http_proxy_create_key(ngx_http_reque
 
     key->data = p;
 
-    if (r->valid_location) {
+    if (r->valid_location && ctx->vars.uri.len) {
         p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len);
     }
 
@@ -1422,7 +1422,7 @@ ngx_http_proxy_create_request(ngx_http_r
         b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
 
     } else {
-        if (r->valid_location) {
+        if (r->valid_location && ctx->vars.uri.len) {
             b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
         }
 
diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -1270,7 +1270,9 @@ ngx_http_file_cache_set_header(ngx_http_
 
     if (c->etag.len <= NGX_HTTP_CACHE_ETAG_LEN) {
         h->etag_len = (u_char) c->etag.len;
-        ngx_memcpy(h->etag, c->etag.data, c->etag.len);
+        if (c->etag.len) {
+            ngx_memcpy(h->etag, c->etag.data, c->etag.len);
+        }
     }
 
     if (c->vary.len) {
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -2157,6 +2157,9 @@ ngx_http_variable_request_body(ngx_http_
 
     for ( /* void */ ; cl; cl = cl->next) {
         buf = cl->buf;
+        if (buf->last == buf->pos) {
+            continue;
+        }
         p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
     }
 
diff --git a/src/mail/ngx_mail_auth_http_module.c b/src/mail/ngx_mail_auth_http_module.c
--- a/src/mail/ngx_mail_auth_http_module.c
+++ b/src/mail/ngx_mail_auth_http_module.c
@@ -1314,11 +1314,15 @@ ngx_mail_auth_http_create_request(ngx_ma
     *b->last++ = CR; *b->last++ = LF;
 
     b->last = ngx_cpymem(b->last, "Auth-User: ", sizeof("Auth-User: ") - 1);
-    b->last = ngx_copy(b->last, login.data, login.len);
+    if (login.len) {
+        b->last = ngx_copy(b->last, login.data, login.len);
+    }
     *b->last++ = CR; *b->last++ = LF;
 
     b->last = ngx_cpymem(b->last, "Auth-Pass: ", sizeof("Auth-Pass: ") - 1);
-    b->last = ngx_copy(b->last, passwd.data, passwd.len);
+    if (passwd.len) {
+        b->last = ngx_copy(b->last, passwd.data, passwd.len);
+    }
     *b->last++ = CR; *b->last++ = LF;
 
     if (s->auth_method != NGX_MAIL_AUTH_PLAIN && s->salt.len) {
@@ -1375,7 +1379,9 @@ ngx_mail_auth_http_create_request(ngx_ma
 
         b->last = ngx_cpymem(b->last, "Auth-SMTP-Helo: ",
                              sizeof("Auth-SMTP-Helo: ") - 1);
-        b->last = ngx_copy(b->last, s->smtp_helo.data, s->smtp_helo.len);
+        if (s->smtp_helo.len) {
+            b->last = ngx_copy(b->last, s->smtp_helo.data, s->smtp_helo.len);
+        }
         *b->last++ = CR; *b->last++ = LF;
 
         b->last = ngx_cpymem(b->last, "Auth-SMTP-From: ",
diff --git a/src/stream/ngx_stream_script.c b/src/stream/ngx_stream_script.c
--- a/src/stream/ngx_stream_script.c
+++ b/src/stream/ngx_stream_script.c
@@ -842,7 +842,9 @@ ngx_stream_script_copy_var_code(ngx_stre
 
         if (value && !value->not_found) {
             p = e->pos;
-            e->pos = ngx_copy(p, value->data, value->len);
+            if (value->len) {
+                e->pos = ngx_copy(p, value->data, value->len);
+            }
 
             ngx_log_debug2(NGX_LOG_DEBUG_STREAM,
                            e->session->connection->log, 0,
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel

Reply via email to