From d43f796292dca6755c4cdf0823d1b9dff80ebeb5 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 21 Nov 2024 10:55:41 +0900 Subject: [PATCH] Fix the usage of realloc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit http://ci.rvm.jp/results/trunk-repeat50@ruby-sp2-noble-docker/5420911 ``` /tmp/ruby/src/trunk-repeat50/ext/socket/ipsocket.c: In function ‘reallocate_connection_attempt_fds’: /tmp/ruby/src/trunk-repeat50/ext/socket/ipsocket.c:292:62: warning: pointer ‘fds’ may be used after ‘realloc’ [-Wuse-after-free] 292 | for (int i = current_capacity; i < new_capacity; i++) fds[i] = -1; | ^ /tmp/ruby/src/trunk-repeat50/ext/socket/ipsocket.c:288:9: note: call to ‘realloc’ here 288 | if (realloc(fds, new_capacity * sizeof(int)) == NULL) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` --- ext/socket/ipsocket.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c index 8ef0034b7d..9530dac482 100644 --- a/ext/socket/ipsocket.c +++ b/ext/socket/ipsocket.c @@ -281,15 +281,18 @@ allocate_connection_attempt_fds(int additional_capacity) } static int -reallocate_connection_attempt_fds(int *fds, int current_capacity, int additional_capacity) +reallocate_connection_attempt_fds(int **fds, int current_capacity, int additional_capacity) { int new_capacity = current_capacity + additional_capacity; + int *new_fds; - if (realloc(fds, new_capacity * sizeof(int)) == NULL) { + new_fds = realloc(*fds, new_capacity * sizeof(int)); + if (new_fds == NULL) { rb_syserr_fail(errno, "realloc(3)"); } + *fds = new_fds; - for (int i = current_capacity; i < new_capacity; i++) fds[i] = -1; + for (int i = current_capacity; i < new_capacity; i++) (*fds)[i] = -1; return new_capacity; } @@ -845,7 +848,7 @@ init_fast_fallback_inetsock_internal(VALUE v) if (errno == EINPROGRESS) { if (current_capacity == arg->connection_attempt_fds_size) { current_capacity = reallocate_connection_attempt_fds( - arg->connection_attempt_fds, + &arg->connection_attempt_fds, current_capacity, additional_capacity );