Why do ngx_ssl_get_protocol() and ngx_ssl_get_cipher_name() leave ngx_str_t->len uninitialized?

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?

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

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:

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

• The mail module sets it explicitly:

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.