summaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2012-05-24 12:56:31 -0400
committerNick Mathewson <nickm@torproject.org>2012-05-24 12:56:31 -0400
commit254504fc1494b1ca4eea14eb71bd5b4b5d0d2765 (patch)
tree1c856a23e6cb89a5cd81c9459f47ce98fd9a9596 /src/common/compat.c
parent75fc4dbbcabaedc715f0f9e883ccab1c9634e787 (diff)
downloadtor-254504fc1494b1ca4eea14eb71bd5b4b5d0d2765.tar.gz
tor-254504fc1494b1ca4eea14eb71bd5b4b5d0d2765.zip
Have get_parent_directory() handle "/foo" and "/" correctly.
The parent of "/foo" is "/"; and "/" is its own parent. This would cause Tor to fail if you tried to have a PF_UNIX control socket in the root directory. That would be a stupid thing to do for other reasons, but there's no reason to fail like _this_. Bug found by Esteban Manchado Velázquez. Fix for bug 5089; bugfix on Tor 0.2.2.26-beta. Unit test included.
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index a4e50747cd..ffd9724824 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1481,7 +1481,11 @@ get_user_homedir(const char *username)
}
#endif
-/** Modify <b>fname</b> to contain the name of the directory */
+/** Modify <b>fname</b> to contain the name of its parent directory. Doesn't
+ * actually examine the filesystem; does a purely syntactic modification.
+ *
+ * The parent of the root director is considered to be iteself.
+ * */
int
get_parent_directory(char *fname)
{
@@ -1503,13 +1507,18 @@ get_parent_directory(char *fname)
*/
cp = fname + strlen(fname);
at_end = 1;
- while (--cp > fname) {
+ while (--cp >= fname) {
int is_sep = (*cp == '/'
#ifdef MS_WINDOWS
|| *cp == '\\'
#endif
);
if (is_sep) {
+ if (cp == fname) {
+ /* This is the first separator in the file name; don't remove it! */
+ cp[1] = '\0';
+ return 0;
+ }
*cp = '\0';
if (! at_end)
return 0;