src: more automatic memory management in node_crypto.cc#20238
src: more automatic memory management in node_crypto.cc#20238addaleax wants to merge 1 commit intonodejs:masterfrom
Conversation
tniessen
left a comment
There was a problem hiding this comment.
Thanks Anna, this looks great! I will have a closer look soon.
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
I might be missing something here, but doesn't this negate the check?
There was a problem hiding this comment.
@tniessen Yes, good catch. I don’t think we have tests for this at the moment. :/
bnoordhuis
left a comment
There was a problem hiding this comment.
Left some comments, didn't review fully. I like the overall direction.
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
Coincidence: I was working yesterday on simplifying this to something that uses std::vector<char>.
(Working on adding scrypt support and DRYing the PBKDF2 and RandomBytes code in the process.)
src/node_crypto.h
Outdated
src/node_crypto.h
Outdated
There was a problem hiding this comment.
Can you name this Ptr, Pointer, UniquePointer or whatever to make it clearer it's a type?
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
I suppose this works because of boolean coercion but it might be clearer to write it as !!store_ctx or store_ctx == true or store_ctx.get() != nullptr.
|
Fixed tests (not sure why I didn’t see the failures before) and addressed @bnoordhuis’ first round of review nits. |
src/node_crypto.h
Outdated
There was a problem hiding this comment.
Is there a reason not to reuse EVPMDPointer?
|
CI: https://ci.nodejs.org/job/node-test-commit/18082/ Would anybody in particular like me to wait with landing until they have time to review? I know it’s a large commit, and not urgent, so I really don’t want to push this to land early. |
bnoordhuis
left a comment
There was a problem hiding this comment.
Needs a rebase (I'm guessing it conflicts with the EVP_CTRL_AEAD_SET_IVLEN change) but LGTM modulo comments. This PR makes the crypto code a lot nicer!
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
s/ENGINE_free_fn/ENGINE_free/ in the function body.
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
Just a suggestion since this isn't a functional change but it's correcter to use assignment here, like this:
std::vector<char> sbuf;
if (char* p = Buffer::Data(args[0])) sbuf.assign(p, p + slen);The reason is that Buffer::Data() can return nullptr when slen == 0 and passing a nullptr to memcpy() is technically UB, even when the pointer isn't dereferenced.
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
Fits on one line now. (Likewise on lines 3289 and 3378, I think.)
src/node_crypto.cc
Outdated
There was a problem hiding this comment.
Maybe use automatic memory management for this as well (and rename dataSize to data_size while you're here)? Just a suggestion.
There was a problem hiding this comment.
Good idea – I’ve switched this to MallocedBuffer, too (that takes care of dataSize anyway)
src/node_crypto.cc
Outdated
src/node_crypto.cc
Outdated
src/util.h
Outdated
There was a problem hiding this comment.
Style: new(this)
Don't know if it matters here but use of placement new sometimes inhibits compiler optimizations.
There was a problem hiding this comment.
Don't know if it matters here but use of placement new sometimes inhibits compiler optimizations.
I don’t think we actually use it at this point, so this is just here for completeness
src/util.h
Outdated
There was a problem hiding this comment.
Is this class necessary? A std::unique_ptr with a custom deleter would be sufficient too, wouldn't it?
There was a problem hiding this comment.
@bnoordhuis Yes, the difference being that this also tracks the length of the buffer by itself. If you feel strongly about it I can switch to splitting up into two variables again
|
Rerunning Windows: https://ci.nodejs.org/job/node-test-commit-windows-fanned/17684/ |
|
Rebased CI before landing: https://ci.nodejs.org/job/node-test-commit/18218/ |
|
Another day, another merge conflict, another rebase. :) CI: https://ci.nodejs.org/job/node-test-commit/18232/ |
Prefer custom smart pointers fitted to the OpenSSL data structures over more manual memory management and lots of `goto`s. PR-URL: nodejs#20238 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Prefer custom smart pointers fitted to the OpenSSL data structures over more manual memory management and lots of `goto`s. Backport-PR-URL: #20609 PR-URL: #20238 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Prefer custom smart pointers fitted to the OpenSSL data structures over more manual memory management and lots of `goto`s. PR-URL: nodejs#20238 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Prefer custom smart pointers fitted to the OpenSSL data structures over more manual memory management and lots of `goto`s. Backport-PR-URL: #20706 PR-URL: #20238 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Prefer custom smart pointers fitted to the OpenSSL data structures
over more manual memory management and lots of
gotos.Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes