Skip to content

termios.tpsetattr does rewrite errors #110365

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

Closed
sobolevn opened this issue Oct 4, 2023 · 0 comments
Closed

termios.tpsetattr does rewrite errors #110365

sobolevn opened this issue Oct 4, 2023 · 0 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 4, 2023

Bug report

This code is problematic:

cpython/Modules/termios.c

Lines 215 to 224 in bf4bc36

mode.c_iflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 0));
mode.c_oflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 1));
mode.c_cflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 2));
mode.c_lflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 3));
speed_t ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4));
speed_t ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5));
PyObject *cc = PyList_GetItem(term, 6);
if (PyErr_Occurred()) {
return NULL;
}

It does rewrite errors that happened before. Showing the last error, not the first one.
This goes against Python's semantics.

Here's the reproducer:

>>> import termios

>>> termios.tcsetattr(0, 0, [0, 1, 2, '3', 4, 5, 6])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer

>>> termios.tcsetattr(0, 0, [object(), 1, 2, '3', 4, 5, 6])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer

The second error should say:

>>> termios.tcsetattr(0, 0, [object(), 1, 2, '3', 4, 5, 6])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'object' object cannot be interpreted as an integer

And while we are there we can also change PyList_GetItem to PyList_GET_ITEM, because:

  • term is known to be a list
  • the size of term is known to be 7
    See check

    cpython/Modules/termios.c

    Lines 197 to 201 in bf4bc36

    if (!PyList_Check(term) || PyList_Size(term) != 7) {
    PyErr_SetString(PyExc_TypeError,
    "tcsetattr, arg 3: must be 7 element list");
    return NULL;
    }
    before these lines.

The only problem is tests. I think that termios is not tested that much in our suite. We don't even have test_termios.py file. Should I add one?

Refs #110260

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error extension-modules C modules in the Modules dir labels Oct 4, 2023
@sobolevn sobolevn self-assigned this Oct 4, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Oct 4, 2023
erlend-aasland added a commit that referenced this issue Oct 5, 2023
Co-authored-by: Erlend E. Aasland <erlend@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 5, 2023
…-110366)

(cherry picked from commit 2bbbab2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 5, 2023
…-110366)

(cherry picked from commit 2bbbab2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend@python.org>
erlend-aasland added a commit that referenced this issue Oct 5, 2023
…) (#110389)

(cherry picked from commit 2bbbab2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend@python.org>
erlend-aasland added a commit that referenced this issue Oct 5, 2023
…) (#110390)

(cherry picked from commit 2bbbab2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend@python.org>
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…10366)

Co-authored-by: Erlend E. Aasland <erlend@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants