w32: Better protect the IO-system's fd_table
* src/w32-io.c (fd_table_lock): New. (new_fd): Lock allocation of a new slot. (release_fd): Lock deallocation of a slot. -- Note that we lock only the allocation but not the sanitiy checks we do further down in the code. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
3509cf2f98
commit
10f2e1c30b
20
src/w32-io.c
20
src/w32-io.c
@ -84,6 +84,7 @@ static struct
|
||||
duplicates works just fine. */
|
||||
int dup_from;
|
||||
} fd_table[MAX_SLAFD];
|
||||
DEFINE_STATIC_LOCK (fd_table_lock);
|
||||
|
||||
|
||||
/* Returns the FD or -1 on resource limit. */
|
||||
@ -92,6 +93,8 @@ new_fd (void)
|
||||
{
|
||||
int idx;
|
||||
|
||||
LOCK (fd_table_lock);
|
||||
|
||||
for (idx = 0; idx < MAX_SLAFD; idx++)
|
||||
if (! fd_table[idx].used)
|
||||
break;
|
||||
@ -99,14 +102,18 @@ new_fd (void)
|
||||
if (idx == MAX_SLAFD)
|
||||
{
|
||||
gpg_err_set_errno (EIO);
|
||||
return -1;
|
||||
idx = -1;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
fd_table[idx].used = 1;
|
||||
fd_table[idx].handle = INVALID_HANDLE_VALUE;
|
||||
fd_table[idx].socket = INVALID_SOCKET;
|
||||
fd_table[idx].rvid = 0;
|
||||
fd_table[idx].dup_from = -1;
|
||||
}
|
||||
|
||||
UNLOCK (fd_table_lock);
|
||||
|
||||
return idx;
|
||||
}
|
||||
@ -115,9 +122,13 @@ new_fd (void)
|
||||
void
|
||||
release_fd (int fd)
|
||||
{
|
||||
if (fd < 0 || fd >= MAX_SLAFD || !fd_table[fd].used)
|
||||
if (fd < 0 || fd >= MAX_SLAFD)
|
||||
return;
|
||||
|
||||
LOCK (fd_table_lock);
|
||||
|
||||
if (fd_table[fd].used)
|
||||
{
|
||||
fd_table[fd].used = 0;
|
||||
fd_table[fd].handle = INVALID_HANDLE_VALUE;
|
||||
fd_table[fd].socket = INVALID_SOCKET;
|
||||
@ -125,6 +136,9 @@ release_fd (int fd)
|
||||
fd_table[fd].dup_from = -1;
|
||||
}
|
||||
|
||||
UNLOCK (fd_table_lock);
|
||||
}
|
||||
|
||||
|
||||
#define handle_to_fd(a) ((int)(a))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user