From 4b16b245f48980de5abb6b62252d95e8c7ab8ab3 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Sun, 30 Aug 2026 01:02:44 +0200 Subject: [PATCH] Reject negative native ByteBuffer capacity --- ext/nio4r/bytebuffer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ext/nio4r/bytebuffer.c b/ext/nio4r/bytebuffer.c index 99e34f2..942656f 100644 --- a/ext/nio4r/bytebuffer.c +++ b/ext/nio4r/bytebuffer.c @@ -130,10 +130,16 @@ static size_t NIO_ByteBuffer_memsize(const void *data) static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity) { + int requested_capacity; struct NIO_ByteBuffer *buffer; TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer); - buffer->capacity = NUM2INT(capacity); + requested_capacity = NUM2INT(capacity); + if (requested_capacity < 0) { + rb_raise(rb_eArgError, "negative capacity given"); + } + + buffer->capacity = requested_capacity; buffer->buffer = xmalloc(buffer->capacity); NIO_ByteBuffer_clear(self);