Skip to content

Commit 0ce6ab9

Browse files
committed
cipher: remove incorrect assertion in Cipher#update
Commit 1de3b80 (cipher: make output buffer String independent, 2024-12-10) ensures the output buffer String has sufficient capacity, bu the length can be shorter. The assert() is simply incorrect and should be removed. Also remove a similar assert() in Cipher#final. While not incorrect, it is not useful either.
1 parent 5af1eda commit 0ce6ab9

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

ext/openssl/ossl_cipher.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,19 +401,19 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
401401
}
402402
out_len = in_len + EVP_MAX_BLOCK_LENGTH;
403403

404-
if (NIL_P(str)) {
405-
str = rb_str_new(0, out_len);
406-
} else {
404+
if (NIL_P(str))
405+
str = rb_str_buf_new(out_len);
406+
else {
407407
StringValue(str);
408408
if ((long)rb_str_capacity(str) >= out_len)
409409
rb_str_modify(str);
410410
else
411411
rb_str_modify_expand(str, out_len - RSTRING_LEN(str));
412412
}
413413

414-
if (!ossl_cipher_update_long(ctx, (unsigned char *)RSTRING_PTR(str), &out_len, in, in_len))
415-
ossl_raise(eCipherError, NULL);
416-
assert(out_len <= RSTRING_LEN(str));
414+
if (!ossl_cipher_update_long(ctx, (unsigned char *)RSTRING_PTR(str),
415+
&out_len, in, in_len))
416+
ossl_raise(eCipherError, "EVP_CipherUpdate");
417417
rb_str_set_len(str, out_len);
418418

419419
return str;
@@ -456,7 +456,6 @@ ossl_cipher_final(VALUE self)
456456
ossl_raise(eCipherError, "cipher final failed");
457457
}
458458
}
459-
assert(out_len <= RSTRING_LEN(str));
460459
rb_str_set_len(str, out_len);
461460

462461
return str;

test/openssl/test_cipher.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@ def test_ctr_if_exists
134134
def test_update_with_buffer
135135
cipher = OpenSSL::Cipher.new("aes-128-ecb").encrypt
136136
cipher.random_key
137-
expected = cipher.update("data") << cipher.final
138-
assert_equal 16, expected.bytesize
137+
expected = cipher.update("data" * 10) << cipher.final
138+
assert_equal 48, expected.bytesize
139139

140140
# Buffer is supplied
141141
cipher.reset
142142
buf = String.new
143-
assert_same buf, cipher.update("data", buf)
143+
assert_same buf, cipher.update("data" * 10, buf)
144+
assert_equal 32, buf.bytesize
144145
assert_equal expected, buf + cipher.final
145146

146147
# Buffer is frozen
@@ -149,9 +150,9 @@ def test_update_with_buffer
149150

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

0 commit comments

Comments
 (0)