From b6d5da2330de7230c1e2e7e6f7075edfe21d433c Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Fri, 24 Apr 2026 17:35:47 -0500 Subject: [PATCH] Rework readlink to not depend on cstr nul termination --- src/fuse_readlink.cpp | 50 +++++++----------------- src/fuse_readlink.hpp | 2 +- vendored/libfuse/include/debug.hpp | 3 +- vendored/libfuse/include/fuse.h | 8 ++-- vendored/libfuse/include/fuse_lowlevel.h | 4 +- vendored/libfuse/lib/debug.cpp | 7 ++-- vendored/libfuse/lib/fuse.cpp | 25 +++++------- vendored/libfuse/lib/fuse_lowlevel.cpp | 9 +++-- 8 files changed, 44 insertions(+), 64 deletions(-) diff --git a/src/fuse_readlink.cpp b/src/fuse_readlink.cpp index 7bd9f91a..cb23a894 100644 --- a/src/fuse_readlink.cpp +++ b/src/fuse_readlink.cpp @@ -32,34 +32,15 @@ static -int -_readlink_core_standard(const fs::path &fullpath_, - char *buf_, - const size_t bufsize_) - -{ - int rv; - - rv = fs::readlink(fullpath_,buf_,(bufsize_ - 1)); - if(rv < 0) - return rv; - - buf_[rv] = '\0'; - - return 0; -} - -static -int +ssize_t _readlink_core_symlinkify(const fs::path &fullpath_, char *buf_, const size_t bufsize_, const time_t symlinkify_timeout_) { - int rv; - size_t n; - std::string fullpath_str; + ssize_t rv; struct stat st; + std::string fullpath_str; fullpath_str = fullpath_.string(); @@ -68,17 +49,16 @@ _readlink_core_symlinkify(const fs::path &fullpath_, return rv; if(!symlinkify::can_be_symlink(st,symlinkify_timeout_)) - return ::_readlink_core_standard(fullpath_,buf_,bufsize_); + return fs::readlink(fullpath_,buf_,bufsize_); - n = std::min(fullpath_str.size(),(bufsize_ - 1)); - memcpy(buf_,fullpath_str.c_str(),n); - buf_[n] = '\0'; + rv = std::min(fullpath_str.size(),bufsize_); + memcpy(buf_,fullpath_str.c_str(),rv); - return 0; + return rv; } static -int +ssize_t _readlink_core(const fs::path &basepath_, const fs::path &fusepath_, char *buf_, @@ -93,11 +73,11 @@ _readlink_core(const fs::path &basepath_, if(symlinkify_) return ::_readlink_core_symlinkify(fullpath,buf_,bufsize_,symlinkify_timeout_); - return ::_readlink_core_standard(fullpath,buf_,bufsize_); + return fs::readlink(fullpath,buf_,bufsize_); } static -int +ssize_t _readlink(const Policy::Search &searchFunc_, const Branches::Ptr ibranches_, const fs::path &fusepath_, @@ -106,7 +86,7 @@ _readlink(const Policy::Search &searchFunc_, const bool symlinkify_, const time_t symlinkify_timeout_) { - int rv; + ssize_t rv; std::vector obranches; rv = searchFunc_(ibranches_,fusepath_,obranches); @@ -123,16 +103,16 @@ _readlink(const Policy::Search &searchFunc_, symlinkify_timeout_); } -int +ssize_t FUSE::readlink(const fuse_req_ctx_t *ctx_, const char *fusepath_, char *buf_, size_t bufsize_) { - const fs::path fusepath{fusepath_}; - if(bufsize_ == 0) - return -EINVAL; + return 0; + + const fs::path fusepath{fusepath_}; return ::_readlink(cfg.func.readlink.policy, cfg.branches, diff --git a/src/fuse_readlink.hpp b/src/fuse_readlink.hpp index 3e5f8b35..531b33f1 100644 --- a/src/fuse_readlink.hpp +++ b/src/fuse_readlink.hpp @@ -25,7 +25,7 @@ namespace FUSE { - int + ssize_t readlink(const fuse_req_ctx_t *ctx, const char *fusepath, char *buf, diff --git a/vendored/libfuse/include/debug.hpp b/vendored/libfuse/include/debug.hpp index 26348935..4b42ecf7 100644 --- a/vendored/libfuse/include/debug.hpp +++ b/vendored/libfuse/include/debug.hpp @@ -43,7 +43,8 @@ void fuse_debug_entry_open_out(const uint64_t unique, const struct fuse_entry_out *earg, const struct fuse_open_out *oarg); void fuse_debug_readlink(const uint64_t unique, - const char *linkname); + const char *linkname, + const size_t linkname_len); void fuse_debug_write_out(const uint64_t unique, const struct fuse_write_out *arg); void fuse_debug_statfs_out(const uint64_t unique, diff --git a/vendored/libfuse/include/fuse.h b/vendored/libfuse/include/fuse.h index 1064e1a9..5d8b3598 100644 --- a/vendored/libfuse/include/fuse.h +++ b/vendored/libfuse/include/fuse.h @@ -70,10 +70,10 @@ struct fuse_operations const char *, struct stat *, fuse_timeouts_t *); - int (*readlink)(const fuse_req_ctx_t *, - const char *, - char *, - size_t); + ssize_t (*readlink)(const fuse_req_ctx_t *, + const char *, + char *, + size_t); int (*mknod)(const fuse_req_ctx_t *, const char *, mode_t, diff --git a/vendored/libfuse/include/fuse_lowlevel.h b/vendored/libfuse/include/fuse_lowlevel.h index 40394b84..6a982e09 100644 --- a/vendored/libfuse/include/fuse_lowlevel.h +++ b/vendored/libfuse/include/fuse_lowlevel.h @@ -241,7 +241,9 @@ int fuse_reply_statx(fuse_req_t *req, * @param link symbolic link contents * @return zero for success, -errno for failure to send reply */ -int fuse_reply_readlink(fuse_req_t *req, const char *link); +int fuse_reply_readlink(fuse_req_t *req, + const char *linkname, + const size_t linkname_len); /** * Reply with open parameters diff --git a/vendored/libfuse/lib/debug.cpp b/vendored/libfuse/lib/debug.cpp index 7dfd9027..d435a0d2 100644 --- a/vendored/libfuse/lib/debug.cpp +++ b/vendored/libfuse/lib/debug.cpp @@ -1734,7 +1734,8 @@ fuse_debug_entry_open_out(const uint64_t unique_, void fuse_debug_readlink(const uint64_t unique_, - const char *linkname_) + const char *linkname_, + const size_t linkname_len_) { auto output = fuse_cfg.log_file(); @@ -1748,8 +1749,8 @@ fuse_debug_readlink(const uint64_t unique_, "\n", _timestamp_ns(), unique_, - (sizeof(struct fuse_out_header) + strlen(linkname_)), - _quoted(linkname_)); + (sizeof(struct fuse_out_header) + linkname_len_), + _quoted(linkname_,linkname_len_)); } void diff --git a/vendored/libfuse/lib/fuse.cpp b/vendored/libfuse/lib/fuse.cpp index cf349e5d..2596391c 100644 --- a/vendored/libfuse/lib/fuse.cpp +++ b/vendored/libfuse/lib/fuse.cpp @@ -1729,29 +1729,24 @@ void fuse_lib_readlink(fuse_req_t *req_, struct fuse_in_header *hdr_) { - int err; + ssize_t rv; char *fusepath; char linkname[PATH_MAX + 1]; - err = get_path(hdr_->nodeid,&fusepath); - if(!err) + rv = get_path(hdr_->nodeid,&fusepath); + if(!rv) { - err = f.ops.readlink(&req_->ctx, - &fusepath[1], - linkname, - sizeof(linkname)); + rv = f.ops.readlink(&req_->ctx, + &fusepath[1], + linkname, + sizeof(linkname)); free_path(hdr_->nodeid,fusepath); } - if(!err) - { - linkname[PATH_MAX] = '\0'; - fuse_reply_readlink(req_,linkname); - } + if(rv >= 0) + fuse_reply_readlink(req_,linkname,rv); else - { - fuse_reply_err(req_,err); - } + fuse_reply_err(req_,rv); } static diff --git a/vendored/libfuse/lib/fuse_lowlevel.cpp b/vendored/libfuse/lib/fuse_lowlevel.cpp index 411168d2..9c06a940 100644 --- a/vendored/libfuse/lib/fuse_lowlevel.cpp +++ b/vendored/libfuse/lib/fuse_lowlevel.cpp @@ -357,16 +357,17 @@ fuse_reply_statx(fuse_req_t *req_, } int -fuse_reply_readlink(fuse_req_t *req, - const char *linkname) +fuse_reply_readlink(fuse_req_t *req, + const char *linkname, + const size_t linkname_len) { if(not linkname) return fuse_reply_err(req,EIO); if(fuse_cfg.debug) - fuse_debug_readlink(req->ctx.unique,linkname); + fuse_debug_readlink(req->ctx.unique,linkname,linkname_len); - return send_reply_ok(req, linkname, strlen(linkname)); + return send_reply_ok(req,linkname,linkname_len); } int