diff --git a/lib/roles/http/client/client-handshake.c b/lib/roles/http/client/client-handshake.c
index 70cb547198d9efcbfcc26ccf7c33f00c9303ef05..b84dac1d6b6b1d9d2eb38d462869cefe8f0614df 100644
--- a/lib/roles/http/client/client-handshake.c
+++ b/lib/roles/http/client/client-handshake.c
@@ -241,10 +241,9 @@ create_new_conn:
 
 #ifdef LWS_WITH_IPV6
 	if (wsi->ipv6) {
-		struct sockaddr_in6 *sa6 =
-				((struct sockaddr_in6 *)result->ai_addr);
+		struct sockaddr_in6 *sa6;
 
-		if (n) {
+		if (n || !result) {
 			/* lws_getaddrinfo46 failed, there is no usable result */
 			lwsl_notice("%s: lws_getaddrinfo46 failed %d\n",
 					__func__, n);
@@ -252,6 +251,8 @@ create_new_conn:
 			goto oom4;
 		}
 
+		sa6 = ((struct sockaddr_in6 *)result->ai_addr);
+
 		memset(&sa46, 0, sizeof(sa46));
 
 		sa46.sa6.sin6_family = AF_INET6;