From d74749fd60c81fd682187ca5eaf08819e55d76d9 Mon Sep 17 00:00:00 2001 From: Chaosus Date: Sat, 10 Aug 2024 11:07:53 +0300 Subject: [PATCH] Fix shader crash when the comma used in `for` loop as a trailing --- servers/rendering/shader_language.cpp | 17 +++++++++++++++++ servers/rendering/shader_language.h | 1 + 2 files changed, 18 insertions(+) diff --git a/servers/rendering/shader_language.cpp b/servers/rendering/shader_language.cpp index 0235d72cfa0..b03bc2bd119 100644 --- a/servers/rendering/shader_language.cpp +++ b/servers/rendering/shader_language.cpp @@ -898,6 +898,13 @@ bool ShaderLanguage::_lookup_next(Token &r_tk) { return false; } +ShaderLanguage::Token ShaderLanguage::_peek() { + TkPos pre_pos = _get_tkpos(); + Token tk = _get_token(); + _set_tkpos(pre_pos); + return tk; +} + String ShaderLanguage::token_debug(const String &p_code) { clear(); @@ -8080,6 +8087,11 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_fun _set_error(RTR("The middle expression is expected to be a boolean operator.")); return ERR_PARSE_ERROR; } + tk = _peek(); + if (tk.type == TK_SEMICOLON) { + _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk))); + return ERR_PARSE_ERROR; + } continue; } if (tk.type != TK_SEMICOLON) { @@ -8088,6 +8100,11 @@ Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_fun } } else if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION) { if (tk.type == TK_COMMA) { + tk = _peek(); + if (tk.type == TK_PARENTHESIS_CLOSE) { + _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk))); + return ERR_PARSE_ERROR; + } continue; } if (tk.type != TK_PARENTHESIS_CLOSE) { diff --git a/servers/rendering/shader_language.h b/servers/rendering/shader_language.h index 4a67c8f2d2a..ad3b78546d1 100644 --- a/servers/rendering/shader_language.h +++ b/servers/rendering/shader_language.h @@ -1037,6 +1037,7 @@ private: Token _make_token(TokenType p_type, const StringName &p_text = StringName()); Token _get_token(); bool _lookup_next(Token &r_tk); + Token _peek(); ShaderNode *shader = nullptr;