Skip to content

Commit 689aadf

Browse files
wiomocvstinnerkumaraditya303
authored andcommitted
pythongh-138401: Check arg count>=0 in os.sendfile() (python#138403)
Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
1 parent cc14494 commit 689aadf

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Lib/test/test_os.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,11 @@ async def test_invalid_offset(self):
39573957
await self.async_sendfile(self.sockno, self.fileno, -1, 4096)
39583958
self.assertEqual(cm.exception.errno, errno.EINVAL)
39593959

3960+
async def test_invalid_count(self):
3961+
with self.assertRaises(ValueError, msg="count cannot be negative"):
3962+
await self.sendfile_wrapper(self.sockno, self.fileno, offset=0,
3963+
count=-1)
3964+
39603965
async def test_keywords(self):
39613966
# Keyword arguments should be supported
39623967
await self.async_sendfile(out_fd=self.sockno, in_fd=self.fileno,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add missing validation of argument ``count`` in :func:`os.sendfile` to be
2+
non-negative.

Modules/clinic/posixmodule.c.h

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11874,7 +11874,7 @@ os.sendfile
1187411874
out_fd: int
1187511875
in_fd: int
1187611876
offset: Py_off_t
11877-
count: Py_ssize_t
11877+
count: Py_ssize_t(allow_negative=False)
1187811878
headers: object(c_default="NULL") = ()
1187911879
trailers: object(c_default="NULL") = ()
1188011880
flags: int = 0
@@ -11886,28 +11886,38 @@ static PyObject *
1188611886
os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
1188711887
Py_ssize_t count, PyObject *headers, PyObject *trailers,
1188811888
int flags)
11889-
/*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/
11889+
/*[clinic end generated code: output=329ea009bdd55afc input=dcb026b94effa922]*/
1189011890
#else
1189111891
/*[clinic input]
1189211892
os.sendfile
1189311893
1189411894
out_fd: int
1189511895
in_fd: int
1189611896
offset as offobj: object
11897-
count: Py_ssize_t
11897+
count: Py_ssize_t(allow_negative=False)
1189811898
1189911899
Copy count bytes from file descriptor in_fd to file descriptor out_fd.
1190011900
[clinic start generated code]*/
1190111901

1190211902
static PyObject *
1190311903
os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
1190411904
Py_ssize_t count)
11905-
/*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/
11905+
/*[clinic end generated code: output=ae81216e40f167d8 input=424df0949059ea5b]*/
1190611906
#endif
1190711907
{
1190811908
Py_ssize_t ret;
1190911909
int async_err = 0;
1191011910

11911+
#ifdef __APPLE__
11912+
if(sbytes < 0) {
11913+
PyErr_SetString(PyExc_ValueError,
11914+
"count cannot be negative");
11915+
return NULL;
11916+
}
11917+
#else
11918+
assert(count >= 0);
11919+
#endif
11920+
1191111921
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
1191211922
#ifndef __APPLE__
1191311923
off_t sbytes;

0 commit comments

Comments
 (0)