Skip to content

Commit

Permalink
src: refactor crypto code with RAII cleanup
Browse files Browse the repository at this point in the history
use more idiomatic expressions with RAII primitives,
instead of old style goto

PR-URL: #23014

Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
gireeshpunathil committed Sep 28, 2018
1 parent d228c4d commit 406e3ad
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/node_crypto_clienthello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}

Expand All @@ -116,10 +122,8 @@ void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) {
hello.servername_ = servername_;
hello.servername_size_ = static_cast<uint8_t>(servername_size_);
onhello_cb_(cb_arg_, hello);
failed = false;
return;

fail:
End();
}


Expand Down

0 comments on commit 406e3ad

Please sign in to comment.