# Why do ngx\_ssl\_get\_protocol() and ngx\_ssl\_get\_cipher\_name() leave ngx\_str\_t-\>len uninitialized?

**URL:** <https://community.nginx.org/t/why-do-ngx-ssl-get-protocol-and-ngx-ssl-get-cipher-name-leave-ngx-str-t-len-uninitialized/9649>\
**Category:** NGINX\
**Tags:** ssl\
**Created:** [June 9, 2026, 2:33pm UTC](https://community.nginx.org/t/why-do-ngx-ssl-get-protocol-and-ngx-ssl-get-cipher-name-leave-ngx-str-t-len-uninitialized/9649 "2026-06-09T14:33:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TUTU](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@TUTU](https://community.nginx.org/u/TUTU)\
**Post date:** [June 9, 2026, 2:33pm UTC](https://community.nginx.org/t/why-do-ngx-ssl-get-protocol-and-ngx-ssl-get-cipher-name-leave-ngx-str-t-len-uninitialized/9649/1 "2026-06-09T14:33:33Z")

</div>

**My issue:**

I noticed that `ngx_ssl_get_protocol()` and `ngx_ssl_get_cipher_name()` only assign the static pointer returned by OpenSSL to `s->data`, leaving `s->len` completely untouched (which remains `0` if initialized with `ngx_str_null`, or garbage if allocated on the stack):

Is there any historical reason or architectural consideration behind leaving `s->len` uninitialized in these specific functions?

```auto
ngx_int_t
ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
{
    s->data = (u_char *) SSL_get_version(c->ssl->connection);
    return NGX_OK;
}

ngx_int_t
ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
{
    s->data = (u_char *) SSL_get_cipher_name(c->ssl->connection);
    return NGX_OK;
}

```

**How I encountered the problem:**

**Solutions I’ve tried:**

Add `ngx_strlen()`.

**Version of NGINX or NGINX adjacent software (e.g. NGINX Gateway Fabric):**

nginx-1.31.1

**Deployment environment:**

ubuntu

---

<div class="post-metadata">

**Author:** ![system](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/system/32/19_2.png) [@system](https://community.nginx.org/u/system)\
**Post date:** [July 9, 2026, 2:34pm UTC](https://community.nginx.org/t/why-do-ngx-ssl-get-protocol-and-ngx-ssl-get-cipher-name-leave-ngx-str-t-len-uninitialized/9649/2 "2026-07-09T14:34:24Z")

</div>

Closed on July 9, 2026, 2:34pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![ac000](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/ac000/32/207_2.png) [@ac000](https://community.nginx.org/u/ac000)\
**Post date:** [September 3, 2026, 4:29pm UTC](https://community.nginx.org/t/why-do-ngx-ssl-get-protocol-and-ngx-ssl-get-cipher-name-leave-ngx-str-t-len-uninitialized/9649/3 "2026-09-03T16:29:47Z")

</div>

Good question — from what I can tell this is intentional rather than an oversight.

`SSL_get_version()` and `SSL_get_cipher_name()` return a pointer to a static, NUL-terminated string owned by OpenSSL. `ngx_ssl_get_protocol()` and `ngx_ssl_get_cipher_name()` deliberately only pass that pointer back in `s->data`; computing the length is left to the caller, which is why `s->len` is untouched.

Every caller does set it, at the point it’s needed:

• The `$ssl_protocol` / `$ssl_cipher` variables are handled by `ngx_http_ssl_static_variable()` (note the static in the name), which scans for the terminating NUL to fill in `v->len`:

```c
for (len = 0; v->data[len]; len++) { /* void */ }
v->len = len;

```

• The mail module sets it explicitly:

```c
protocol.len = ngx_strlen(protocol.data);

```

This is distinct from the `ngx_http_ssl_variable()` family (e.g. `$ssl_ciphers`, `$ssl_curve`), whose underlying getters build the value and set `len` themselves.

So if you call these two functions directly in your own code, just do as the mail module does — `s->len = ngx_strlen(s->data);` after the call (and `ngx_str_null()` when there’s no TLS). Adding an `ngx_strlen()` inside the getters themselves would simply duplicate work the existing callers already do.
