Work around language and compiler bug in memcpy, etc.
Most C standard library functions are undefined if passed NULL, even
when the corresponding length is zero. This gives them (and, in turn,
all functions which call them) surprising behavior on empty arrays.
Some compilers will miscompile code due to this rule. See also
https://www.imperialviolet.org/2016/06/26/nonnull.html
Add OPENSSL_memcpy, etc., wrappers which avoid this problem.
BUG=23
Change-Id: I95f42b23e92945af0e681264fffaf578e7f8465e
Reviewed-on: https://boringssl-review.googlesource.com/12928
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/ssl/handshake_client.c b/ssl/handshake_client.c
index 8c21818..2791abc 100644
--- a/ssl/handshake_client.c
+++ b/ssl/handshake_client.c
@@ -167,6 +167,7 @@
#include <openssl/x509.h>
#include <openssl/x509v3.h>
+#include "../crypto/internal.h"
#include "internal.h"
@@ -815,7 +816,7 @@
goto f_err;
}
- memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
+ OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
ssl->d1->cookie_len = CBS_len(&cookie);
ssl->d1->send_cookie = 1;
@@ -913,7 +914,7 @@
}
/* Copy over the server random. */
- memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
+ OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
/* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
* 1.3 is finalized and we are not implementing a draft version. */
@@ -932,7 +933,7 @@
}
/* Note: session_id could be empty. */
ssl->s3->new_session->session_id_length = CBS_len(&session_id);
- memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
+ OPENSSL_memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
CBS_len(&session_id));
}
@@ -1515,7 +1516,7 @@
}
char identity[PSK_MAX_IDENTITY_LEN + 1];
- memset(identity, 0, sizeof(identity));
+ OPENSSL_memset(identity, 0, sizeof(identity));
psk_len =
ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
sizeof(identity), psk, sizeof(psk));
@@ -1616,7 +1617,7 @@
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
goto err;
}
- memset(pms, 0, pms_len);
+ OPENSSL_memset(pms, 0, pms_len);
} else {
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);