netutils/thttpd: Apply the same check to related allocation sites

Extend the allocation-failure check to httpd_strdup(), the thttpd-local wrapper that has the same failure contract as strdup().

This is logically separable from the direct strdup()/asprintf() fixes: it prevents using hs->hostname before checking whether the wrapped string allocation succeeded, and releases the partially allocated server state on failure.

The scope intentionally does not extend to malloc(), calloc(), or other raw allocators because #1727 specifically calls out strdup()/asprintf(), and covering all allocation APIs would make this PR much broader.

Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
This commit is contained in:
Nightt 2026-05-23 10:24:19 +08:00 committed by Matteo Golin
parent c44f9a0556
commit 8568013436

View file

@ -2163,14 +2163,15 @@ FAR httpd_server *httpd_initialize(FAR httpd_sockaddr *sa)
#else
hs->hostname = httpd_strdup(httpd_ntoa(sa));
#endif
ninfo("hostname: %s\n", hs->hostname);
if (!hs->hostname)
{
nerr("ERROR: out of memory copying hostname\n");
free_httpd_server(hs);
return NULL;
}
ninfo("hostname: %s\n", hs->hostname);
hs->cgi_count = 0;
/* Initialize listen sockets */