Browse Source

crypto: don't assert when calling Cipher#final() twice

Remove the assert() that triggered when Cipher#final() or
Decipher#final() was called twice.

Fixes #4886.
v0.8.22-release
Ben Noordhuis 12 years ago
parent
commit
7189b3ed33
  1. 11
      src/node_crypto.cc
  2. 13
      test/simple/test-crypto.js

11
src/node_crypto.cc

@ -2301,11 +2301,7 @@ class Cipher : public ObjectWrap {
int r = cipher->CipherFinal(&out_value, &out_len);
assert(out_value != NULL);
assert(out_len != -1 || r == 0);
if (out_len == 0 || r == 0) {
// out_value always get allocated.
if (out_len <= 0 || r == 0) {
delete[] out_value;
if (r == 0) {
Local<Value> exception = Exception::TypeError(
@ -2756,10 +2752,7 @@ class Decipher : public ObjectWrap {
int r = cipher->DecipherFinal<TOLERATE_PADDING>(&out_value, &out_len);
assert(out_value != NULL);
assert(out_len != -1);
if (out_len == 0 || r == 0) {
if (out_len <= 0 || r == 0) {
delete [] out_value; // allocated even if out_len == 0
if (r == 0) {
Local<Value> exception = Exception::TypeError(

13
test/simple/test-crypto.js

@ -665,3 +665,16 @@ crypto.pbkdf2('pass\0word', 'sa\0lt', 4096, 16, function(err, result) {
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, 20, null);
});
// Calling Cipher.final() or Decipher.final() twice should error but
// not assert. See #4886.
(function() {
var c = crypto.createCipher('aes-256-cbc', 'secret');
try { c.final('xxx') } catch (e) { /* Ignore. */ }
try { c.final('xxx') } catch (e) { /* Ignore. */ }
try { c.final('xxx') } catch (e) { /* Ignore. */ }
var d = crypto.createDecipher('aes-256-cbc', 'secret');
try { d.final('xxx') } catch (e) { /* Ignore. */ }
try { d.final('xxx') } catch (e) { /* Ignore. */ }
try { d.final('xxx') } catch (e) { /* Ignore. */ }
})();

Loading…
Cancel
Save