Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bef02ab

Browse files
tniessentargos
authored andcommittedAug 11, 2018
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: #22131 Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]>
1 parent 3839ff0 commit bef02ab

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed
 

‎lib/internal/crypto/cipher.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Cipher.prototype.final = function final(outputEncoding) {
181181

182182

183183
Cipher.prototype.setAutoPadding = function setAutoPadding(ap) {
184-
if (this._handle.setAutoPadding(ap) === false)
184+
if (!this._handle.setAutoPadding(ap))
185185
throw new ERR_CRYPTO_INVALID_STATE('setAutoPadding');
186186
return this;
187187
};
@@ -200,10 +200,7 @@ Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
200200
['Buffer', 'TypedArray', 'DataView'],
201201
tagbuf);
202202
}
203-
// Do not do a normal falsy check because the method returns
204-
// undefined if it succeeds. Returns false specifically if it
205-
// errored
206-
if (this._handle.setAuthTag(tagbuf) === false)
203+
if (!this._handle.setAuthTag(tagbuf))
207204
throw new ERR_CRYPTO_INVALID_STATE('setAuthTag');
208205
return this;
209206
};
@@ -216,7 +213,7 @@ Cipher.prototype.setAAD = function setAAD(aadbuf, options) {
216213
}
217214

218215
const plaintextLength = getUIntOption(options, 'plaintextLength');
219-
if (this._handle.setAAD(aadbuf, plaintextLength) === false)
216+
if (!this._handle.setAAD(aadbuf, plaintextLength))
220217
throw new ERR_CRYPTO_INVALID_STATE('setAAD');
221218
return this;
222219
};

‎src/node_crypto.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,8 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
29262926

29272927
memset(cipher->auth_tag_, 0, sizeof(cipher->auth_tag_));
29282928
memcpy(cipher->auth_tag_, Buffer::Data(args[0]), cipher->auth_tag_len_);
2929+
2930+
args.GetReturnValue().Set(true);
29292931
}
29302932

29312933

@@ -2980,9 +2982,9 @@ void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
29802982
CHECK(args[1]->IsInt32());
29812983
int plaintext_len = args[1].As<Int32>()->Value();
29822984

2983-
if (!cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0]),
2984-
plaintext_len))
2985-
args.GetReturnValue().Set(false); // Report invalid state failure
2985+
bool b = cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0]),
2986+
plaintext_len);
2987+
args.GetReturnValue().Set(b); // Possibly report invalid state failure
29862988
}
29872989

29882990

@@ -3094,8 +3096,8 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
30943096
CipherBase* cipher;
30953097
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
30963098

3097-
if (!cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue()))
3098-
args.GetReturnValue().Set(false); // Report invalid state failure
3099+
bool b = cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
3100+
args.GetReturnValue().Set(b); // Possibly report invalid state failure
30993101
}
31003102

31013103

0 commit comments

Comments
 (0)
Please sign in to comment.