Skip to content
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

FD 0 is a valid file descriptor #462

Open
mzyngier opened this issue Oct 15, 2020 · 0 comments
Open

FD 0 is a valid file descriptor #462

mzyngier opened this issue Oct 15, 2020 · 0 comments

Comments

@mzyngier
Copy link

Playing with usbrelay (which uses hidapi), I noticed that it fails if FD 0 is closed and that an open result is FD 0 being allocated by the kernel:

maz@pygmy-twylyte:~$ (exec 0<&-; usbrelay)
Device Found
  type: 16c0 05df
  path: /dev/hidraw0
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
  Number of Relays = 2
unable to open device

strace confirms this:

openat(AT_FDCWD, "/dev/hidraw0", O_RDWR) = 0
write(2, "unable to open device\n", 22) = 22

The bug seems to be here:

if (dev->device_handle > 0) {

easily fixed with:

diff --git a/linux/hid.c b/linux/hid.c
index 56dac0f..f99cd5c 100644
--- a/linux/hid.c
+++ b/linux/hid.c
@@ -629,7 +629,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
 	dev->device_handle = open(path, O_RDWR);
 
 	/* If we have a good handle, return it. */
-	if (dev->device_handle > 0) {
+	if (dev->device_handle >= 0) {
 
 		/* Get the report descriptor */
 		int res, desc_size = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant