From 8630c134a87329ee8d6873695266ae87d5468ffe Mon Sep 17 00:00:00 2001 From: Jay Weisskopf Date: Wed, 4 Jan 2012 03:06:53 -0600 Subject: [PATCH] Check for null pointers from gethostbyname_r() to prevent seg faults. --- libhttp/ssl.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) mode change 100644 => 100755 libhttp/ssl.c diff --git a/libhttp/ssl.c b/libhttp/ssl.c old mode 100644 new mode 100755 index aec3e0b..8e3ae02 --- a/libhttp/ssl.c +++ b/libhttp/ssl.c @@ -672,12 +672,15 @@ void sslSetCertificate(struct SSLSupport *ssl, const char *filename, char hostname[256], buf[4096]; check(!gethostname(hostname, sizeof(hostname))); struct hostent he_buf, *he; - int h_err; - if (gethostbyname_r(hostname, &he_buf, buf, sizeof(buf), - &he, &h_err)) { - sslGenerateCertificate(defaultCertificate, hostname); - } else { + int h_err = 0; + int ret = gethostbyname_r(hostname, &he_buf, buf, sizeof(buf), &he, &h_err); + if (!ret && he && he->h_name) { sslGenerateCertificate(defaultCertificate, he->h_name); + } else { + if (h_err) { + warn("Error getting host information: \"%s\".", hstrerror(h_err)); + } + sslGenerateCertificate(defaultCertificate, hostname); } } else { goto valid_certificate;