Free more memory in cleanup functions.

The extra free in ex_data_impl.c is fixing a mistake: when calling
|CRYPTO_cleanup_all_ex_data| the |EX_CLASS_ITEM| itself wouldn't be
freed.

The change in err_impl.c is to free the thread-id hash also. This allows
programs to free absolutely all memory allocated by BoringSSL, which
allows fuzz testing to find any memory leaks.

Change-Id: I1e518adf2b3e0efa7d7f00f7ab4e65e1dc70161e
Reviewed-on: https://boringssl-review.googlesource.com/2670
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/err/err_impl.c b/crypto/err/err_impl.c
index 7428fb7..e448d65 100644
--- a/crypto/err/err_impl.c
+++ b/crypto/err/err_impl.c
@@ -293,6 +293,10 @@
     lh_ERR_STRING_DATA_free(error_hash);
     error_hash = NULL;
   }
+  if (state_hash) {
+    lh_ERR_STATE_free(state_hash);
+    state_hash = NULL;
+  }
   CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
 }
 
diff --git a/crypto/ex_data_impl.c b/crypto/ex_data_impl.c
index cac1daa..f55b369 100644
--- a/crypto/ex_data_impl.c
+++ b/crypto/ex_data_impl.c
@@ -167,6 +167,7 @@
  * structures. */
 static void class_free(EX_CLASS_ITEM *item) {
   sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, data_funcs_free);
+  OPENSSL_free(item);
 }
 
 static LHASH_OF(EX_CLASS_ITEM) *get_classes(void) {
diff --git a/include/openssl/err.h b/include/openssl/err.h
index 071c5ba..8e99767 100644
--- a/include/openssl/err.h
+++ b/include/openssl/err.h
@@ -148,8 +148,10 @@
  * human-readable strings. */
 OPENSSL_EXPORT void ERR_load_crypto_strings(void);
 
-/* ERR_free_strings frees any internal error values that have been loaded. This
- * should only be called at process shutdown. */
+/* ERR_free_strings frees any memory retained by the error system, expect for
+ * per-thread structures which are assumed to have already been freed with
+ * |ERR_remove_thread_state|. This should only be called at process
+ * shutdown. */
 OPENSSL_EXPORT void ERR_free_strings(void);