From 416af29cc193951919369d86fa154e4016f5486b Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Sat, 21 Nov 2026 14:35:48 -0500 Subject: [PATCH] fix: keep temp file names within short NAME_MAX limits - fs_mktemp computed the prefix length as name_max - 6 - 2 in size_t. Below a NAME_MAX of 8 that wrapped, so the whole source file name was used and every temp file name was too long. moveonenospc and copy-on-write always failed on such a branch. - Shorten the random suffix when the limit is that small and never let the prefix length go negative. Names on normal filesystems are unchanged. --- src/fs_mktemp.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fs_mktemp.cpp b/src/fs_mktemp.cpp index 4ee3ba2a..02290d78 100644 --- a/src/fs_mktemp.cpp +++ b/src/fs_mktemp.cpp @@ -44,6 +44,7 @@ _generate_tmp_path(const fs::path &dirpath_, const std::string &src_filename_) { long name_max; + long pad_len; size_t substr_len; std::string tmp_filename; @@ -51,13 +52,16 @@ _generate_tmp_path(const fs::path &dirpath_, if(name_max == -1) name_max = NAME_MAX; + // The name is '.' + prefix + '_' + random suffix. Where names are + // too short for the full suffix, shorten it so the name still fits. + pad_len = std::clamp(name_max - 2L,1L,(long)PAD_LEN); substr_len = std::min(src_filename_.size(), - (size_t)(name_max - PAD_LEN - 2ULL)); + (size_t)std::max(name_max - 2L - pad_len,0L)); tmp_filename = '.'; tmp_filename += src_filename_.substr(0,substr_len); tmp_filename += '_'; - for(size_t i = 0; i < PAD_LEN; i++) + for(long i = 0; i < pad_len; i++) tmp_filename += CHARS[RND::rand64(CHARS_SIZE)]; return dirpath_ / tmp_filename;