Skip to content

Commit

Permalink
winusb: Fix winusb_get_device_list() failing to find port numbers
Browse files Browse the repository at this point in the history
Fix recent regression introduced in commit 9d595d4.

get_dev_port_number() was always returning port number 0 for any port,
due to the incorrect assumption that strtoll's 'end' pointer will point
to a null terminator on success. This was causing
winusb_get_device_list() to always fail. Since we actually know the
expected values of *end for both the SPDRP_LOCATION_INFORMATION and the
SPDRP_LOCATION_PATHS case, check for those instead.

Additionally, document why a return value of 0 is treated as a failure
in this particular function, for valid but somewhat coincidental reasons
that may not be immediately obvious.

Closes libusb#1544
  • Loading branch information
Mattiwatti authored and tormodvolden committed Jul 30, 2024
1 parent 8776b80 commit 467b6a8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions libusb/os/windows_winusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,11 @@ static bool get_dev_port_number(HDEVINFO dev_info, SP_DEVINFO_DATA *dev_info_dat
// Check for the required format.
if (strncmp(buffer, "Port_#", 6) == 0) {
start = buffer + 6;
// Note that 0 is both strtoll's sentinel return value to indicate failure, as well
// as (obviously) the return value for the literal "0". Fortunately we can always treat
// 0 as a failure, since Windows USB port numbers are numbered 1..n.
port = strtoll(start, &end, 10);
if (port < 0 || port > ULONG_MAX || end == start || *end != '\0') {
if (port <= 0 || port >= ULONG_MAX || end == start || (*end != '.' && *end != '\0')) {
return false;
}
*port_nr = (DWORD)port;
Expand All @@ -1262,7 +1265,7 @@ static bool get_dev_port_number(HDEVINFO dev_info, SP_DEVINFO_DATA *dev_info_dat
if (strncmp(token, "#USB(", 5) == 0) {
start = token + 5;
port = strtoll(start, &end, 10);
if (port < 0 || port > ULONG_MAX || end == start || *end != '\0') {
if (port <= 0 || port >= ULONG_MAX || end == start || (*end != ')' && *end != '\0')) {
return false;
}
*port_nr = (DWORD)port;
Expand Down
2 changes: 1 addition & 1 deletion libusb/version_nano.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define LIBUSB_NANO 11935
#define LIBUSB_NANO 11936

0 comments on commit 467b6a8

Please sign in to comment.