-
-
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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -3332,24 +3332,41 @@ 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 *type; | ||||||
PyObject *optval; | ||||||
|
||||||
if (!PyArg_ParseTuple(args, "iiO|I:setsockopt", | ||||||
&level, &optname, &type, &optlen)) { | ||||||
return NULL; | ||||||
arglen = PyTuple_Size(args); | ||||||
switch (arglen) { | ||||||
case 3: | ||||||
if (!PyArg_ParseTuple(args, "iiO:setsockopt", | ||||||
&level, &optname, &optval)) { | ||||||
return NULL; | ||||||
} | ||||||
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. Can you please move this test for None and 4 arguments here?
|
||||||
break; | ||||||
case 4: | ||||||
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.
Suggested change
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. This ensures that the 3rd argument would be of the specified type. |
||||||
&level, &optname, Py_TYPE(Py_None), &optval, &optlen)) { | ||||||
return NULL; | ||||||
} | ||||||
break; | ||||||
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. We need 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. Look at https://github.com/python/cpython/pull/107546/files#r2192102447. |
||||||
default: | ||||||
PyErr_Format(PyExc_TypeError, | ||||||
"setsockopt() takes 3 or 4 arguments (%zd given)", | ||||||
arglen); | ||||||
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_Parse(type, "K", &vflag)) { | ||||||
if (!PyArg_Parse(optval, "K", &vflag)) { | ||||||
return NULL; | ||||||
} | ||||||
// level should always be set to AF_VSOCK | ||||||
|
@@ -3360,8 +3377,8 @@ sock_setsockopt(PyObject *self, PyObject *args) | |||||
#endif | ||||||
|
||||||
/* setsockopt(level, opt, flag) */ | ||||||
if (PyIndex_Check(type)) { | ||||||
if (!PyArg_Parse(type, "i", &flag)) { | ||||||
if (PyIndex_Check(optval)) { | ||||||
if (!PyArg_Parse(optval, "i", &flag)) { | ||||||
return NULL; | ||||||
} | ||||||
#ifdef MS_WINDOWS | ||||||
|
@@ -3381,32 +3398,38 @@ sock_setsockopt(PyObject *self, PyObject *args) | |||||
} | ||||||
|
||||||
/* 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 (type == Py_None) { | ||||||
if (optval == Py_None) { | ||||||
if (arglen != 4) { | ||||||
PyErr_Format(PyExc_TypeError, | ||||||
"setsockopt() take 4 arguments when socket option is None (%zd given)", | ||||||
arglen); | ||||||
return NULL; | ||||||
} | ||||||
assert(sizeof(socklen_t) >= sizeof(unsigned int)); | ||||||
res = setsockopt(get_sock_fd(s), level, optname, | ||||||
NULL, (socklen_t)optlen); | ||||||
goto done; | ||||||
} | ||||||
|
||||||
/* setsockopt(level, opt, buffer) */ | ||||||
if (PyObject_CheckBuffer(type)) { | ||||||
if (!PyArg_Parse(type, "y*", &optval)) { | ||||||
if (PyObject_CheckBuffer(optval)) { | ||||||
if (!PyArg_Parse(optval, "y*", &buffer)) { | ||||||
return NULL; | ||||||
} | ||||||
#ifdef MS_WINDOWS | ||||||
if (optval.len > INT_MAX) { | ||||||
PyBuffer_Release(&optval); | ||||||
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, | ||||||
optval.buf, (int)optval.len); | ||||||
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; | ||||||
} | ||||||
|
||||||
|
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.
It should be
assertRaisesRegex
. And the expected error message is a regular expression, so you need to escape parentheses.