Skip to contentSkip to conversation

fix NAME="foo-%c{N}" gets a truncated name

PATCH Applied Patrick Mansfield 4 participants 8 messages Mar 3, 2004 – Mar 4, 2004

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix NAME="foo-%c{N}" gets a truncated name

Here is a fix and a new test for the problem Atul hit, where if we have a NAME based on a result of the form:

	NAME="foo-%c{7}"

udev truncates the name. Without any prefix (the foo- in this example), the rule was working OK.

=== namedev.c 1.122 vs edited ==--- 1.122/namedev.c Tue Mar 2 21:00:21 2004
+++ edited/namedev.c Wed Mar 3 14:16:54 2004
@@ -285,7 +285,7 @@
dbg("requested part of result string not found"); break;
}
- strnfieldcat(string, udev->program_result + spos, slen+1);
+ strnfieldcat(string, udev->program_result + spos, maxsize);
dbg("substitute part of result string '%s'", pos);
} else {
strnfieldcat(string, udev->program_result, maxsize);
=== test/udev-test.pl 1.40 vs edited ==--- 1.40/test/udev-test.pl Fri Feb 27 19:36:01 2004
+++ edited/test/udev-test.pl Wed Mar 3 14:20:05 2004
@@ -262,6 +262,15 @@
EOF
},
{

+		desc     => "lots of arguments and characters before %c{N}",
+		subsys   => "block",
+		devpath  => "block/sda/sda3",

+ expected => "my-foo9" ,
+ conf => <<EOF
+BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{7}" +EOF
+ },
+ {

 		desc     => "program result substitution (numbered part of)",
 		subsys   => "block",
 		devpath  => "block/sda/sda3",

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#1 lore raw
Quoted 11 lines · Patrick Mansfield

Here is a fix and a new test for the problem Atul hit, where if we have a NAME based on a result of the form:

	NAME="foo-%c{7}"

udev truncates the name. Without any prefix (the foo- in this example), the rule was working OK.

=== namedev.c 1.122 vs edited ==> --- 1.122/namedev.c Tue Mar 2 21:00:21 2004
+++ edited/namedev.c Wed Mar 3 14:16:54 2004

Review line 289

Quoted 6 lines
(quoted diff) +1 -1
@@ -285,7 +285,7 @@
 					dbg("requested part of result string not found");
 					break;
 				}
-				strnfieldcat(string, udev->program_result + spos, slen+1);
+				strnfieldcat(string, udev->program_result + spos, maxsize);
 				dbg("substitute part of result string '%s'", pos);

No, this doesn't work. The string part of the string is not \0 terminated, so we need to truncate after the computed length.

BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}"

TEST 24: lots of arguments and characters before %c{N} device 'block/sda/sda3' expecting node 'my-foo8'
add: error
udev-root/
`-- my-foo8 foo9

thanks,
Kay

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Patrick Mansfield
Quoted 7 lines · Patrick Mansfield

Here is a fix and a new test for the problem Atul hit, where if we have a NAME based on a result of the form:

	NAME="foo-%c{7}"

udev truncates the name. Without any prefix (the foo- in this example), the rule was working OK.

Here is a fix for the fix :)

Sorry, I broke it yesterday.

thanks,
Kay

===== namedev.c 1.122 vs edited =====

namedev.c +4 -2
@@ -214,6 +214,7 @@
 			 struct sysfs_device *sysfs_device)
 {
 	char temp[NAME_SIZE];
+	char temp2[NAME_SIZE];
 	char *tail;
 	char *pos;
 	char *attr;
@@ -277,6 +278,7 @@
 				i = atoi(attr);
 			if (i > 0) {
 				foreach_strpart(udev->program_result, " \n\r", spos, slen) {
+					strnfieldcpy(temp2, udev->program_result + spos, slen+1);
 					i--;
 					if (i == 0)
 						break;
@@ -285,8 +287,8 @@
 					dbg("requested part of result string not found");
 					break;
 				}
-				strnfieldcat(string, udev->program_result + spos, slen+1);
-				dbg("substitute part of result string '%s'", pos);
+				strnfieldcat(string, temp2, maxsize);
+				dbg("substitute part of result string '%s'", temp2);
 			} else {
 				strnfieldcat(string, udev->program_result, maxsize);
 				dbg("substitute result string '%s'", udev->program_result);
#3 lore raw
In reply to Patrick Mansfield
Quoted 4 lines · Kay Sievers
Quoted 3 lines · Patrick Mansfield

Here is a fix and a new test for the problem Atul hit, where if we have a NAME based on a result of the form:

Quoted 8 lines
Quoted 4 lines
	NAME="foo-%c{7}"

udev truncates the name. Without any prefix (the foo- in this example), the rule was working OK.

Here is a fix for the fix :)

Sorry, I broke it yesterday.

I have a patch without a temp string, but your patch works and is simpler.

Here is a patch for some new tests.

=== test/udev-test.pl 1.40 vs edited ==--- 1.40/test/udev-test.pl Fri Feb 27 19:36:01 2004
+++ edited/test/udev-test.pl Wed Mar 3 15:21:04 2004
@@ -262,6 +262,24 @@
EOF
},
{

+		desc     => "characters before the %c{N} substitution",
+		subsys   => "block",
+		devpath  => "block/sda/sda3",

+ expected => "my-foo9" ,
+ conf => <<EOF
+BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{7}" +EOF
+ },
+ {

+		desc     => "substitute the second to last argument",
+		subsys   => "block",
+		devpath  => "block/sda/sda3",

+ expected => "my-foo8" ,
+ conf => <<EOF
+BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}" +EOF
+ },
+ {

 		desc     => "program result substitution (numbered part of)",
 		subsys   => "block",
 		devpath  => "block/sda/sda3",

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

Applied
#4 lore raw
In reply to Patrick Mansfield
Quoted 4 lines · Kay Sievers
Quoted 3 lines · Patrick Mansfield

Here is a fix and a new test for the problem Atul hit, where if we have a NAME based on a result of the form:

Quoted 8 lines
Quoted 4 lines
	NAME="foo-%c{7}"

udev truncates the name. Without any prefix (the foo- in this example), the rule was working OK.

Here is a fix for the fix :)

Sorry, I broke it yesterday.

Applied, thanks.

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

Applied
#5 lore raw
In reply to Patrick Mansfield
Quoted 2 lines · Patrick Mansfield

Here is a patch for some new tests.

Applied, thanks.

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#6 lore raw
In reply to Patrick Mansfield
Quoted 5 lines · Greg KH
Quoted 2 lines · Patrick Mansfield

Here is a patch for some new tests.

Applied, thanks.

Here is a small improvement, which looks much better.

Hey Pat, thanks a lot for finding the recent bug, hope this one will not break it again :)

thanks,
Kay

===== namedev.c 1.123 vs edited =====

namedev.c +3 -2
@@ -220,8 +220,9 @@
 	char *attr;
 	int len;
 	int i;
-	int spos, slen;
 	char c;
+	char *spos;
+	int slen;
 	struct sysfs_attribute *tmpattr;
 
 	pos = string;
@@ -278,7 +279,6 @@
 				i = atoi(attr);
 			if (i > 0) {
 				foreach_strpart(udev->program_result, " \n\r", spos, slen) {
-					strnfieldcpy(temp2, udev->program_result + spos, slen+1);
 					i--;
 					if (i == 0)
 						break;
@@ -287,6 +287,7 @@
 					dbg("requested part of result string not found");
 					break;
 				}
+				strnfieldcpy(temp2, spos, slen+1);
 				strnfieldcat(string, temp2, maxsize);
 				dbg("substitute part of result string '%s'", temp2);
 			} else {

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

udev-add.c +3 -2
@@ -195,7 +195,8 @@
 	gid_t gid = 0;
 	int i;
 	int tail;
-	int pos, len;
+	char *pos;
+	int len;
 
 	strfieldcpy(filename, udev_root);
 	strfieldcat(filename, dev->name);
@@ -281,7 +282,7 @@
 
 	/* create symlink if requested */
 	foreach_strpart(dev->symlink, " ", pos, len) {
-		strnfieldcpy(linkname, dev->symlink + pos, len+1);
+		strnfieldcpy(linkname, pos, len+1);
 		strfieldcpy(filename, udev_root);
 		strfieldcat(filename, linkname);
 		dbg("symlink '%s' to node '%s' requested", filename, dev->name);

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

udev-remove.c +3 -2
@@ -72,7 +72,8 @@
 	char partitionname[NAME_SIZE];
 	int retval;
 	int i;
-	int pos, len;
+	char *pos;
+	int len;
 
 	strfieldcpy(filename, udev_root);
 	strfieldcat(filename, dev->name);
@@ -102,7 +103,7 @@
 		delete_path(filename);
 
 	foreach_strpart(dev->symlink, " ", pos, len) {
-		strnfieldcpy(linkname, dev->symlink + pos, len+1);
+		strnfieldcpy(linkname, pos, len+1);
 		strfieldcpy(filename, udev_root);
 		strfieldcat(filename, linkname);
 

===== udev.h 1.49 vs edited =====

udev.h +3 -3
@@ -98,9 +98,9 @@
 } while (0)
 
 #define foreach_strpart(str, separator, pos, len) \
-	for(pos = 0, len = strcspn(str, separator); \
-	    (pos) < strlen(str); \
-	    pos = pos + (len) + 1, len = strcspn((str) + pos, separator)) \
+	for(pos = str, len = 0; \
+	    (pos) < ((str) + strlen(str)); \
+	    pos = pos + len + strspn(pos, separator), len = strcspn(pos, separator)) \
 		if (len > 0)
 
 static inline char *get_action(void)

===== udevdb.c 1.25 vs edited =====

udevdb.c +3 -2
@@ -179,7 +179,8 @@
 
 static int find_device_by_name(char *path, struct udevice *dev)
 {
-	int pos, len;
+	char *pos;
+	int len;
 
 	if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) {
 		memcpy(find_dev, dev, sizeof(struct udevice));
@@ -190,7 +191,7 @@
 	}
 	/* look for matching symlink*/
 	foreach_strpart(dev->symlink, " ", pos, len) {
-		if (strncmp(&dev->symlink[pos], find_name, len) != 0)
+		if (strncmp(pos, find_name, len) != 0)
 			continue;
 
 		if (len != strlen(find_name))
Applied
#7 lore raw
In reply to Patrick Mansfield
Quoted 11 lines · Kay Sievers
Quoted 5 lines · Greg KH
Quoted 2 lines · Patrick Mansfield

Here is a patch for some new tests.

Applied, thanks.

Here is a small improvement, which looks much better.

Hey Pat, thanks a lot for finding the recent bug, hope this one will not break it again :)

Applied, thanks.

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel