Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions ext/openssl/ossl_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,19 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
}
out_len = in_len + EVP_MAX_BLOCK_LENGTH;

if (NIL_P(str)) {
str = rb_str_new(0, out_len);
} else {
if (NIL_P(str))
str = rb_str_buf_new(out_len);
else {
StringValue(str);
if ((long)rb_str_capacity(str) >= out_len)
rb_str_modify(str);
else
rb_str_modify_expand(str, out_len - RSTRING_LEN(str));
}

if (!ossl_cipher_update_long(ctx, (unsigned char *)RSTRING_PTR(str), &out_len, in, in_len))
ossl_raise(eCipherError, NULL);
assert(out_len <= RSTRING_LEN(str));
if (!ossl_cipher_update_long(ctx, (unsigned char *)RSTRING_PTR(str),
&out_len, in, in_len))
ossl_raise(eCipherError, "EVP_CipherUpdate");
rb_str_set_len(str, out_len);

return str;
Expand Down Expand Up @@ -456,7 +456,6 @@ ossl_cipher_final(VALUE self)
ossl_raise(eCipherError, "cipher final failed");
}
}
assert(out_len <= RSTRING_LEN(str));
rb_str_set_len(str, out_len);

return str;
Expand Down
13 changes: 7 additions & 6 deletions test/openssl/test_cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ def test_ctr_if_exists
def test_update_with_buffer
cipher = OpenSSL::Cipher.new("aes-128-ecb").encrypt
cipher.random_key
expected = cipher.update("data") << cipher.final
assert_equal 16, expected.bytesize
expected = cipher.update("data" * 10) << cipher.final
assert_equal 48, expected.bytesize

# Buffer is supplied
cipher.reset
buf = String.new
assert_same buf, cipher.update("data", buf)
assert_same buf, cipher.update("data" * 10, buf)
assert_equal 32, buf.bytesize
assert_equal expected, buf + cipher.final

# Buffer is frozen
Expand All @@ -149,9 +150,9 @@ def test_update_with_buffer

# Buffer is a shared string [ruby-core:120141] [Bug #20937]
cipher.reset
buf = "x" * 1024
shared = buf[-("data".bytesize + 32)..-1]
assert_same shared, cipher.update("data", shared)
buf = "x".b * 1024
shared = buf[-("data".bytesize * 10 + 32)..-1]
assert_same shared, cipher.update("data" * 10, shared)
assert_equal expected, shared + cipher.final
end

Expand Down
Loading