chiark / gitweb /
[PATCH] udev - safer sprintf() use
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Sat, 28 Feb 2004 09:59:02 +0000 (01:59 -0800)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 04:34:28 +0000 (21:34 -0700)
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.

namedev.c
udev-add.c
udev-remove.c
udev.h

index 7c07e3b10423f9185cea3f1af40c23f9800d23f8..581a7f2218ca08973d90bef696ba45d1132f2a36 100644 (file)
--- a/namedev.c
+++ b/namedev.c
@@ -263,11 +263,11 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
                        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':
index 8fdd66fad52511d0f1bc9c165608bb160fade14d..3a72c544b8b9b20af53c61153c6b4e3aee579bda 100644 (file)
@@ -211,7 +211,8 @@ static int create_node(struct udevice *dev, int fake)
                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);
                        }
index 8794429635bd0caec5f53b294dcd713464f720b2..e62d1fb1223bcbcd3a27e2ae5f31aa02b54ce8b1 100644 (file)
@@ -87,7 +87,8 @@ static int delete_node(struct udevice *dev)
        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);
                }
        }
diff --git a/udev.h b/udev.h
index 3b676acf40d99ab1a7a137dbbe49804b80f6ecde..5737c84c9d16fec99f1e337426b093522c580278 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -85,6 +85,18 @@ do { \
        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;