Skip to content

Commit 803ad84

Browse files
committed
FileLister: ensure stable sorting of files
1 parent c8fdceb commit 803ad84

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

cli/filelister.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,17 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
151151
if (path.empty())
152152
return "no path specified";
153153

154-
return addFiles2(files, path, extra, recursive, ignored);
154+
std::list<FileWithDetails> filesSorted;
155+
156+
std::string err = addFiles2(files, path, extra, recursive, ignored);
157+
158+
// files need to be sorted as the filesystems dosn't provide a stable order
159+
filesSorted.sort([](const FileWithDetails& a, const FileWithDetails& b) {
160+
return a.path() < b.path();
161+
});
162+
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
163+
164+
return err;
155165
}
156166

157167
#else
@@ -199,8 +209,6 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
199209
std::string new_path = path;
200210
new_path += '/';
201211

202-
std::list<FileWithDetails> filesSorted;
203-
204212
while (const dirent* dir_result = readdir(dir)) {
205213
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
206214
(std::strcmp(dir_result->d_name, "..") == 0))
@@ -216,7 +224,7 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
216224
#endif
217225
if (path_is_directory) {
218226
if (recursive && !ignored.match(new_path)) {
219-
std::string err = addFiles2(filesSorted, new_path, extra, recursive, ignored);
227+
std::string err = addFiles2(files, new_path, extra, recursive, ignored);
220228
if (!err.empty()) {
221229
return err;
222230
}
@@ -227,18 +235,11 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
227235
const int err = errno;
228236
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
229237
}
230-
filesSorted.emplace_back(new_path, file_stat.st_size);
238+
files.emplace_back(new_path, file_stat.st_size);
231239
}
232240
}
233241
}
234242

235-
// files inside directories need to be sorted as the filesystem doesn't provide a stable order
236-
filesSorted.sort([](const FileWithDetails& a, const FileWithDetails& b) {
237-
return a.path() < b.path();
238-
});
239-
240-
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
241-
242243
return "";
243244
}
244245

@@ -251,7 +252,17 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
251252
if (endsWith(corrected_path, '/'))
252253
corrected_path.erase(corrected_path.end() - 1);
253254

254-
return addFiles2(files, corrected_path, extra, recursive, ignored);
255+
std::list<FileWithDetails> filesSorted;
256+
257+
std::string err = addFiles2(files, corrected_path, extra, recursive, ignored);
258+
259+
// files need to be sorted as the filesystems dosn't provide a stable order
260+
filesSorted.sort([](const FileWithDetails& a, const FileWithDetails& b) {
261+
return a.path() < b.path();
262+
});
263+
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
264+
265+
return err;
255266
}
256267

257268
#endif

test/cli/other_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,15 +1411,15 @@ def test_filelist(tmpdir):
14111411
lines = stdout.splitlines()
14121412
expected = [
14131413
'Checking aaa.c ...',
1414-
'Checking valueflow.cpp ...',
14151414
'Checking valueflow/file.c ...',
14161415
'Checking valueflow/file.cpp ...',
1417-
'Checking vf_enumvalue.cpp ...',
1416+
'Checking valueflow.cpp ...',
14181417
'Checking vf_enumvalue/file.c ...',
14191418
'Checking vf_enumvalue/file.cpp ...',
1420-
'Checking vfvalue.cpp ...',
1419+
'Checking vf_enumvalue.cpp ...',
14211420
'Checking vfvalue/file.c ...',
14221421
'Checking vfvalue/file.cpp ...',
1422+
'Checking vfvalue.cpp ...',
14231423
'Checking zzz.c ...'
14241424
]
14251425
assert len(expected), len(lines)

0 commit comments

Comments
 (0)