Skip to contentSkip to conversation

change no_trailing_slash() to remove all trailing slashes

PATCH Applied Michael Buesch 3 participants 6 messages Feb 5, 2005 – Feb 6, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 change no_trailing_slash() to remove all trailing slashes

Hi,

This changes no_trailing_slash() to remove all trailing slashes and not just the last one.
This protects against crazy users which do:
export SYSFS_PATH="/sys//"
or something similiar.

Signature · 1 line
Regards Michael Buesch  [ http://www.tuxsoft.de.vu ]

===== udev_utils.c 1.25 vs edited =====

udev_utils.c +3 -3
@@ -200,11 +200,11 @@
 
 void no_trailing_slash(char *path)
 {
-	int len;
+	size_t len;
 
 	len = strlen(path);
-	if (len > 0 && path[len-1] == '/')
-		path[len-1] = '\0';
+	while (len > 0 && path[len-1] == '/')
+		path[--len] = '\0';
 }
 
 struct files {
Applied
#1 lore raw

Re: [PATCH, UDEV] change no_trailing_slash() to remove all

Quoted 5 lines · Michael Buesch

This changes no_trailing_slash() to remove all trailing slashes and not just the last one.
This protects against crazy users which do:
export SYSFS_PATH="/sys//"
or something similiar.

Applied.

Thanks,
Kay

-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting Tool for open source databases. Create drag-&-drop reports. Save time by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc. Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw

Re: [PATCH, UDEV] change no_trailing_slash() to remove all trailing

In reply to Michael Buesch
Quoted 1 line · Michael
	while (len > 0 && path[len-1] = '/')

I think you'd better make that:

	while (len > 1 && path[len-1] = '/')

or else you can eat your last slash.

The call no_trailing_slash("/") should produce "/", not "".

Not that anyone is likely to call no_trailing_slash("////") very often ;).

Signature · 14 lines
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <[email protected]> 1.650.933.1373, 1.925.600.0401


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#3 lore raw
In reply to Michael Buesch

Quoting Paul Jackson <[email protected]>:

Quoted 10 lines
Quoted 1 line · Michael
	while (len > 0 && path[len-1] == '/')

I think you'd better make that:

	while (len > 1 && path[len-1] == '/')

or else you can eat your last slash.

The call no_trailing_slash("/") should produce "/", not "".

Well, I would call that expected behaviour.
Consider the following:
char buf[SIZE];
strcpy(buf, "///");
no_trailing_slash(buf);
strcat(buf, "/foo/bar");

I would expect that to expand to "/foo/bar" and _not_ to "//foo/bar" as you suggest.

Quoted 3 lines

Not that anyone is likely to call no_trailing_slash("////") very often ;).

Signature · 1 line
Regards Michael Buesch  [ http://www.tuxsoft.de.vu ]
#4 lore raw

Re: [PATCH, UDEV] change no_trailing_slash() to remove all trailing

In reply to Michael Buesch

What if someone does:

    strcpy(buf, "/");
    no_trailing_slash(buf);
    strcat(buf, "foo");

Would you expect "foo", or "/foo" ?

Actually, I did not expect this to be called in the middle of a construction, after only explicit constant strings had been added.

I would have expected it to be called after the final construction, following the concatenation of a variable string.

And I would have expected it to scrupulously avoid changing the meaning of the path - just cosmetic trimming.

But apparently my expectations are confused. Feel free to dismiss my comments.

Signature · 14 lines
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <[email protected]> 1.650.933.1373, 1.925.600.0401


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#5 lore raw
In reply to Michael Buesch

Quoting Paul Jackson <[email protected]>:

Quoted 7 lines

What if someone does:

    strcpy(buf, "/");
    no_trailing_slash(buf);
    strcat(buf, "foo");

Would you expect "foo", or "/foo" ?

I would expect "foo".
It's the function semantics:
no_trailing_slash means (at least for me) clearly,
that there will be "no trailing slash" under any circumstances in the string.
It should be the caller's job to ensure, that we have a valid path after cutting of all trailing slashes.

	strcpy(buf, "/");
	no_trailing_slash(buf);
	if (buf[0] == '\0')
		strcat(buf, "/");
	strcat(buf, "foo");

But having saied this all, I can't find where this actually happens in the udev code. I did a very quick search even before I submitted the code, but I could only find places like this, which will always result in a valid path:

	no_trailing_slash(buf);
	strcat(buf, "/foo");
Quoted 14 lines

Actually, I did not expect this to be called in the middle of a construction, after only explicit constant strings had been added.

I would have expected it to be called after the final construction, following the concatenation of a variable string.

And I would have expected it to scrupulously avoid changing the meaning of the path - just cosmetic trimming.

But apparently my expectations are confused. Feel free to dismiss my comments.

Signature · 1 line
Regards Michael Buesch  [ http://www.tuxsoft.de.vu ]