Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix editor crash when shader has incorrect global array declaration #90792

Merged
merged 1 commit into from
Jul 26, 2024

Conversation

jsjtxietian
Copy link
Contributor

@jsjtxietian jsjtxietian commented Apr 17, 2024

Trying to fix #90683

The crash happens around the change in shader_compiler.cpp, in that condition, cnode->array_declarations[0].initializer has size 0 and thus use i=1 to index it will crash the editor. I leave it as a safe guard there.

Note that const bool array[1] = bool[];; will cause the crash but const bool array[1] = bool[]; won't (just trigger Expected a ',' or ';'), the changes I made in shader_language.cpp prevent this by simply don't allow a global const array to be defined without initialization, I checked the doc it didn't say whether it's legal or not, so it might not be a good idea. Feel free to correct me.

@jsjtxietian jsjtxietian requested a review from a team as a code owner April 17, 2024 12:24
@AThousandShips AThousandShips added this to the 4.3 milestone Apr 17, 2024
Copy link
Member

@Chaosus Chaosus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please add

else {
_set_expected_error("(");
}

after the

if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
while (true) {
Node *n = _parse_and_reduce_expression(p_block, p_function_info);
if (!n) {
return ERR_PARSE_ERROR;
}
if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
_set_error(RTR("Expected a constant expression."));
return ERR_PARSE_ERROR;
}
if (!_compare_datatypes(var.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
return ERR_PARSE_ERROR;
}
tk = _get_token();
if (tk.type == TK_COMMA) {
decl.initializer.push_back(n);
continue;
} else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
decl.initializer.push_back(n);
break;
} else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
decl.initializer.push_back(n);
break;
} else {
if (curly) {
_set_expected_error("}", ",");
} else {
_set_expected_error(")", ",");
}
return ERR_PARSE_ERROR;
}
}
if (unknown_size) {
decl.size = decl.initializer.size();
var.array_size = decl.initializer.size();
} else if (decl.initializer.size() != var.array_size) {
_set_error(RTR("Array size mismatch."));
return ERR_PARSE_ERROR;
}
tk = _get_token();
}

(its local function case of the same problem)

@akien-mga akien-mga added the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Jul 23, 2024
@akien-mga akien-mga merged commit a50cead into godotengine:master Jul 26, 2024
18 checks passed
@akien-mga
Copy link
Member

Thanks!

@jsjtxietian jsjtxietian deleted the shader-crash branch July 26, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release crash topic:shaders
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Editor Crash When Using Invalid Array Initialization in Shader
5 participants