Fix minor issues found by Clang's analysis.

Thanks to Denis Denisov for running the analysis.

Change-Id: I80810261e013423e746fd8d8afefb3581cffccc0
Reviewed-on: https://boringssl-review.googlesource.com/1701
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/bio/bio_mem.c b/crypto/bio/bio_mem.c
index 457c2e0..6e90db5 100644
--- a/crypto/bio/bio_mem.c
+++ b/crypto/bio/bio_mem.c
@@ -132,13 +132,12 @@
 }
 
 static int mem_read(BIO *bio, char *out, int outl) {
-  int ret = -1;
-  BUF_MEM *b;
+  int ret;
+  BUF_MEM *b = (BUF_MEM*) bio->ptr;
 
-  b = (BUF_MEM *)bio->ptr;
   BIO_clear_retry_flags(bio);
   ret = outl;
-  if (ret > (int)b->length) {
+  if (b->length < INT_MAX && ret > (int)b->length) {
     ret = b->length;
   }
 
diff --git a/crypto/ec/wnaf.c b/crypto/ec/wnaf.c
index b86107d..2fed4a5 100644
--- a/crypto/ec/wnaf.c
+++ b/crypto/ec/wnaf.c
@@ -448,8 +448,6 @@
         wNAF[num] = tmp_wNAF;
         wNAF[num + 1] = NULL;
         wNAF_len[num] = tmp_len;
-        if (tmp_len > max_len)
-          max_len = tmp_len;
         /* pre_comp->points starts with the points that we need here: */
         val_sub[num] = pre_comp->points;
       } else {
diff --git a/crypto/ecdsa/ecdsa.c b/crypto/ecdsa/ecdsa.c
index 067fd6c..ddc3e61 100644
--- a/crypto/ecdsa/ecdsa.c
+++ b/crypto/ecdsa/ecdsa.c
@@ -140,8 +140,9 @@
   }
 
   /* check input values */
-  if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
-      (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
+  if ((group = EC_KEY_get0_group(eckey)) == NULL ||
+      (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
+      sig == NULL) {
     OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_MISSING_PARAMETERS);
     return 0;
   }
diff --git a/crypto/evp/example_sign.c b/crypto/evp/example_sign.c
index 847330d..9d2a296 100644
--- a/crypto/evp/example_sign.c
+++ b/crypto/evp/example_sign.c
@@ -127,9 +127,13 @@
     fprintf(stderr, "sig_len mismatch\n");
     goto out;
   }
+
   sig = malloc(sig_len);
-  if (sig == NULL ||
-      EVP_DigestSignFinal(&md_ctx, sig, &sig_len) != 1) {
+  if (sig == NULL) {
+    goto out;
+  }
+  if (EVP_DigestSignFinal(&md_ctx, sig, &sig_len) != 1) {
+    free(sig);
     goto out;
   }
 
diff --git a/crypto/internal.h b/crypto/internal.h
index 39d35ad..65a52ed 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -111,6 +111,10 @@
 
 #include <openssl/ex_data.h>
 
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 
 /* st_CRYPTO_EX_DATA_IMPL contains an ex_data implementation. See the comments
  * in ex_data.h for details of the behaviour of each of the functions. */
diff --git a/include/openssl/base.h b/include/openssl/base.h
index bee4030..079b1c4 100644
--- a/include/openssl/base.h
+++ b/include/openssl/base.h
@@ -63,6 +63,11 @@
 
 #include <openssl/opensslfeatures.h>
 
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+
 #if defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64)
 #define OPENSSL_64_BIT
 #define OPENSSL_X86_64
@@ -205,4 +210,8 @@
 typedef void *OPENSSL_BLOCK;
 
 
+#if defined(__cplusplus)
+}  /* extern C */
+#endif
+
 #endif  /* OPENSSL_HEADER_BASE_H */
diff --git a/ssl/d1_both.c b/ssl/d1_both.c
index caa18d1..2d944d8 100644
--- a/ssl/d1_both.c
+++ b/ssl/d1_both.c
@@ -1165,7 +1165,6 @@
 	saved_state.write_hash = s->write_hash;
 	saved_state.session = s->session;
 	saved_state.epoch = s->d1->w_epoch;
-	saved_state.epoch = s->d1->w_epoch;
 	
 	s->d1->retransmitting = 1;
 	
diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c
index 44d26a7..97d1107 100644
--- a/ssl/s3_clnt.c
+++ b/ssl/s3_clnt.c
@@ -2229,9 +2229,8 @@
 
 			/* Free allocated memory */
 			BN_CTX_free(bn_ctx);
-			if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
-			if (clnt_ecdh != NULL)
-				 EC_KEY_free(clnt_ecdh);
+			OPENSSL_free(encodedPoint);
+			EC_KEY_free(clnt_ecdh);
 			EVP_PKEY_free(srvr_pub_pkey);
 			}
 		else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 58684b0..b8df1bf 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -1079,7 +1079,7 @@
 		return -1;
 		}
 
-	if ((s != NULL) && !SSL_in_init(s))
+	if (!SSL_in_init(s))
 		return(s->method->ssl_shutdown(s));
 	else
 		return(1);