diff --git a/mongoose.c b/mongoose.c index 4abdf2fc..45a24966 100644 --- a/mongoose.c +++ b/mongoose.c @@ -21153,8 +21153,8 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { mg_error(c, "hostname too long"); return; } - strncpy((char *) tls->hostname, opts->name.buf, sizeof(tls->hostname) - 1); - tls->hostname[opts->name.len] = 0; + memcpy(tls->hostname, opts->name.buf, opts->name.len); + tls->hostname[opts->name.len] = '\0'; } // server CA certificate; parse PEM [bundle] or DER diff --git a/src/tls_builtin.c b/src/tls_builtin.c index 65acd2a4..815096b8 100644 --- a/src/tls_builtin.c +++ b/src/tls_builtin.c @@ -2748,8 +2748,8 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) { mg_error(c, "hostname too long"); return; } - strncpy((char *) tls->hostname, opts->name.buf, sizeof(tls->hostname) - 1); - tls->hostname[opts->name.len] = 0; + memcpy(tls->hostname, opts->name.buf, opts->name.len); + tls->hostname[opts->name.len] = '\0'; } // server CA certificate; parse PEM [bundle] or DER