[Pcsclite-cvs-commit] r7058 - trunk/PCSC/src

ludovic.rousseau at free.fr ludovic.rousseau at free.fr
Wed Dec 31 12:19:43 UTC 2014


Author: rousseau
Date: 2014-12-31 12:19:43 +0000 (Wed, 31 Dec 2014)
New Revision: 7058

Modified:
   trunk/PCSC/src/hotplug_libudev.c
Log:
Use asprintf(3) (from GNU) instead of strlcat(3)

http://lwn.net/Articles/612627/
" strlcpy() in glibc

Posted Sep 19, 2014 23:34 UTC (Fri) by Koromix (subscriber, #71455)
In reply to: strlcpy() in glibc by epa
Parent article: Adding strlcpy() to glibc

None that I know of. When I manipulate dynamic raw C strings I go for
one of these:
- asprintf is great, you can't abuse it
- malloc + a suite of stpcpy
- strdup (with in-place characters substitutions on the copy if needed,
  such as fixing slashes in paths)

I almost never use strcat because stpcpy is more efficient.

This is enough when your code does little string handling (some
identifiers, a few paths here and there). Beyond that, it's inefficient
and insecure and you better use another language (C++) or at the very
least a higher-level string handling code/library.

Note that for cross-platform code you may need to provide fallback
implementations of asprintf and stpcpy/stpncpy. "

With this patch we can remove our own copy (from OpenBSD) of strlcat(3).


Modified: trunk/PCSC/src/hotplug_libudev.c
===================================================================
--- trunk/PCSC/src/hotplug_libudev.c	2014-12-31 11:49:17 UTC (rev 7057)
+++ trunk/PCSC/src/hotplug_libudev.c	2014-12-31 12:19:43 UTC (rev 7058)
@@ -40,6 +40,7 @@
 #include "config.h"
 #if defined(HAVE_LIBUDEV) && defined(USE_USB)
 
+#define _GNU_SOURCE		/* for asprintf(3) */
 #include <string.h>
 #include <stdio.h>
 #include <dirent.h>
@@ -379,7 +380,7 @@
 {
 	int i;
 	char deviceName[MAX_DEVICENAME];
-	char fullname[MAX_READERNAME];
+	char *fullname;
 	struct _driverTracker *driver, *classdriver;
 	const char *sSerialNumber = NULL, *sInterfaceName = NULL;
 	const char *sInterfaceNumber;
@@ -466,14 +467,18 @@
 		sSerialNumber = udev_device_get_sysattr_value(parent, "serial");
 
 	/* name from the Info.plist file */
-	strlcpy(fullname, driver->readerName, sizeof(fullname));
+	fullname = strdup(driver->readerName);
 
 	/* interface name from the device (if any) */
 	if (sInterfaceName)
 	{
-		strlcat(fullname, " [", sizeof(fullname));
-		strlcat(fullname, sInterfaceName, sizeof(fullname));
-		strlcat(fullname, "]", sizeof(fullname));
+		char *result;
+
+		/* create a new name */
+		asprintf(&result, "%s [%s]", fullname, sInterfaceName);
+
+		free(fullname);
+		fullname = result;
 	}
 
 	/* serial number from the device (if any) */
@@ -483,9 +488,13 @@
 		 * interface name */
 		if (!sInterfaceName || NULL == strstr(sInterfaceName, sSerialNumber))
 		{
-			strlcat(fullname, " (", sizeof(fullname));
-			strlcat(fullname, sSerialNumber, sizeof(fullname));
-			strlcat(fullname, ")", sizeof(fullname));
+			char *result;
+
+			/* create a new name */
+			asprintf(&result, "%s (%s)", fullname, sSerialNumber);
+
+			free(fullname);
+			fullname = result;
 		}
 	}
 
@@ -517,6 +526,8 @@
 			(void)CheckForOpenCT();
 		}
 	}
+
+	free(fullname);
 } /* HPAddDevice */
 
 




More information about the Pcsclite-cvs-commit mailing list