Handle 0 length arrays properly #1356

This commit is contained in:
Ian Craggs 2023-05-15 15:53:45 +01:00
parent 3933f8b796
commit fd213a8461
1 changed files with 28 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2022 IBM Corp., Ian Craggs and others
* Copyright (c) 2009, 2023 IBM Corp., Ian Craggs and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
@ -516,17 +516,40 @@ SOCKET Socket_getReadySocket(int more_work, int timeout, mutex_type mutex, int*
if (mod_s.nfds != mod_s.saved.nfds)
{
mod_s.saved.nfds = mod_s.nfds;
if (mod_s.saved.fds_read)
if (mod_s.nfds == 0)
{
if (mod_s.saved.fds_read)
{
free(mod_s.saved.fds_read);
mod_s.saved.fds_read = NULL;
}
}
else if (mod_s.saved.fds_read)
mod_s.saved.fds_read = realloc(mod_s.saved.fds_read, mod_s.nfds * sizeof(struct pollfd));
else
mod_s.saved.fds_read = malloc(mod_s.nfds * sizeof(struct pollfd));
if (mod_s.saved.fds_write)
if (mod_s.nfds == 0)
{
if (mod_s.saved.fds_write)
{
free(mod_s.saved.fds_write);
mod_s.saved.fds_write = NULL;
}
}
else if (mod_s.saved.fds_write)
mod_s.saved.fds_write = realloc(mod_s.saved.fds_write, mod_s.nfds * sizeof(struct pollfd));
else
mod_s.saved.fds_write = malloc(mod_s.nfds * sizeof(struct pollfd));
}
memcpy(mod_s.saved.fds_read, mod_s.fds_read, mod_s.nfds * sizeof(struct pollfd));
memcpy(mod_s.saved.fds_write, mod_s.fds_write, mod_s.nfds * sizeof(struct pollfd));
if (mod_s.fds_read == NULL)
mod_s.saved.fds_read = NULL;
else
memcpy(mod_s.saved.fds_read, mod_s.fds_read, mod_s.nfds * sizeof(struct pollfd));
if (mod_s.fds_write == NULL)
mod_s.saved.fds_write = NULL;
else
memcpy(mod_s.saved.fds_write, mod_s.fds_write, mod_s.nfds * sizeof(struct pollfd));
if (mod_s.saved.nfds == 0)
{