Skip to content

Commit 05c45f3

Browse files
committed
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.
1 parent 2cc345b commit 05c45f3

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/libutil/util.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,14 @@ Path readLink(const Path & path)
219219
struct stat st = lstat(path);
220220
if (!S_ISLNK(st.st_mode))
221221
throw Error(format("'%1%' is not a symlink") % path);
222-
char buf[st.st_size];
223-
ssize_t rlsize = readlink(path.c_str(), buf, st.st_size);
222+
auto bufSize = std::max(st.st_size, (off_t) PATH_MAX + 1);
223+
char buf[bufSize];
224+
ssize_t rlsize = readlink(path.c_str(), buf, bufSize);
224225
if (rlsize == -1)
225226
throw SysError(format("reading symbolic link '%1%'") % path);
226-
else if (rlsize > st.st_size)
227+
else if (rlsize > bufSize)
227228
throw Error(format("symbolic link '%1%' size overflow %2% > %3%")
228-
% path % rlsize % st.st_size);
229+
% path % rlsize % bufSize);
229230
return string(buf, rlsize);
230231
}
231232

0 commit comments

Comments
 (0)