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?
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:
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.