Patch #2564 2008-09-18 21:33

gryphon

Fix bug in globals.cpp realpath() preventing garbage paths
Download
2564-Fix_bug_in_glo.patch (697 bytes)
Category
Application::Bugfix
Status
Accepted
Close date
2009-01-16 09:43
Assigned to
mortenmacfly
Index: globals.cpp
===================================================================
--- globals.cpp    (revision 5206)
+++ globals.cpp    (working copy)
@@ -1027,6 +1027,7 @@
             if (S_ISLNK(buffer.st_mode))
             {
                 int s = readlink(ret.substr(0, slashPos).c_str(), buf, sizeof(buf));
+                buf[s] = 0;
                 if (s > 0 && buf[0] != '/' && buf[0] != '~')
                 {
                     // relative
@@ -1036,6 +1037,11 @@
                 {
                     // absolute
                     ret = buf + ret.substr(slashPos, ret.size() - slashPos);
+
+                    // start again at the beginning in case the path returned also
+                    // has symlinks. For example if using /etc/alternatives this will
+                    // be the case
+                    s = 0;
                 }
                 slashPos = s;
             }
gryphon 2008-09-18 21:36

This patch does two things.

First it prevents garbage paths by setting buf[s] to 0 so previous calls with longer links don't corrupt the newer links in the path.

Second, setting s to zero for absolute links allows links to links to be correctly resolved. For example if I had,

/nfs -> /etc/alternatives/nfs

and this then pointed to,

/etc/alternatives/nfs -> /local_nfs

it would get resolved correctly.