Skip to content

Commit 0630687

Browse files
chencheng maoclaude
andcommitted
Name the offending path in the NUL character error and add a test
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011BxKvJgr98DbCiLT3aKnmu
1 parent 87659d0 commit 0630687

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎cli/filelister.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
168168
if (path.empty())
169169
return "no path specified";
170170

171-
if (path.find('\0') != std::string::npos)
172-
return "path contains a NUL character";
171+
const std::string::size_type nulPos = path.find('\0');
172+
if (nulPos != std::string::npos)
173+
return "path '" + path.substr(0, nulPos) + "' contains a NUL character";
173174

174175
std::list<FileWithDetails> filesSorted;
175176

@@ -288,8 +289,9 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
288289
if (path.empty())
289290
return "no path specified";
290291

291-
if (path.find('\0') != std::string::npos)
292-
return "path contains a NUL character";
292+
const std::string::size_type nulPos = path.find('\0');
293+
if (nulPos != std::string::npos)
294+
return "path '" + path.substr(0, nulPos) + "' contains a NUL character";
293295

294296
std::string corrected_path = path;
295297
if (endsWith(corrected_path, '/'))

‎test/testfilelister.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestFileLister : public TestFixture {
3636
void run() override {
3737
TEST_CASE(recursiveAddFiles);
3838
TEST_CASE(recursiveAddFilesEmptyPath);
39+
TEST_CASE(recursiveAddFilesNulInPath);
3940
TEST_CASE(excludeFile1);
4041
TEST_CASE(excludeFile2);
4142
TEST_CASE(excludeDir);
@@ -110,6 +111,13 @@ class TestFileLister : public TestFixture {
110111
ASSERT_EQUALS("no path specified", err);
111112
}
112113

114+
void recursiveAddFilesNulInPath() const {
115+
std::list<FileWithDetails> files;
116+
const std::string err = FileLister::recursiveAddFiles(files, std::string("lib\0", 4), {}, PathMatch());
117+
ASSERT_EQUALS("path 'lib' contains a NUL character", err);
118+
ASSERT(files.empty());
119+
}
120+
113121
void excludeFile1() const {
114122
const std::string basedir = findBaseDir();
115123

0 commit comments

Comments
 (0)