From 1249814e93a544c85cbfc21b886cef6e64d3a14b Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Mon, 6 Apr 2020 14:42:03 -0500 Subject: [PATCH] Fix segfault in Linux USB serial port auto-detection --- serial_port/find_usb_serial_port_linux.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/serial_port/find_usb_serial_port_linux.cpp b/serial_port/find_usb_serial_port_linux.cpp index d076cd6d..15ad3180 100644 --- a/serial_port/find_usb_serial_port_linux.cpp +++ b/serial_port/find_usb_serial_port_linux.cpp @@ -50,7 +50,11 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) strcpy(tty_path, "/sys/class/tty/"); strcat(tty_path, ent->d_name); - readlink(tty_path, symlink_path, 1024); + /*-----------------------------------------------------------------*\ + | readlink() does not null-terminate, so manually terminate it | + \*-----------------------------------------------------------------*/ + ssize_t link_path_size = readlink(tty_path, symlink_path, 1024); + symlink_path[link_path_size] = '\0'; char * usb_string = strstr(symlink_path, "usb");