crypto: simplify state failure handling

It is more intuitive to return true/false instead of undefined/false
and also simplifies handling in the JS layer.

PR-URL: https://github.com/nodejs/node/pull/22131
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
This commit is contained in:
Tobias Nießen 2018-08-04 23:52:37 +02:00 committed by Trivikram Kamat
parent 7e29453d2f
commit 77ac9628b3
2 changed files with 10 additions and 11 deletions

View File

@ -181,7 +181,7 @@ Cipher.prototype.final = function final(outputEncoding) {
Cipher.prototype.setAutoPadding = function setAutoPadding(ap) { Cipher.prototype.setAutoPadding = function setAutoPadding(ap) {
if (this._handle.setAutoPadding(ap) === false) if (!this._handle.setAutoPadding(ap))
throw new ERR_CRYPTO_INVALID_STATE('setAutoPadding'); throw new ERR_CRYPTO_INVALID_STATE('setAutoPadding');
return this; return this;
}; };
@ -200,10 +200,7 @@ Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
['Buffer', 'TypedArray', 'DataView'], ['Buffer', 'TypedArray', 'DataView'],
tagbuf); tagbuf);
} }
// Do not do a normal falsy check because the method returns if (!this._handle.setAuthTag(tagbuf))
// undefined if it succeeds. Returns false specifically if it
// errored
if (this._handle.setAuthTag(tagbuf) === false)
throw new ERR_CRYPTO_INVALID_STATE('setAuthTag'); throw new ERR_CRYPTO_INVALID_STATE('setAuthTag');
return this; return this;
}; };
@ -216,7 +213,7 @@ Cipher.prototype.setAAD = function setAAD(aadbuf, options) {
} }
const plaintextLength = getUIntOption(options, 'plaintextLength'); const plaintextLength = getUIntOption(options, 'plaintextLength');
if (this._handle.setAAD(aadbuf, plaintextLength) === false) if (!this._handle.setAAD(aadbuf, plaintextLength))
throw new ERR_CRYPTO_INVALID_STATE('setAAD'); throw new ERR_CRYPTO_INVALID_STATE('setAAD');
return this; return this;
}; };

View File

@ -2939,6 +2939,8 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
memset(cipher->auth_tag_, 0, sizeof(cipher->auth_tag_)); memset(cipher->auth_tag_, 0, sizeof(cipher->auth_tag_));
memcpy(cipher->auth_tag_, Buffer::Data(args[0]), cipher->auth_tag_len_); memcpy(cipher->auth_tag_, Buffer::Data(args[0]), cipher->auth_tag_len_);
args.GetReturnValue().Set(true);
} }
@ -2993,9 +2995,9 @@ void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsInt32()); CHECK(args[1]->IsInt32());
int plaintext_len = args[1].As<Int32>()->Value(); int plaintext_len = args[1].As<Int32>()->Value();
if (!cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0]), bool b = cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0]),
plaintext_len)) plaintext_len);
args.GetReturnValue().Set(false); // Report invalid state failure args.GetReturnValue().Set(b); // Possibly report invalid state failure
} }
@ -3107,8 +3109,8 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
CipherBase* cipher; CipherBase* cipher;
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder()); ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
if (!cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue())) bool b = cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
args.GetReturnValue().Set(false); // Report invalid state failure args.GetReturnValue().Set(b); // Possibly report invalid state failure
} }