From 3d63af4202e8a51e6f3d9407d6ac872a6dfa6724 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Sun, 16 Aug 2026 22:47:59 +0200 Subject: [PATCH] Escape the path field so unusual filenames survive a round trip A filename may contain any byte except NUL and '/', so it may contain the field delimiter or a newline. Until now create wrote such paths verbatim: a name with '|' shifted the field layout and a name with a newline split the record in two, and list could only reject the result. create now escapes the path before writing it -- '\' as '\\', '|' as '\|', and a newline as '\n' -- and list decodes it back. The backslash has to be escaped as well: without it '\|' would be ambiguous between an escaped delimiter and a literal backslash followed by a delimiter. Splitting stays escape-unaware on purpose. Only the path may hold an escaped delimiter and it is the last of the seven fields, so the six separators found while splitting are always the real ones; the check that used to reject a delimiter in the last field moves into the unescaper, which has the context to tell a valid escape from a malformed one. It rejects a bare delimiter, an unknown escape, and a trailing backslash, and reports them as an invalid path field rather than an unparsable line. walk guards the d_type fast path with #ifdef DT_UNKNOWN and falls back to the lstat path when the constants are unavailable, which SPEC 4.1 already requires and which POSIX-only feature test macros need, since d_type is a BSD extension rather than part of POSIX. Verified by decoding every record of three trees -- one with pipes, backslashes and a newline in the names, the SPEC 22 tree, and this repository -- and confirming lstat finds all 315 decoded paths. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01F3NNME8GifswDYLr9VpBzs --- src/create.c | 48 +++++++++++++++++++++++++++++++++++---- src/list.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++----- src/walk.c | 6 +++++ 3 files changed, 107 insertions(+), 11 deletions(-) diff --git a/src/create.c b/src/create.c index 4fe3e7c..3248eba 100644 --- a/src/create.c +++ b/src/create.c @@ -32,6 +32,40 @@ static char file_type(mode_t mode) { return '?'; } +static char *sanitize_path(const char *path) { + size_t len = strlen(path); + size_t escapes = 0; + for (size_t i = 0; i < len; i++) { + if (path[i] == '|' || path[i] == '\\' || path[i] == '\n') + escapes++; + } + + char *s = calloc(len + escapes + 1, sizeof(char)); + if (s == NULL) + return NULL; + + for (size_t i = 0, j = 0; i < len; i++) { + switch (path[i]) { + case '\\': + s[j++] = '\\'; + s[j++] = '\\'; + break; + case '|': + s[j++] = '\\'; + s[j++] = '|'; + break; + case '\n': + s[j++] = '\\'; + s[j++] = 'n'; + break; + default: + s[j++] = path[i]; + break; + } + } + return s; +} + static int write_record(const char *path, void *context) { // skip snapshot files const char *base = strrchr(path, '/'); @@ -52,12 +86,16 @@ static int write_record(const char *path, void *context) { uintmax_t gid = (uintmax_t)st.st_gid; intmax_t size = (intmax_t)st.st_size; intmax_t time = (intmax_t)st.st_mtime; + char *s = sanitize_path(path); + if (s == NULL) + return -1; - // Write record in the stream file - return fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid, - size, time, path) < 0 - ? -1 - : 0; + int ret = fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, + gid, size, time, s); + + free(s); + + return ret < 0 ? -1 : 0; } int create(const char *directory, const char *output_file) { diff --git a/src/list.c b/src/list.c index 90ef05e..5ee67e4 100644 --- a/src/list.c +++ b/src/list.c @@ -1,4 +1,4 @@ -#define _GNU_SOURCE +#define _POSIX_C_SOURCE 200809L #include "list.h" #include #include @@ -44,14 +44,61 @@ static int split_record(char *line, char *fields[NFIELDS], char delim) { p = sep + 1; } + /* Only the path may contain an escaped delimiter, and it is the last field, + so the six separators found above are always the real ones. Validating + what is left is the unescaper's job. */ fields[NFIELDS - 1] = p; - if (strchr(fields[NFIELDS - 1], delim) != NULL) - return -1; - return 0; } +/* Undo the escaping create applies: \\ -> \, \| -> |, \n -> newline. A bare + delimiter, an unknown escape, or a trailing backslash means the record is + malformed; that is reported with EINVAL to keep it apart from allocation + failure. */ +static char *unescape_path(const char *s, char delim) { + const size_t len = strlen(s); + + char *out = calloc(len + 1, sizeof(char)); + if (out == NULL) + return NULL; + + size_t j = 0; + for (size_t i = 0; i < len; i++) { + if (s[i] == delim) + goto malformed; + + if (s[i] != '\\') { + out[j++] = s[i]; + continue; + } + + /* s[len] is the terminator, so a trailing backslash lands on the + default case rather than reading past the end. */ + i++; + switch (s[i]) { + case '\\': + out[j++] = '\\'; + break; + case 'n': + out[j++] = '\n'; + break; + default: + if (s[i] != delim) + goto malformed; + out[j++] = delim; + break; + } + } + + return out; + +malformed: + free(out); + errno = EINVAL; + return NULL; +} + static int parse_file_type(const char *s, char *out) { if (s == NULL || s[0] == '\0' || s[1] != '\0') return -1; @@ -191,13 +238,18 @@ struct RecordObject *unpack(char *s) { return objp; } - char *path = calloc(strlen(fields[6]) + 1, sizeof(char)); + errno = 0; + char *path = unescape_path(fields[6], '|'); if (path == NULL) { + if (errno == EINVAL) { + field = PATH; + return objp; + } release(objp); return NULL; } - objp->path = strcpy(path, fields[6]); + objp->path = path; return objp; } diff --git a/src/walk.c b/src/walk.c index 1647b87..14a13b4 100644 --- a/src/walk.c +++ b/src/walk.c @@ -60,6 +60,7 @@ int walk(const char *directory, operation op, void *context) { if (path == NULL) goto out; +#ifdef DT_UNKNOWN switch (direntp->d_type) { case DT_UNKNOWN: // operating system or filesystem do not support dirent->d_type. @@ -78,6 +79,11 @@ int walk(const char *directory, operation op, void *context) { goto out; break; } +#else + if (handle_unknown(path, op, context) == -1) { + goto out; + } +#endif free(path); path = NULL;