Repository: nginx/nginx
File: src/http/ngx_http_upstream.c (C)

Code before:
    } else {
        preserve = 0;
    }

    conf->ssl_passwords = ngx_ssl_preserve_passwords(cf, conf->ssl_passwords);
    if (conf->ssl_passwords == NULL) {
        return NGX_ERROR;
    }

    /*
     * special handling to keep a preserved ssl_passwords copy
     * in the previous configuration to inherit it to all children
     */

    if (preserve) {
        prev->ssl_passwords = conf->ssl_passwords;
    }

    return NGX_OK;
}

#endif


static void *
ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
{
    ngx_http_upstream_main_conf_t  *umcf;

    umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
    if (umcf == NULL) {
        return NULL;
    }

    if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
                       sizeof(ngx_http_upstream_srv_conf_t *))
        != NGX_OK)
    {
        return NULL;
    }

    return umcf;
}


static char *
ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
{
    ngx_http_upstream_main_conf_t  *umcf = conf;

    ngx_uint_t                      i;

Review comment (suggestion):
Regarding making this a merge function, here's my take on how this could be done. I integrated the merge code into this function.
```C
ngx_int_t                                                                        
ngx_http_upstream_merge_ssl_passwords(ngx_conf_t *cf,                         
    ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev)              
{                                                                                
    ngx_uint_t  preserve;                                                        
                                                                                 
    if (conf->ssl_passwords == NGX_CONF_UNSET_PTR) {                             
        conf->ssl_passwords = (prev->ssl_passwords == NGX_CONF_UNSET_PTR         
                               ? NULL : prev->ssl_passwords);                    
                                                                                 
        if (conf->ssl_passwords && conf->ssl_passwords->pool != cf->temp_pool) { 
            return NGX_OK;                                                       
        }
                                                                      
        preserve = 1;                                                            
                                                                                 
    } else {                                                                     
        preserve = 0;                                                            
    }                                                                            
                                                                                 
    if (conf->ssl_certificate == NULL                                            
        || conf->ssl_certificate_key == NULL                                     
        || conf->ssl_certificate->value.len == 0                                 
        || (conf->ssl_certificate->lengths == NULL                               
            && conf->ssl_certificate_key->lengths == NULL))                      
    {                                                                            
        return NGX_OK;                                                           
    }                                                                            
                                                                                 
    conf->ssl_passwords = ngx_ssl_preserve_passwords(cf, conf->ssl_passwords);   
    if (conf->ssl_passwords == NULL) {                                           
        return NGX_ERROR;                                                        
    }                                                                            
                                                                                 
    /*                                                                           
     * special handling to keep a preserved ssl_passwords copy                   
     * in the previous configuration to inherit it to all children               
     */                                                                          
                                                                                 
    if (preserve) {                                                              
        prev->ssl_passwords = conf->ssl_passwords;                               
    }                                                                            
                                                                                 
    return NGX_OK;                                                               
}
```

Code after:
        return NGX_OK;
    }

    if (conf->ssl_passwords && conf->ssl_passwords->pool != cf->temp_pool) {
        /* already preserved */
        return NGX_OK;
    }

    preserve = (conf->ssl_passwords == prev->ssl_passwords) ? 1 : 0;

    conf->ssl_passwords = ngx_ssl_preserve_passwords(cf, conf->ssl_passwords);
    if (conf->ssl_passwords == NULL) {
        return NGX_ERROR;
    }

    /*
     * special handling to keep a preserved ssl_passwords copy
     * in the previous configuration to inherit it to all children
     */

    if (preserve) {
        prev->ssl_passwords = conf->ssl_passwords;
    }

    return NGX_OK;
}

#endif


static void *
ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
{
    ngx_http_upstream_main_conf_t  *umcf;

    umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
    if (umcf == NULL) {
        return NULL;
    }

    if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
                       sizeof(ngx_http_upstream_srv_conf_t *))
        != NGX_OK)
    {
        return NULL;
    }

    return umcf;
}