From 83f57ca3d225ce06abbc5eef6aec37de4fa36d58 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 19 Jun 2024 14:04:30 +0200 Subject: String.new(capacity:) don't substract termlen [Bug #20585] This was changed in 36a06efdd9f0604093dccbaf96d4e2cb17874dc8 because `String.new(1024)` would end up allocating `1025` bytes, but the problem with this change is that the caller may be trying to right size a String. So instead, we should just better document the behavior of `capacity:`. --- string.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index 650d09186a..c97351c0d3 100644 --- a/string.c +++ b/string.c @@ -2055,12 +2055,7 @@ rb_str_s_new(int argc, VALUE *argv, VALUE klass) } } - long fake_len = capa - termlen; - if (fake_len < 0) { - fake_len = 0; - } - - VALUE str = str_new0(klass, NULL, fake_len, termlen); + VALUE str = str_new0(klass, NULL, capa, termlen); STR_SET_LEN(str, 0); TERM_FILL(RSTRING_PTR(str), termlen); -- cgit v1.2.3