From f4d8c9f45dff584aec8df0993e1716390d57508c Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 4 Sep 2022 21:22:10 +0100 Subject: unixfd: Fix assertion failure constructing UnixFd for invalid fd File descriptors are represented as the C int type, but only non-negative values represent a valid fd, with all negative values representing the absence of a fd (a NULL-like value). Previously, make_fd() accepted negative fds, but then UnixFd_tp_new would crash with an assertion failure. Instead, range-check the value according to the semantically valid range. If it isn't, raise the same error we previously raised for values that don't fit in an int. Signed-off-by: Simon McVittie --- dbus_bindings/unixfd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbus_bindings/unixfd.c b/dbus_bindings/unixfd.c index 63a2402..e515cd3 100644 --- a/dbus_bindings/unixfd.c +++ b/dbus_bindings/unixfd.c @@ -62,9 +62,9 @@ typedef struct { } UnixFdObject; /* Return values: - * -2 - the long value overflows an int + * -2 - the long value is not plausible as a file descriptor * -1 - Python failed producing a long (or in Python 2 an int) - * 0 - success + * 0 - success (value might not *actually* be a fd, but it *could* be) * 1 - arg is not a long (or in Python 2 an int) * * Or to summarize: @@ -89,7 +89,7 @@ make_fd(PyObject *arg, int *fd) return 1; } /* Check for int overflow. */ - if (fd_arg < INT_MIN || fd_arg > INT_MAX) { + if (fd_arg < 0 || fd_arg > INT_MAX) { PyErr_Format(PyExc_ValueError, "int is outside fd range"); return -2; } -- cgit v1.2.1