From b80327ea49ec3a2ac2f56ac2a2c29f1c7f7ef201 Mon Sep 17 00:00:00 2001
From: Patrick McDermott <patrick.mcdermott@libiquity.com>
Date: Tue, 30 Jul 2019 03:47:46 -0400
Subject: s_client: Simplify error handling

As many source lines as this removes, here's the binary size difference:

	 wolfssl-util:     file format elf64-x86-64

	 Sections:
	 Idx Name          Size      VMA               LMA               File off  Algn
	   0 .interp       0000001c  0000000000000238  0000000000000238  00000238  2**0
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   1 .note.ABI-tag 00000020  0000000000000254  0000000000000254  00000254  2**2
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   2 .note.gnu.build-id 00000024  0000000000000274  0000000000000274  00000274  2**2
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   3 .gnu.hash     0000003c  0000000000000298  0000000000000298  00000298  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   4 .dynsym       00000450  00000000000002d8  00000000000002d8  000002d8  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   5 .dynstr       000002ab  0000000000000728  0000000000000728  00000728  2**0
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   6 .gnu.version  0000005c  00000000000009d4  00000000000009d4  000009d4  2**1
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   7 .gnu.version_r 00000020  0000000000000a30  0000000000000a30  00000a30  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   8 .rela.dyn     00000108  0000000000000a50  0000000000000a50  00000a50  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	   9 .rela.plt     00000318  0000000000000b58  0000000000000b58  00000b58  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	  10 .init         00000017  0000000000000e70  0000000000000e70  00000e70  2**2
	                   CONTENTS, ALLOC, LOAD, READONLY, CODE
	  11 .plt          00000220  0000000000000e90  0000000000000e90  00000e90  2**4
	                   CONTENTS, ALLOC, LOAD, READONLY, CODE
	  12 .plt.got      00000008  00000000000010b0  00000000000010b0  000010b0  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, CODE
	- 13 .text         00000782  00000000000010c0  00000000000010c0  000010c0  2**4
	+ 13 .text         00000792  00000000000010c0  00000000000010c0  000010c0  2**4
	                   CONTENTS, ALLOC, LOAD, READONLY, CODE
	- 14 .fini         00000009  0000000000001844  0000000000001844  00001844  2**2
	+ 14 .fini         00000009  0000000000001854  0000000000001854  00001854  2**2
	                   CONTENTS, ALLOC, LOAD, READONLY, CODE
	- 15 .rodata       000001a6  0000000000001850  0000000000001850  00001850  2**3
	+ 15 .rodata       000001a6  0000000000001860  0000000000001860  00001860  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	- 16 .eh_frame_hdr 00000044  00000000000019f8  00000000000019f8  000019f8  2**2
	+ 16 .eh_frame_hdr 00000044  0000000000001a08  0000000000001a08  00001a08  2**2
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	- 17 .eh_frame     00000184  0000000000001a40  0000000000001a40  00001a40  2**3
	+ 17 .eh_frame     00000184  0000000000001a50  0000000000001a50  00001a50  2**3
	                   CONTENTS, ALLOC, LOAD, READONLY, DATA
	  18 .init_array   00000008  0000000000201dc8  0000000000201dc8  00001dc8  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  19 .fini_array   00000008  0000000000201dd0  0000000000201dd0  00001dd0  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  20 .jcr          00000008  0000000000201dd8  0000000000201dd8  00001dd8  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  21 .dynamic      000001f0  0000000000201de0  0000000000201de0  00001de0  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  22 .got          00000030  0000000000201fd0  0000000000201fd0  00001fd0  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  23 .got.plt      00000120  0000000000202000  0000000000202000  00002000  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  24 .data         00000018  0000000000202120  0000000000202120  00002120  2**3
	                   CONTENTS, ALLOC, LOAD, DATA
	  25 .bss          00000010  0000000000202140  0000000000202140  00002138  2**5
	                   ALLOC
	  26 .comment      00000026  0000000000000000  0000000000000000  00002138  2**0
	                   CONTENTS, READONLY

16 more bytes in the .text section.  Oh well.
---
(limited to 'src')

diff --git a/src/s_client.c b/src/s_client.c
index 51f0adb..b2a0f06 100644
--- a/src/s_client.c
+++ b/src/s_client.c
@@ -278,11 +278,11 @@ s_client(int argc, char **argv)
 	char           *host       = NULL;
 	char           *port       = NULL;
 	const char     *servername = NULL;
-	int             ret        = EXIT_SUCCESS;
+	int             ret        = EXIT_FAILURE;
 	WOLFSSL_METHOD *method;
-	WOLFSSL_CTX    *ctx;
-	WOLFSSL        *ssl;
-	int             sfd;
+	WOLFSSL_CTX    *ctx        = NULL;
+	WOLFSSL        *ssl        = NULL;
+	int             sfd        = -1;
 	int             err;
 	char            buf[WOLFSSL_MAX_ERROR_SZ];
 #ifdef OPENSSL_EXTRA
@@ -310,30 +310,27 @@ s_client(int argc, char **argv)
 	method = wolfTLSv1_2_client_method();
 	if (method == NULL) {
 		fputs("Out of memory\n", stderr);
-		ret = EXIT_FAILURE;
-		goto cleanup;
+		goto error;
 	}
 
 	ctx = wolfSSL_CTX_new(method);
 	if (ctx == NULL) {
 		fputs("Out of memory\n", stderr);
-		ret = EXIT_FAILURE;
-		goto cleanup;
+		goto error;
 	}
 
 	if (wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, CA_CERTS,
 				WOLFSSL_LOAD_FLAG_IGNORE_ERR) !=
 			WOLFSSL_SUCCESS) {
 		fputs("Failed to load CA certificates\n", stderr);
-		ret = EXIT_FAILURE;
-		goto ctx_free;
+		goto error;
 	}
 
 #ifdef HAVE_OCSP
 	if (wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL) !=
 			WOLFSSL_SUCCESS) {
 		fputs("Failed to enable OCSP\n", stderr);
-		goto ctx_free;
+		goto error;
 	}
 #endif
 #ifdef HAVE_SNI
@@ -341,8 +338,7 @@ s_client(int argc, char **argv)
 		if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, servername,
 					strlen(servername)) != WOLFSSL_SUCCESS){
 			fputs("Out of memory\n", stderr);
-			ret = EXIT_FAILURE;
-			goto ctx_free;
+			goto error;
 		}
 	}
 #else
@@ -351,22 +347,19 @@ s_client(int argc, char **argv)
 #ifdef HAVE_SUPPORTED_CURVES
 	if (use_curves(ctx) == false) {
 		fputs("Out of memory\n", stderr);
-		ret = EXIT_FAILURE;
-		goto ctx_free;
+		goto error;
 	}
 #endif
 
 	ssl = wolfSSL_new(ctx);
 	if (ssl == NULL) {
 		fputs("Out of memory\n", stderr);
-		ret = EXIT_FAILURE;
-		goto ctx_free;
+		goto error;
 	}
 
 	sfd = connect_socket(host, port);
 	if (sfd == -1) {
-		ret = EXIT_FAILURE;
-		goto ssl_free;
+		goto error;
 	}
 	wolfSSL_set_fd(ssl, sfd);
 
@@ -374,36 +367,31 @@ s_client(int argc, char **argv)
 		err = wolfSSL_get_error(ssl, err);
 		wolfSSL_ERR_error_string(err, buf);
 		fprintf(stderr, "Handshake error: %s\n", buf);
-		ret = EXIT_FAILURE;
-		goto ssl_free;
+		goto error;
 	}
 
 #ifdef OPENSSL_EXTRA
 	cert = wolfSSL_get_peer_certificate(ssl);
 	if (cert == NULL) {
 		fputs("Failed to get certificate\n", stderr);
-		ret = EXIT_FAILURE;
-		goto ssl_free;
+		goto error;
 	}
 	if (wolfSSL_X509_check_host(cert, host, strlen(host), 0, NULL) !=
 			WOLFSSL_SUCCESS) {
 		fputs("Domain name mismatch\n", stderr);
-		ret = EXIT_FAILURE;
-		goto ssl_free;
+		goto error;
 	}
 #endif  /* OPENSSL_EXTRA */
 
 	if (poll_fds(sfd, ssl) == false) {
-		ret = EXIT_FAILURE;
+		goto error;
 	}
 
+	ret = EXIT_SUCCESS;
+error:
 	close(sfd);
-
-ssl_free:
 	wolfSSL_free(ssl);
-ctx_free:
 	wolfSSL_CTX_free(ctx);
-cleanup:
 	wolfSSL_Cleanup();
 
 	return ret;
--
cgit v0.9.1