-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-107545: Fix misleading setsockopt error message #107546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f85c7dc
82be245
167d212
e3d4e74
ca87487
543c20b
2ca926d
97ee4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Improve the error messages that may be raised by | ||
:meth:`~socket.socket.setsockopt` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3332,32 +3332,58 @@ sock_setsockopt(PyObject *self, PyObject *args) | |
{ | ||
PySocketSockObject *s = _PySocketSockObject_CAST(self); | ||
|
||
Py_ssize_t arglen; | ||
int level; | ||
int optname; | ||
int res; | ||
Py_buffer optval; | ||
Py_buffer buffer; | ||
int flag; | ||
unsigned int optlen; | ||
PyObject *none; | ||
PyObject *optval; | ||
|
||
if (!PyArg_ParseTuple(args, "iiO|I:setsockopt", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All this should be after the code for AF_VSOCK. Otherwise we will get wrong error messages for AF_VSOCK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docs for setsockopt state there are 3 different overloads and does not mention anything different for
It weird that suddenly the error message tell you that setsockopt only supports 3 arguments although there is overload with 4. I personally would expect that
|
||
&level, &optname, &optval, &optlen)) { | ||
return NULL; | ||
} | ||
|
||
arglen = PyTuple_Size(args); | ||
if (arglen == 3 && optval == Py_None) { | ||
PyErr_Format(PyExc_TypeError, | ||
"setsockopt() requires 4 arguments when the third argument is None", | ||
arglen); | ||
return NULL; | ||
} | ||
if (arglen == 4 && optval != Py_None) { | ||
PyErr_Format(PyExc_TypeError, | ||
"setsockopt() only takes 4 arguments when the third argument is None (got %T)", | ||
optval); | ||
return NULL; | ||
} | ||
|
||
#ifdef AF_VSOCK | ||
if (s->sock_family == AF_VSOCK) { | ||
if (!PyIndex_Check(optval)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"setsockopt() argument 3 for AF_VSOCK must be an int (got %T)", | ||
optval); | ||
} | ||
uint64_t vflag; // Must be set width of 64 bits | ||
/* setsockopt(level, opt, flag) */ | ||
if (PyArg_ParseTuple(args, "iiK:setsockopt", | ||
&level, &optname, &vflag)) { | ||
// level should always be set to AF_VSOCK | ||
res = setsockopt(get_sock_fd(s), level, optname, | ||
(void*)&vflag, sizeof vflag); | ||
goto done; | ||
if (!PyArg_Parse(optval, "K", &vflag)) { | ||
return NULL; | ||
} | ||
return NULL; | ||
// level should always be set to AF_VSOCK | ||
res = setsockopt(get_sock_fd(s), level, optname, | ||
(void*)&vflag, sizeof vflag); | ||
goto done; | ||
} | ||
#endif | ||
|
||
/* setsockopt(level, opt, flag) */ | ||
if (PyArg_ParseTuple(args, "iii:setsockopt", | ||
&level, &optname, &flag)) { | ||
if (PyIndex_Check(optval)) { | ||
if (!PyArg_Parse(optval, "i", &flag)) { | ||
return NULL; | ||
} | ||
#ifdef MS_WINDOWS | ||
if (optname == SIO_TCP_SET_ACK_FREQUENCY) { | ||
DWORD dummy; | ||
|
@@ -3374,36 +3400,40 @@ sock_setsockopt(PyObject *self, PyObject *args) | |
goto done; | ||
} | ||
|
||
PyErr_Clear(); | ||
/* setsockopt(level, opt, None, flag) */ | ||
if (PyArg_ParseTuple(args, "iiO!I:setsockopt", | ||
&level, &optname, Py_TYPE(Py_None), &none, &optlen)) { | ||
/* setsockopt(level, opt, None, optlen) */ | ||
if (optval == Py_None) { | ||
assert(sizeof(socklen_t) >= sizeof(unsigned int)); | ||
res = setsockopt(get_sock_fd(s), level, optname, | ||
NULL, (socklen_t)optlen); | ||
goto done; | ||
} | ||
|
||
PyErr_Clear(); | ||
/* setsockopt(level, opt, buffer) */ | ||
if (!PyArg_ParseTuple(args, "iiy*:setsockopt", | ||
&level, &optname, &optval)) | ||
return NULL; | ||
|
||
if (PyObject_CheckBuffer(optval)) { | ||
if (!PyArg_Parse(optval, "y*", &buffer)) { | ||
return NULL; | ||
} | ||
#ifdef MS_WINDOWS | ||
if (optval.len > INT_MAX) { | ||
PyBuffer_Release(&optval); | ||
PyErr_Format(PyExc_OverflowError, | ||
"socket option is larger than %i bytes", | ||
INT_MAX); | ||
return NULL; | ||
} | ||
res = setsockopt(get_sock_fd(s), level, optname, | ||
optval.buf, (int)optval.len); | ||
if (buffer.len > INT_MAX) { | ||
PyBuffer_Release(&buffer); | ||
PyErr_Format(PyExc_OverflowError, | ||
"socket option is larger than %i bytes", | ||
INT_MAX); | ||
return NULL; | ||
} | ||
res = setsockopt(get_sock_fd(s), level, optname, | ||
buffer.buf, (int)buffer.len); | ||
#else | ||
res = setsockopt(get_sock_fd(s), level, optname, optval.buf, optval.len); | ||
res = setsockopt(get_sock_fd(s), level, optname, buffer.buf, buffer.len); | ||
#endif | ||
PyBuffer_Release(&optval); | ||
PyBuffer_Release(&buffer); | ||
goto done; | ||
} | ||
|
||
PyErr_Format(PyExc_TypeError, | ||
"socket option should be int, bytes-like object or None (got %T)", | ||
optval); | ||
return NULL; | ||
|
||
done: | ||
if (res < 0) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.