libftdi Archives

Subject: [PATCH] avoid PATH_MAX/string copies usage

From: Mike Frysinger <vapier@xxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Tue, 29 Dec 2009 03:37:15 -0500
The new ftdi_usb_open_string() func breaks mingw builds where PATH_MAX is
not defined.  Rather than adding fallback code, compare the path strings
piece by piece to avoid the string copy overhead in the process.

Signed-off-by: Mike Frysinger <vapier@xxxxxxxxxx>
---
only compile tested ...

 src/ftdi.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/ftdi.c b/src/ftdi.c
index 6068e7e..9740fca 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -715,7 +715,6 @@ int ftdi_usb_open_string(struct ftdi_context *ftdi, const 
char* description)
     {
         struct usb_bus *bus;
         struct usb_device *dev;
-        char dev_name[PATH_MAX+1];
 
         usb_init();
 
@@ -728,9 +727,18 @@ int ftdi_usb_open_string(struct ftdi_context *ftdi, const 
char* description)
         {
             for (dev = bus->devices; dev; dev = dev->next)
             {
-                snprintf(dev_name, sizeof(dev_name), 
"%s/%s",bus->dirname,dev->filename);
-                if (strcmp(description+2,dev_name) == 0)
-                    return ftdi_usb_open_dev(ftdi, dev);
+                /* XXX: This doesn't handle symlinks/odd paths/etc... */
+                const char *desc = description + 2;
+                size_t len = strlen(bus->dirname);
+                if (strncmp(desc, bus->dirname, len))
+                    continue;
+                desc += len;
+                if (desc[0] != '/')
+                    continue;
+                ++desc;
+                if (strcmp(desc, dev->filename))
+                    continue;
+                return ftdi_usb_open_dev(ftdi, dev);
             }
         }
 
-- 
1.6.6


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread
  • [PATCH] avoid PATH_MAX/string copies usage, Mike Frysinger <=