Skip to contentSkip to conversation

udev - safer sprintf() use

PATCH Applied Kay Sievers 2 participants 2 messages Feb 28, 2004

lore
Share & tools
lore
#0 lore raw

Here is for now my last patch to the string handling for a rather theorethical case, where the node is very very very long. :)

We have accordant to strfieldcat(to, from) now a strintcat(to, i) macro, which appends the ascii representation of a integer to a string in a safe way.

thanks,
Kay

===== namedev.c 1.116 vs edited =====

namedev.c +3 -3
@@ -263,11 +263,11 @@
 			dbg("substitute kernel number '%s'", udev->kernel_number);
 				break;
 		case 'm':
-			sprintf(pos, "%u", udev->minor);
+			strnintcat(string, udev->minor, maxsize);
 			dbg("substitute minor number '%u'", udev->minor);
 			break;
-			case 'M':
-			sprintf(pos, "%u", udev->major);
+		case 'M':
+			strnintcat(string, udev->major, maxsize);
 			dbg("substitute major number '%u'", udev->major);
 			break;
 		case 'c':

===== udev-add.c 1.52 vs edited =====

udev-add.c +2 -1
@@ -211,7 +211,8 @@
 		info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions);
 		if (!fake) {
 			for (i = 1; i <= dev->partitions; i++) {
-				sprintf(partitionname, "%s%i", filename, i);
+				strfieldcpy(partitionname, filename);
+				strintcat(partitionname, i);
 				make_node(partitionname, dev->major,
 					  dev->minor + i, dev->mode, uid, gid);
 			}

===== udev-remove.c 1.20 vs edited =====

udev-remove.c +2 -1
@@ -87,7 +87,8 @@
 	if (dev->partitions > 0) {
 		info("removing partitions '%s[1-%i]'", filename, dev->partitions);
 		for (i = 1; i <= dev->partitions; i++) {
-			sprintf(partitionname, "%s%i", filename, i);
+			strfieldcpy(partitionname, filename);
+			strintcat(partitionname, i);
 			unlink(partitionname);
 		}
 	}

===== udev.h 1.47 vs edited =====

udev.h +12 -0
@@ -85,6 +85,18 @@
 	strncat(to, from, maxsize - strlen(to)-1); \
 } while (0)
 
+#define strintcat(to, i) \
+do { \
+	to[sizeof(to)-1] = '\0'; \
+	snprintf((to) + strlen(to), sizeof(to) - strlen(to)-1, "%u", i); \
+} while (0)
+
+#define strnintcat(to, i, maxsize) \
+do { \
+	to[maxsize-1] = '\0'; \
+	snprintf((to) + strlen(to), maxsize - strlen(to)-1, "%u", i); \
+} while (0)
+
 static inline char *get_action(void)
 {
 	char *action;
Applied
#1 lore raw
Quoted 6 lines · Kay Sievers

Here is for now my last patch to the string handling for a rather theorethical case, where the node is very very very long. :)

We have accordant to strfieldcat(to, from) now a strintcat(to, i) macro, which appends the ascii representation of a integer to a string in a safe way.

Nice, thanks a lot for doing all of this auditing. I really appreciate it.

Applied.

greg k-h

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel