diff options
author | Yusuke Endoh <[email protected]> | 2024-11-21 10:55:41 +0900 |
---|---|---|
committer | Yusuke Endoh <[email protected]> | 2024-11-21 00:20:11 -0600 |
commit | d43f796292dca6755c4cdf0823d1b9dff80ebeb5 (patch) | |
tree | 59a9c4bae53a91a67a15590e69fe5b7df8b01cc1 /ext/socket | |
parent | f20b6e5dbcb8af41c64dc468976e518806b0712f (diff) |
Fix the usage of realloc
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) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Diffstat (limited to 'ext/socket')
-rw-r--r-- | ext/socket/ipsocket.c | 11 |
1 files 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 ); |