Skip to content

Commit 77ac962

Browse files
tniessentrivikr
authored andcommitted
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 7e29453 commit 77ac962

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
@@ -2939,6 +2939,8 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
29392939

29402940
memset(cipher->auth_tag_, 0, sizeof(cipher->auth_tag_));
29412941
memcpy(cipher->auth_tag_, Buffer::Data(args[0]), cipher->auth_tag_len_);
2942+
2943+
args.GetReturnValue().Set(true);
29422944
}
29432945

29442946

@@ -2993,9 +2995,9 @@ void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
29932995
CHECK(args[1]->IsInt32());
29942996
int plaintext_len = args[1].As<Int32>()->Value();
29952997

2996-
if (!cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0]),
2997-
plaintext_len))
2998-
args.GetReturnValue().Set(false); // Report invalid state failure
2998+
bool b = cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0]),
2999+
plaintext_len);
3000+
args.GetReturnValue().Set(b); // Possibly report invalid state failure
29993001
}
30003002

30013003

@@ -3107,8 +3109,8 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
31073109
CipherBase* cipher;
31083110
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
31093111

3110-
if (!cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue()))
3111-
args.GetReturnValue().Set(false); // Report invalid state failure
3112+
bool b = cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
3113+
args.GetReturnValue().Set(b); // Possibly report invalid state failure
31123114
}
31133115

31143116

0 commit comments

Comments
 (0)