Skip to content

Commit f633a8e

Browse files
committed
Fail early if "trusted_certificate" is a directory.
Previously, the error was caused by enormous std::string allocation.
1 parent c9136f2 commit f633a8e

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/http_module.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,8 @@ char* addResourceAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
711711
return NGX_CONF_OK;
712712
}
713713

714-
char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) {
714+
char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
715+
{
715716
auto path = ((ngx_str_t*)cf->args->elts)[1];
716717
auto mcf = getMainConf(cf);
717718

@@ -727,11 +728,13 @@ char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) {
727728
return (char*)NGX_CONF_ERROR;
728729
}
729730
file.exceptions(std::ios::failbit | std::ios::badbit);
730-
file.seekg(0, std::ios::end);
731-
size_t size = file.tellg();
732-
mcf->trustedCert.resize(size);
731+
file.peek(); // trigger early error for dirs
732+
733+
size_t size = file.seekg(0, std::ios::end).tellg();
733734
file.seekg(0);
734-
file.read(&mcf->trustedCert[0], mcf->trustedCert.size());
735+
736+
mcf->trustedCert.resize(size);
737+
file.read(&mcf->trustedCert[0], size);
735738
} catch (const std::exception& e) {
736739
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
737740
"failed to read \"%V\": %s", &path, e.what());

0 commit comments

Comments
 (0)