Skip to content

Argument count of os.sendfile isn't checked to be non-negative #138401

@wiomoc

Description

@wiomoc

Bug report

Bug description:

Passing a negative integer as count argument, doesn't result in a ValueError. Instead, the value is coerced to a (unsigned) ssize_t.

The bug is clearly visible when tracing the sendfile syscall using strace

uli@DESKTOP-BD876D0:~/cpython$ strace -Tfe trace=sendfile ./python
Python 3.15.0a0 (heads/main:e4e2390a645, Sep  2 2025, 21:51:53) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> os.sendfile(os.sendfile(sys.stdout.fileno(), sys.stdin.fileno(), 0, count=-2))
sendfile(1, 0, [0], 18446744073709551614) = -1 ESPIPE (Illegal seek) <0.000070>
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    os.sendfile(os.sendfile(sys.stdout.fileno(), sys.stdin.fileno(), 0, count=-2))
                ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 29] Illegal seek

Rel: gh-138394

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions