mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 11:19:52 +00:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
25
src/types.c
25
src/types.c
@@ -428,15 +428,34 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
|
|||||||
if (self->file_type == 'L') {
|
if (self->file_type == 'L') {
|
||||||
size_t bufsiz = ((size_t)(st.st_size + 1));
|
size_t bufsiz = ((size_t)(st.st_size + 1));
|
||||||
|
|
||||||
if (st.st_size == 0)
|
/* Some filesystems report st_size 0 for a symlink, so fall back to the
|
||||||
bufsiz = (size_t)sysconf(_PC_PATH_MAX);
|
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));
|
target = calloc(bufsiz, sizeof(char));
|
||||||
if (target == NULL)
|
if (target == NULL)
|
||||||
goto cleanup;
|
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;
|
goto cleanup;
|
||||||
|
if ((size_t)n >= bufsiz) {
|
||||||
|
errno = ENAMETOOLONG;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
target[n] = '\0';
|
||||||
}
|
}
|
||||||
if (self->target != NULL)
|
if (self->target != NULL)
|
||||||
free(self->target);
|
free(self->target);
|
||||||
|
|||||||
Reference in New Issue
Block a user