[PATCH 5 of 5] Script: switched to ngx_align() macro in code size calculations

Maxim Dounin mdounin at mdounin.ru
Tue May 26 00:47:05 UTC 2026


Hello!

On Mon, May 25, 2026 at 04:09:44AM +0300, Maxim Dounin wrote:

> # HG changeset patch
> # User Maxim Dounin <mdounin at mdounin.ru>
> # Date 1779656364 -10800
> #      Sun May 24 23:59:24 2026 +0300
> # Node ID ca6f420a4edd9fd334e11d87540c0f899bc6f04f
> # Parent  c058606a02aed53f52b26479897a35c0dbdb2f59
> Script: switched to ngx_align() macro in code size calculations.
> 
> Previously, explicitly written alignment operations were used.  With
> the ngx_align() macro the code becomes slightly more readable, and
> script code size calculations for values better match ones for lengths.
> 
> No functional changes.
> 
> diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c
> --- a/src/http/modules/ngx_http_fastcgi_module.c
> +++ b/src/http/modules/ngx_http_fastcgi_module.c
> @@ -3473,9 +3473,8 @@ ngx_http_fastcgi_init_params(ngx_conf_t 
>          copy->len = src[i].skip_empty;
>  
>  
> -        size = (sizeof(ngx_http_script_copy_code_t)
> -                + src[i].key.len + sizeof(uintptr_t) - 1)
> -               & ~(sizeof(uintptr_t) - 1);
> +        size = sizeof(ngx_http_script_copy_code_t)
> +               + ngx_align(src[i].key.len, sizeof(uintptr_t));
>  
>          copy = ngx_array_push_n(params->values, size);
>          if (copy == NULL) {
> 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
> @@ -4094,9 +4094,8 @@ ngx_http_proxy_init_headers(ngx_conf_t *
>                                                   ngx_http_script_copy_len_code;
>          copy->len = src[i].key.len;
>  
> -        size = (sizeof(ngx_http_script_copy_code_t)
> -                + src[i].key.len + sizeof(uintptr_t) - 1)
> -               & ~(sizeof(uintptr_t) - 1);
> +        size = sizeof(ngx_http_script_copy_code_t)
> +               + ngx_align(src[i].key.len, sizeof(uintptr_t));
>  
>          copy = ngx_array_push_n(headers->values, size);
>          if (copy == NULL) {
> diff --git a/src/http/modules/ngx_http_scgi_module.c b/src/http/modules/ngx_http_scgi_module.c
> --- a/src/http/modules/ngx_http_scgi_module.c
> +++ b/src/http/modules/ngx_http_scgi_module.c
> @@ -1840,9 +1840,8 @@ ngx_http_scgi_init_params(ngx_conf_t *cf
>          copy->len = src[i].skip_empty;
>  
>  
> -        size = (sizeof(ngx_http_script_copy_code_t)
> -                + src[i].key.len + 1 + sizeof(uintptr_t) - 1)
> -               & ~(sizeof(uintptr_t) - 1);
> +        size = sizeof(ngx_http_script_copy_code_t)
> +               + ngx_align(src[i].key.len + 1, sizeof(uintptr_t));
>  
>          copy = ngx_array_push_n(params->values, size);
>          if (copy == NULL) {
> diff --git a/src/http/modules/ngx_http_uwsgi_module.c b/src/http/modules/ngx_http_uwsgi_module.c
> --- a/src/http/modules/ngx_http_uwsgi_module.c
> +++ b/src/http/modules/ngx_http_uwsgi_module.c
> @@ -2141,9 +2141,8 @@ ngx_http_uwsgi_init_params(ngx_conf_t *c
>          copy->len = src[i].skip_empty;
>  
>  
> -        size = (sizeof(ngx_http_script_copy_code_t)
> -                + src[i].key.len + sizeof(uintptr_t) - 1)
> -               & ~(sizeof(uintptr_t) - 1);
> +        size = sizeof(ngx_http_script_copy_code_t)
> +               + ngx_align(src[i].key.len, sizeof(uintptr_t));
>  
>          copy = ngx_array_push_n(params->values, size);
>          if (copy == NULL) {
> diff --git a/src/http/ngx_http_script.c b/src/http/ngx_http_script.c
> --- a/src/http/ngx_http_script.c
> +++ b/src/http/ngx_http_script.c
> @@ -194,12 +194,10 @@ ngx_http_compile_complex_value(ngx_http_
>          return NGX_ERROR;
>      }
>  
> -    n = (nv * (2 * sizeof(ngx_http_script_copy_code_t)
> -                   + sizeof(ngx_http_script_var_code_t))
> -                + sizeof(uintptr_t)
> -                + v->len
> -                + sizeof(uintptr_t) - 1)
> -            & ~(sizeof(uintptr_t) - 1);
> +    n = nv * (2 * sizeof(ngx_http_script_copy_code_t)
> +                  + sizeof(ngx_http_script_var_code_t))
> +        + sizeof(uintptr_t)
> +        + ngx_align(v->len, sizeof(uintptr_t));
>  
>      if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {
>          return NGX_ERROR;
> @@ -696,12 +694,10 @@ ngx_http_script_init_arrays(ngx_http_scr
>      }
>  
>      if (*sc->values == NULL) {
> -        n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
> -                              + sizeof(ngx_http_script_var_code_t))
> -                + sizeof(uintptr_t)
> -                + sc->source->len
> -                + sizeof(uintptr_t) - 1)
> -            & ~(sizeof(uintptr_t) - 1);
> +        n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
> +                             + sizeof(ngx_http_script_var_code_t))
> +            + sizeof(uintptr_t)
> +            + ngx_align(sc->source->len, sizeof(uintptr_t));
>  
>          *sc->values = ngx_array_create(sc->cf->pool, n, 1);
>          if (*sc->values == NULL) {
> @@ -819,8 +815,8 @@ ngx_http_script_add_copy_code(ngx_http_s
>                                                   ngx_http_script_copy_len_code;
>      code->len = len;
>  
> -    size = (sizeof(ngx_http_script_copy_code_t) + len + sizeof(uintptr_t) - 1)
> -            & ~(sizeof(uintptr_t) - 1);
> +    size = sizeof(ngx_http_script_copy_code_t)
> +           + ngx_align(len, sizeof(uintptr_t));
>  
>      code = ngx_http_script_add_code(*sc->values, size, &sc->main);
>      if (code == NULL) {
> @@ -871,7 +867,7 @@ ngx_http_script_copy_code(ngx_http_scrip
>      }
>  
>      e->ip += sizeof(ngx_http_script_copy_code_t)
> -          + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
> +          + ngx_align(code->len, sizeof(uintptr_t));
>  
>      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
>                     "http script copy: \"%*s\"", e->pos - p, p);
> 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
> @@ -194,12 +194,10 @@ ngx_stream_compile_complex_value(ngx_str
>          return NGX_ERROR;
>      }
>  
> -    n = (nv * (2 * sizeof(ngx_stream_script_copy_code_t)
> -                   + sizeof(ngx_stream_script_var_code_t))
> -                + sizeof(uintptr_t)
> -                + v->len
> -                + sizeof(uintptr_t) - 1)
> -            & ~(sizeof(uintptr_t) - 1);
> +    n = nv * (2 * sizeof(ngx_stream_script_copy_code_t)
> +                  + sizeof(ngx_stream_script_var_code_t))
> +        + sizeof(uintptr_t)
> +        + ngx_align(v->len, sizeof(uintptr_t));
>  
>      if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {
>          return NGX_ERROR;
> @@ -577,12 +575,10 @@ ngx_stream_script_init_arrays(ngx_stream
>      }
>  
>      if (*sc->values == NULL) {
> -        n = (sc->variables * (2 * sizeof(ngx_stream_script_copy_code_t)
> -                              + sizeof(ngx_stream_script_var_code_t))
> -                + sizeof(uintptr_t)
> -                + sc->source->len
> -                + sizeof(uintptr_t) - 1)
> -            & ~(sizeof(uintptr_t) - 1);
> +        n = sc->variables * (2 * sizeof(ngx_stream_script_copy_code_t)
> +                             + sizeof(ngx_stream_script_var_code_t))
> +            + sizeof(uintptr_t)
> +            + ngx_align(sc->source->len, sizeof(uintptr_t));
>  
>          *sc->values = ngx_array_create(sc->cf->pool, n, 1);
>          if (*sc->values == NULL) {
> @@ -688,8 +684,8 @@ ngx_stream_script_add_copy_code(ngx_stre
>                                                 ngx_stream_script_copy_len_code;
>      code->len = len;
>  
> -    size = (sizeof(ngx_stream_script_copy_code_t) + len + sizeof(uintptr_t) - 1)
> -            & ~(sizeof(uintptr_t) - 1);
> +    size = sizeof(ngx_stream_script_copy_code_t)
> +           + ngx_align(len, sizeof(uintptr_t));
>  
>      code = ngx_stream_script_add_code(*sc->values, size, &sc->main);
>      if (code == NULL) {
> @@ -740,7 +736,7 @@ ngx_stream_script_copy_code(ngx_stream_s
>      }
>  
>      e->ip += sizeof(ngx_stream_script_copy_code_t)
> -          + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
> +          + ngx_align(code->len, sizeof(uintptr_t));
>  
>      ngx_log_debug2(NGX_LOG_DEBUG_STREAM, e->session->connection->log, 0,
>                     "stream script copy: \"%*s\"", e->pos - p, p);
> 

It looks like I missed one case, in the gRPC proxy module:

diff --git a/src/http/modules/ngx_http_grpc_module.c b/src/http/modules/ngx_http_grpc_module.c
--- a/src/http/modules/ngx_http_grpc_module.c
+++ b/src/http/modules/ngx_http_grpc_module.c
@@ -4717,9 +4717,8 @@ ngx_http_grpc_init_headers(ngx_conf_t *c
                                                  ngx_http_script_copy_len_code;
         copy->len = src[i].key.len;
 
-        size = (sizeof(ngx_http_script_copy_code_t)
-                + src[i].key.len + sizeof(uintptr_t) - 1)
-               & ~(sizeof(uintptr_t) - 1);
+        size = sizeof(ngx_http_script_copy_code_t)
+               + ngx_align(src[i].key.len, sizeof(uintptr_t));
 
         copy = ngx_array_push_n(headers->values, size);
         if (copy == NULL) {


-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx-devel mailing list