Skip to content

Commit 8c16af1

Browse files
committed
Add some validation
1 parent ebbe96e commit 8c16af1

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class SceneForwardClusteredShaderRD : public ShaderRD {
4343
// static const char _fragment_code[];
4444
// static const char *_compute_code = nullptr;
4545
ShaderLoaderRD::ShaderLoadResult res = ShaderLoaderRD::get_singleton()->load_shader_file("./shaders/forward_clustered/scene_forward_clustered.glsl");
46+
ERR_FAIL_COND_MSG(res.error, "Failed to load shader file: ./shaders/forward_clustered/scene_forward_clustered.glsl");
4647

4748
setup(res.vertex_code, res.fragment_code, res.compute_code, "SceneForwardClusteredShaderRD");
4849
}

servers/rendering/renderer_rd/shader_loader_rd.cpp

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ ShaderLoaderRD::~ShaderLoaderRD() {
4747
singleton = nullptr;
4848
}
4949

50-
static void _process_shader_file(const String &p_path, Vector<String> &r_vertex_lines, Vector<String> &r_fragment_lines, Vector<String> &r_compute_lines, HashSet<String> &r_vertex_included, HashSet<String> &r_fragment_included, HashSet<String> &r_compute_included, int p_depth);
50+
static bool _process_shader_file(const String &p_path, Vector<String> &r_vertex_lines, Vector<String> &r_fragment_lines, Vector<String> &r_compute_lines, HashSet<String> &r_vertex_included, HashSet<String> &r_fragment_included, HashSet<String> &r_compute_included, int p_depth);
5151

52-
static void _include_shader_file(const String &p_path, Vector<String> &r_lines, HashSet<String> &r_included_files, int p_depth) {
52+
static bool _include_shader_file(const String &p_path, Vector<String> &r_lines, HashSet<String> &r_included_files, int p_depth) {
5353
if (r_included_files.has(p_path)) {
54-
return;
54+
return true;
5555
}
5656

5757
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ);
58-
ERR_FAIL_COND_MSG(file.is_null(), vformat("Shader include file does not exist: %s", p_path));
58+
if (file.is_null()) {
59+
ERR_PRINT(vformat("Shader include file does not exist: %s", p_path));
60+
return false;
61+
}
5962

6063
r_included_files.insert(p_path);
6164

@@ -81,21 +84,28 @@ static void _include_shader_file(const String &p_path, Vector<String> &r_lines,
8184
if (include_file.begins_with("thirdparty/")) {
8285
resolved_path = include_file;
8386
} else {
84-
String base_dir = p_path.get_base_dir();
85-
resolved_path = base_dir.path_join(include_file).simplify_path();
86-
}
87+
String base_dir = p_path.get_base_dir();
88+
resolved_path = base_dir.path_join(include_file).simplify_path();
89+
}
8790

88-
_include_shader_file(resolved_path, r_lines, r_included_files, p_depth + 1);
91+
if (!_include_shader_file(resolved_path, r_lines, r_included_files, p_depth + 1)) {
92+
return false;
93+
}
8994
}
9095
} else {
9196
r_lines.push_back(line);
9297
}
9398
}
99+
100+
return true;
94101
}
95102

96-
static void _process_shader_file(const String &p_path, Vector<String> &r_vertex_lines, Vector<String> &r_fragment_lines, Vector<String> &r_compute_lines, HashSet<String> &r_vertex_included, HashSet<String> &r_fragment_included, HashSet<String> &r_compute_included, int p_depth) {
103+
static bool _process_shader_file(const String &p_path, Vector<String> &r_vertex_lines, Vector<String> &r_fragment_lines, Vector<String> &r_compute_lines, HashSet<String> &r_vertex_included, HashSet<String> &r_fragment_included, HashSet<String> &r_compute_included, int p_depth) {
97104
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ);
98-
ERR_FAIL_COND_MSG(file.is_null(), vformat("Shader file does not exist: %s", p_path));
105+
if (file.is_null()) {
106+
ERR_PRINT(vformat("Shader file does not exist: %s", p_path));
107+
return false;
108+
}
99109

100110
enum Section {
101111
SECTION_NONE,
@@ -155,14 +165,18 @@ static void _process_shader_file(const String &p_path, Vector<String> &r_vertex_
155165

156166
// Only include if not already included in this section
157167
if (!current_included->has(resolved_path)) {
158-
_include_shader_file(resolved_path, *current_lines, *current_included, p_depth + 1);
168+
if (!_include_shader_file(resolved_path, *current_lines, *current_included, p_depth + 1)) {
169+
return false;
170+
}
159171
}
160172
}
161173
} else {
162174
current_lines->push_back(line);
163175
}
164176
}
165177
}
178+
179+
return true;
166180
}
167181

168182
ShaderLoaderRD::ShaderLoadResult ShaderLoaderRD::load_shader_file(const String &p_path) {
@@ -177,9 +191,18 @@ ShaderLoaderRD::ShaderLoadResult ShaderLoaderRD::load_shader_file(const String &
177191
HashSet<String> compute_included;
178192

179193
Ref<DirAccess> dir = DirAccess::open(p_path.get_base_dir());
194+
if (dir.is_null()) {
195+
ERR_PRINT(vformat("Failed to open directory for shader file: %s", p_path));
196+
result.error = true;
197+
return result;
198+
}
199+
180200
String s = dir->get_full_path(p_path, DirAccess::ACCESS_FILESYSTEM);
181201

182-
_process_shader_file(p_path, vertex_lines, fragment_lines, compute_lines, vertex_included, fragment_included, compute_included, 0);
202+
if (!_process_shader_file(p_path, vertex_lines, fragment_lines, compute_lines, vertex_included, fragment_included, compute_included, 0)) {
203+
result.error = true;
204+
return result;
205+
}
183206

184207
result.vertex_code = String("\n").join(vertex_lines);
185208
result.fragment_code = String("\n").join(fragment_lines);

servers/rendering/renderer_rd/shader_loader_rd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ShaderLoaderRD {
4343
String fragment_code;
4444
String vertex_code;
4545
String compute_code;
46+
bool error = false;
4647
};
4748

4849
ShaderLoadResult load_shader_file(const String &p_path);

0 commit comments

Comments
 (0)