Add malloc failure tests.

This commit fixes a number of crashes caused by malloc failures. They
were found using the -malloc-test=0 option to runner.go which runs tests
many times, causing a different allocation call to fail in each case.

(This test only works on Linux and only looks for crashes caused by
allocation failures, not memory leaks or other errors.)

This is not the complete set of crashes! More can be found by collecting
core dumps from running with -malloc-test=0.

Change-Id: Ia61d19f51e373bccb7bc604642c51e043a74bd83
Reviewed-on: https://boringssl-review.googlesource.com/2320
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/err/err.c b/crypto/err/err.c
index 6390b9a..6349a74 100644
--- a/crypto/err/err.c
+++ b/crypto/err/err.c
@@ -175,8 +175,7 @@
   uint32_t ret;
 
   state = err_get_state();
-
-  if (state->bottom == state->top) {
+  if (state == NULL || state->bottom == state->top) {
     return 0;
   }
 
@@ -282,6 +281,10 @@
   ERR_STATE *const state = err_get_state();
   unsigned i;
 
+  if (state == NULL) {
+    return;
+  }
+
   for (i = 0; i < ERR_NUM_ERRORS; i++) {
     err_clear(&state->errors[i]);
   }
@@ -481,7 +484,7 @@
   ERR_STATE *const state = err_get_state();
   struct err_error_st *error;
 
-  if (state->top == state->bottom) {
+  if (state == NULL || state->top == state->bottom) {
     if (flags & ERR_FLAG_MALLOCED) {
       OPENSSL_free(data);
     }
@@ -500,6 +503,10 @@
   ERR_STATE *const state = err_get_state();
   struct err_error_st *error;
 
+  if (state == NULL) {
+    return;
+  }
+
   if (library == ERR_LIB_SYS && reason == 0) {
 #if defined(WIN32)
     reason = GetLastError();
@@ -600,7 +607,7 @@
 int ERR_set_mark(void) {
   ERR_STATE *const state = err_get_state();
 
-  if (state->bottom == state->top) {
+  if (state == NULL || state->bottom == state->top) {
     return 0;
   }
   state->errors[state->top].flags |= ERR_FLAG_MARK;
@@ -611,6 +618,10 @@
   ERR_STATE *const state = err_get_state();
   struct err_error_st *error;
 
+  if (state == NULL) {
+    return 0;
+  }
+
   while (state->bottom != state->top) {
     error = &state->errors[state->top];