Skip to content

Commit d43f796

Browse files
committed
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) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
1 parent f20b6e5 commit d43f796

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

ext/socket/ipsocket.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,18 @@ allocate_connection_attempt_fds(int additional_capacity)
281281
}
282282

283283
static int
284-
reallocate_connection_attempt_fds(int *fds, int current_capacity, int additional_capacity)
284+
reallocate_connection_attempt_fds(int **fds, int current_capacity, int additional_capacity)
285285
{
286286
int new_capacity = current_capacity + additional_capacity;
287+
int *new_fds;
287288

288-
if (realloc(fds, new_capacity * sizeof(int)) == NULL) {
289+
new_fds = realloc(*fds, new_capacity * sizeof(int));
290+
if (new_fds == NULL) {
289291
rb_syserr_fail(errno, "realloc(3)");
290292
}
293+
*fds = new_fds;
291294

292-
for (int i = current_capacity; i < new_capacity; i++) fds[i] = -1;
295+
for (int i = current_capacity; i < new_capacity; i++) (*fds)[i] = -1;
293296
return new_capacity;
294297
}
295298

@@ -845,7 +848,7 @@ init_fast_fallback_inetsock_internal(VALUE v)
845848
if (errno == EINPROGRESS) {
846849
if (current_capacity == arg->connection_attempt_fds_size) {
847850
current_capacity = reallocate_connection_attempt_fds(
848-
arg->connection_attempt_fds,
851+
&arg->connection_attempt_fds,
849852
current_capacity,
850853
additional_capacity
851854
);

0 commit comments

Comments
 (0)