From 09295ea796900fb7b05d29e93364090e21598566 Mon Sep 17 00:00:00 2001 From: Kasumi Hanazuki Date: Sat, 25 Mar 2023 08:12:23 +0900 Subject: IO::Buffer#resize: Free internal buffer if new size is zero (#7569) `#resize(0)` on an IO::Buffer with internal buffer allocated will result in calling `realloc(data->base, 0)`. The behavior of `realloc` with size = 0 is implementation-defined (glibc frees the object and returns NULL, while BSDs return an inaccessible object). And thus such usage is deprecated in standard C (upcoming C23 will make it UB). To avoid this problem, just `free`s the memory when the new size is zero. --- io_buffer.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'io_buffer.c') diff --git a/io_buffer.c b/io_buffer.c index 91083cd7e4..1ff3935094 100644 --- a/io_buffer.c +++ b/io_buffer.c @@ -1422,6 +1422,11 @@ rb_io_buffer_resize(VALUE self, size_t size) #endif if (data->flags & RB_IO_BUFFER_INTERNAL) { + if (size == 0) { + io_buffer_free(data); + return; + } + void *base = realloc(data->base, size); if (!base) { -- cgit v1.2.1