Skip to content

Commit d1a6fee

Browse files
authored
Merge pull request diffblue#338 from diffblue/bugfix/restrict_length_of_file_names_of_summaries
SEC-16: Bugfix/restrict length of file names of summaries
2 parents bd5441f + 0292567 commit d1a6fee

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

cbmc/src/util/file_util.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ std::string concat_dir_file(
161161
/// This is not designed to operate on path names and will replace folder
162162
/// seperator characters.
163163
/// \param file_name: The file name to sanitize.
164-
std::string make_valid_filename(std::string file_name)
164+
/// \param max_length: The maximum length for the file name. If the name is
165+
/// longer, then its length will be cut to the max_length.
166+
std::string make_valid_filename(
167+
std::string file_name,
168+
const std::size_t max_length)
165169
{
166170
std::replace(file_name.begin(), file_name.end(), '#', '_');
167171
std::replace(file_name.begin(), file_name.end(), '$', '_');
@@ -170,5 +174,7 @@ std::string make_valid_filename(std::string file_name)
170174
std::replace(file_name.begin(), file_name.end(), '\\', '.');
171175
std::replace(file_name.begin(), file_name.end(), '<', '[');
172176
std::replace(file_name.begin(), file_name.end(), '>', ']');
177+
if(file_name.size() > max_length)
178+
file_name.resize(max_length);
173179
return file_name;
174180
}

cbmc/src/util/file_util.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include <string>
1414

15+
/// This constant defines the maximum name length of any file created by
16+
/// any module of the CProver. The value of the constant was derived
17+
/// from the most restrictive file system we use on our workstations:
18+
/// 'ecryptfs'.
19+
const std::size_t MAX_FILE_NAME_LENGTH = 140;
20+
1521
void delete_directory(const std::string &path);
1622

1723
std::string get_current_working_directory();
1824

1925
std::string concat_dir_file(const std::string &directory,
2026
const std::string &file_name);
2127

22-
std::string make_valid_filename(std::string filename);
28+
std::string make_valid_filename(
29+
std::string filename,
30+
const std::size_t max_length = MAX_FILE_NAME_LENGTH);
2331

2432
#endif // CPROVER_UTIL_FILE_UTIL_H

src/util/json_map_serializer.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ class json_map_serializert:public virtual_map_iterated_by_keyt<keyt, valuet>
254254
path=get_existing_path(key);
255255
else
256256
{
257-
std::string file_name=make_valid_filename(key_converter(key));
257+
const std::size_t max_size =
258+
MAX_FILE_NAME_LENGTH // The general length limit.
259+
- 15 // Save space for unique ID integer.
260+
- std::strlen(suffix); // Save space for '.json' extension
261+
std::string file_name =
262+
make_valid_filename(key_converter(key), max_size);
258263
unsigned long uniqueness=file_name==index_file_name ? 1 : 0;
259264
while(index.right.count(uniqueify(file_name, uniqueness)) != 0)
260265
++uniqueness;

0 commit comments

Comments
 (0)