about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-08-29T09·51+0200
committerEelco Dolstra <edolstra@gmail.com>2017-08-29T09·52+0200
commit05c45f301de6f4b5528969b97b9cc1891b1a985c (patch)
treefdbe494ba5c8aecef2aa59f2156143e214c7edee
parent2cc345b95f19334e7050c7203a5f6823af24344f (diff)
readLink(): Handle symlinks in /proc
Symlinks like /proc/self/exe report a stat() size of 0, so use a
buffer of at least PATH_MAX instead.
-rw-r--r--src/libutil/util.cc9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 2f410f7e3fd5..605e89ce27ff 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -219,13 +219,14 @@ Path readLink(const Path & path)
     struct stat st = lstat(path);
     if (!S_ISLNK(st.st_mode))
         throw Error(format("'%1%' is not a symlink") % path);
-    char buf[st.st_size];
-    ssize_t rlsize = readlink(path.c_str(), buf, st.st_size);
+    auto bufSize = std::max(st.st_size, (off_t) PATH_MAX + 1);
+    char buf[bufSize];
+    ssize_t rlsize = readlink(path.c_str(), buf, bufSize);
     if (rlsize == -1)
         throw SysError(format("reading symbolic link '%1%'") % path);
-    else if (rlsize > st.st_size)
+    else if (rlsize > bufSize)
         throw Error(format("symbolic link '%1%' size overflow %2% > %3%")
-            % path % rlsize % st.st_size);
+            % path % rlsize % bufSize);
     return string(buf, rlsize);
 }