Skip to content

Commit

Permalink
#186: Exporter's filename validations
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 authored and TokisanGames committed Nov 27, 2023
1 parent 27fcd73 commit b341ffc
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,11 +853,56 @@ Error Terrain3DStorage::export_image(String p_file_name, MapType p_map_type) {
LOG(ERROR, "No file specified. Nothing to export");
return FAILED;
}

if (get_region_count() == 0) {
LOG(ERROR, "No valid regions. Nothing to export");
return FAILED;
}

// Simple file name validation
static String bad_chars = "!@%^*~|\"";
for (int i = 0; i < bad_chars.length(); ++i)
{
for (int j = 0; j < p_file_name.length(); ++j)
{
if (bad_chars[i] == p_file_name[j])
{
LOG(ERROR, "Invalid file name '" + p_file_name + "'");
return FAILED;
}
}
}

// Update path delimeter
p_file_name = p_file_name.replace("\\", "/");

// Check if p_file_name is simple filename
bool isSimpleFileName = true;
for (int i = 0; i < p_file_name.length(); ++i)
{
char32_t c = p_file_name[i];
if (c == '/' || c == ':')
{
isSimpleFileName = false;
break;
}
}

if (isSimpleFileName)
{
// For simple filenames prepend "res://"
p_file_name = "res://" + p_file_name;
}

// Check if the file could be opened for writing
Ref<FileAccess> fileRef = FileAccess::open(p_file_name, FileAccess::ModeFlags::WRITE);
if (fileRef.is_null())
{
LOG(ERROR, "Could not open file '" + p_file_name + "' for writing");
return FAILED;
}
fileRef->close();

Ref<Image> img;
switch (p_map_type) {
case TYPE_HEIGHT:
Expand Down

0 comments on commit b341ffc

Please sign in to comment.