Add a helper function for resetting SSL_get_error state. We repeat this in a bunch of places. Change-Id: Iee2c95a13e1645453f101d8be4be9ac78d520387 Reviewed-on: https://boringssl-review.googlesource.com/13051 Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/ssl/d1_lib.c b/ssl/d1_lib.c index ba3c844..258e9ab 100644 --- a/ssl/d1_lib.c +++ b/ssl/d1_lib.c
@@ -236,9 +236,7 @@ } int DTLSv1_handle_timeout(SSL *ssl) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); + ssl_reset_error_state(ssl); if (!SSL_is_dtls(ssl)) { return -1;
diff --git a/ssl/internal.h b/ssl/internal.h index 97ae6a0..e26fa13 100644 --- a/ssl/internal.h +++ b/ssl/internal.h
@@ -1946,6 +1946,9 @@ void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock); +/* ssl_reset_error_state resets state for |SSL_get_error|. */ +void ssl_reset_error_state(SSL *ssl); + #if defined(__cplusplus) } /* extern C */
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c index 806bdba..2e9f4a6 100644 --- a/ssl/ssl_lib.c +++ b/ssl/ssl_lib.c
@@ -612,11 +612,16 @@ return ssl->wbio; } -int SSL_do_handshake(SSL *ssl) { +void ssl_reset_error_state(SSL *ssl) { + /* Functions which use |SSL_get_error| must reset I/O and error state on + * entry. */ ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ ERR_clear_error(); ERR_clear_system_error(); +} + +int SSL_do_handshake(SSL *ssl) { + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); @@ -735,10 +740,7 @@ } static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); - ERR_clear_system_error(); + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); @@ -784,10 +786,7 @@ } int SSL_write(SSL *ssl, const void *buf, int num) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); - ERR_clear_system_error(); + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); @@ -815,10 +814,7 @@ } int SSL_shutdown(SSL *ssl) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); - ERR_clear_system_error(); + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);