From 695a0f37d7640a27b9dde07df282480ed1e10576 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 15:09:33 +0200 Subject: [PATCH] Read symlink targets with pathconf, and detect truncation _PC_PATH_MAX belongs to pathconf, not sysconf. On glibc _PC_PATH_MAX and _SC_OPEN_MAX are both 4, so sysconf(_PC_PATH_MAX) silently returned the open-file limit (524288 here) and the fallback allocated 512KB per link. Where the two constants do not collide sysconf returns -1, which becomes SIZE_MAX and makes calloc fail, so create would break on every symlink whose st_size is 0. readlink also does not terminate the buffer and truncates silently when the target does not fit, so check the returned length rather than only testing for -1. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- src/types.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/types.c b/src/types.c index edc38fb..5f41440 100644 --- a/src/types.c +++ b/src/types.c @@ -428,15 +428,34 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) { if (self->file_type == 'L') { size_t bufsiz = ((size_t)(st.st_size + 1)); - if (st.st_size == 0) - bufsiz = (size_t)sysconf(_PC_PATH_MAX); + /* Some filesystems report st_size 0 for a symlink, so fall back to the + longest path the filesystem holding it accepts. pathconf may legitimately + report "no limit" as -1 without setting errno, hence the reset. */ + if (st.st_size == 0) { + errno = 0; + long limit = pathconf(path, _PC_PATH_MAX); + if (limit <= 0) { + if (errno != 0) + goto cleanup; + limit = PATH_MAX; + } + bufsiz = (size_t)limit; + } target = calloc(bufsiz, sizeof(char)); if (target == NULL) goto cleanup; - if (readlink(path, target, bufsiz) == -1) + /* readlink does not terminate the buffer and silently truncates when the + target does not fit, so the length has to be checked, not just -1. */ + ssize_t n = readlink(path, target, bufsiz); + if (n == -1) goto cleanup; + if ((size_t)n >= bufsiz) { + errno = ENAMETOOLONG; + goto cleanup; + } + target[n] = '\0'; } if (self->target != NULL) free(self->target);