From 4bd3b6e33283537ce94618cd767620f82d6c3150 Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Sat, 22 Sep 2018 03:43:34 -0400 Subject: [PATCH] src: refactor crypto code with RAII cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit use more idiomatic expressions with RAII primitives, instead of old style goto PR-URL: https://github.com/nodejs/node/pull/23014 Reviewed-By: Daniel Bevenius Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Tobias Nießen Reviewed-By: Joyee Cheung Reviewed-By: Ruben Bridgewater --- src/node_crypto_clienthello.cc | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/node_crypto_clienthello.cc b/src/node_crypto_clienthello.cc index d4d222f838c5ac..4df0d934829a07 100644 --- a/src/node_crypto_clienthello.cc +++ b/src/node_crypto_clienthello.cc @@ -74,6 +74,12 @@ bool ClientHelloParser::ParseRecordHeader(const uint8_t* data, size_t avail) { void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) { ClientHello hello; + bool failed = true; + + OnScopeLeave cleanup([&]() { + if (failed) + End(); + }); // >= 5 + frame size bytes for frame parsing if (body_offset_ + frame_len_ > avail) @@ -88,23 +94,23 @@ void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) { if (data[body_offset_ + 4] != 0x03 || data[body_offset_ + 5] < 0x01 || data[body_offset_ + 5] > 0x03) { - goto fail; + return; } if (data[body_offset_] == kClientHello) { if (state_ == kTLSHeader) { if (!ParseTLSClientHello(data, avail)) - goto fail; + return; } else { // We couldn't get here, but whatever - goto fail; + return; } // Check if we overflowed (do not reply with any private data) if (session_id_ == nullptr || session_size_ > 32 || session_id_ + session_size_ > data + avail) { - goto fail; + return; } } @@ -116,10 +122,8 @@ void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) { hello.servername_ = servername_; hello.servername_size_ = static_cast(servername_size_); onhello_cb_(cb_arg_, hello); + failed = false; return; - - fail: - End(); }