Skip to content

Commit 11561cd

Browse files
committed
add an invalid parameter handler to catch _get_osfhandle failures under visual studio
The sfds code blindly uses _get_osfhandle on values that may be file descriptors, sockets or io handles. Under visual studio, _get_osfhandle will call the invalid parameter handler for items that are not file descriptors. Adding the handler allows us to call this in the same way that mingw does. We will still get an assertion, but a prior change sends those to stdiout instead of making the user click through a dialog.
1 parent 58d15ec commit 11561cd

File tree

1 file changed

+99
-57
lines changed
  • contrib/win32/win32compat

1 file changed

+99
-57
lines changed

contrib/win32/win32compat/sfds.c

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <io.h>
3434
#include "sfds.h"
3535

36+
3637
extern void debug(const char *fmt,...);
3738
extern void debug2(const char *fmt,...);
3839
extern void debug3(const char *fmt,...);
@@ -54,69 +55,110 @@ static int sfd_map_init = 0;
5455
static int sfd_count = 0;
5556
int sfd_start = 0;
5657

58+
59+
void myInvalidParameterHandler(const wchar_t* expression,
60+
const wchar_t* function,
61+
const wchar_t* file,
62+
unsigned int line,
63+
uintptr_t pReserved)
64+
{
65+
return;
66+
}
67+
68+
5769
/*
5870
* store real fd in map, detect fd type and return sfd number.
5971
*/
6072

6173
int allocate_sfd(int fd_or_handle)
6274
{
63-
int slot = SFD_FD_INVALID;
64-
int i;
65-
int real_fd;
66-
67-
HANDLE real_handle;
68-
69-
DWORD handle_type;
70-
71-
/*
72-
* Init the map once
73-
*/
74-
75-
if (!sfd_map_init)
76-
{
77-
sfd_map_init = 1;
78-
79-
for (i = 0; i < SFD_MAP_SIZE; ++i)
80-
{
81-
sfd_map[i].fd = SFD_FD_INVALID;
82-
sfd_map[i].type = SFD_TYPE_NONE;
83-
}
84-
}
85-
86-
/*
87-
* Find an open slot
88-
*/
89-
90-
for (i = sfd_start; i < SFD_MAP_SIZE; ++i)
91-
{
92-
/*
93-
* Is this slot open?
94-
*/
95-
96-
if (sfd_map[i].fd == SFD_FD_INVALID)
97-
{
98-
slot = i;
99-
100-
break;
101-
}
102-
}
103-
104-
/*
105-
* Bail if no slot found
106-
*/
107-
108-
if (slot == SFD_FD_INVALID)
109-
{
110-
error("ERROR: Too many connections.");
111-
112-
return -1;
113-
}
114-
115-
/*
116-
* Detect and save real fd and real handle
117-
*/
118-
119-
real_handle = (HANDLE) _get_osfhandle(fd_or_handle);
75+
int slot = SFD_FD_INVALID;
76+
int i;
77+
int real_fd;
78+
79+
HANDLE real_handle;
80+
81+
DWORD handle_type;
82+
83+
/*
84+
* Init the map once
85+
*/
86+
87+
if (!sfd_map_init)
88+
{
89+
sfd_map_init = 1;
90+
91+
for (i = 0; i < SFD_MAP_SIZE; ++i)
92+
{
93+
sfd_map[i].fd = SFD_FD_INVALID;
94+
sfd_map[i].type = SFD_TYPE_NONE;
95+
}
96+
}
97+
98+
/*
99+
* Find an open slot
100+
*/
101+
102+
for (i = sfd_start; i < SFD_MAP_SIZE; ++i)
103+
{
104+
/*
105+
* Is this slot open?
106+
*/
107+
108+
if (sfd_map[i].fd == SFD_FD_INVALID)
109+
{
110+
slot = i;
111+
112+
break;
113+
}
114+
}
115+
116+
/*
117+
* Bail if no slot found
118+
*/
119+
120+
if (slot == SFD_FD_INVALID)
121+
{
122+
error("ERROR: Too many connections.");
123+
124+
return -1;
125+
}
126+
127+
#if 0
128+
/*
129+
* Detect and save real fd and real handle
130+
*/
131+
int optVal;
132+
int optLen = sizeof(int);
133+
BOOL bIsSocket = TRUE;
134+
HRESULT hr = S_OK;
135+
int ret = getsockopt(fd_or_handle,
136+
SOL_SOCKET,
137+
SO_DEBUG,
138+
(char*)&optVal,
139+
&optLen);
140+
141+
if (ret == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK)
142+
{
143+
bIsSocket = FALSE;
144+
}
145+
146+
// if (!bIsSocket && fd_or_handle > 600)
147+
// bIsSocket = TRUE;
148+
149+
if (bIsSocket == TRUE)
150+
real_handle = (HANDLE)fd_or_handle;
151+
else
152+
#endif
153+
154+
155+
_invalid_parameter_handler oldHandler, newHandler;
156+
newHandler = myInvalidParameterHandler;
157+
oldHandler = _set_invalid_parameter_handler(newHandler);
158+
159+
160+
real_handle = (HANDLE)_get_osfhandle(fd_or_handle);
161+
_set_invalid_parameter_handler(oldHandler);
120162

121163
if (real_handle == INVALID_HANDLE_VALUE)
122164
{

0 commit comments

Comments
 (0)