-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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 1 commit
f85c7dc
82be245
167d212
e3d4e74
ca87487
543c20b
2ca926d
97ee4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3338,26 +3338,32 @@ sock_setsockopt(PyObject *self, PyObject *args) | |||||
Py_buffer optval; | ||||||
int flag; | ||||||
unsigned int optlen; | ||||||
PyObject *none; | ||||||
PyObject *type; | ||||||
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. Semantically, this is not type, but value. 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. I choose the name |
||||||
|
||||||
if (!PyArg_ParseTuple(args, "iiO|I:setsockopt", | ||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
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, &type, &optlen)) { | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
#ifdef AF_VSOCK | ||||||
if (s->sock_family == AF_VSOCK) { | ||||||
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(type, "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(type)) { | ||||||
if (!PyArg_Parse(type, "i", &flag)) { | ||||||
return NULL; | ||||||
} | ||||||
#ifdef MS_WINDOWS | ||||||
if (optname == SIO_TCP_SET_ACK_FREQUENCY) { | ||||||
DWORD dummy; | ||||||
|
@@ -3373,43 +3379,40 @@ sock_setsockopt(PyObject *self, PyObject *args) | |||||
(char*)&flag, sizeof flag); | ||||||
goto done; | ||||||
} | ||||||
if (!PyErr_ExceptionMatches(PyExc_TypeError)) { | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
PyErr_Clear(); | ||||||
/* setsockopt(level, opt, None, flag) */ | ||||||
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.
Suggested change
|
||||||
if (PyArg_ParseTuple(args, "iiO!I:setsockopt", | ||||||
&level, &optname, Py_TYPE(Py_None), &none, &optlen)) { | ||||||
if (type == Py_None) { | ||||||
assert(sizeof(socklen_t) >= sizeof(unsigned int)); | ||||||
res = setsockopt(get_sock_fd(s), level, optname, | ||||||
NULL, (socklen_t)optlen); | ||||||
goto done; | ||||||
} | ||||||
if (!PyErr_ExceptionMatches(PyExc_TypeError)) { | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
PyErr_Clear(); | ||||||
/* setsockopt(level, opt, buffer) */ | ||||||
if (!PyArg_ParseTuple(args, "iiy*:setsockopt", | ||||||
&level, &optname, &optval)) | ||||||
return NULL; | ||||||
|
||||||
if (PyObject_CheckBuffer(type)) { | ||||||
if (!PyArg_Parse(type, "y*", &optval)) { | ||||||
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 (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); | ||||||
#else | ||||||
res = setsockopt(get_sock_fd(s), level, optname, optval.buf, optval.len); | ||||||
res = setsockopt(get_sock_fd(s), level, optname, optval.buf, optval.len); | ||||||
#endif | ||||||
PyBuffer_Release(&optval); | ||||||
PyBuffer_Release(&optval); | ||||||
goto done; | ||||||
} | ||||||
|
||||||
PyErr_Format(PyExc_TypeError, | ||||||
"socket option should be integer, bytes-like object or None"); | ||||||
return NULL; | ||||||
|
||||||
done: | ||||||
if (res < 0) { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can test the error message as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Idea, I also created some more type checks and added their exact message to the tests code.