diff --git a/src/code_generators.cpp b/src/code_generators.cpp index b7860e1b33..f5bc504d28 100644 --- a/src/code_generators.cpp +++ b/src/code_generators.cpp @@ -212,7 +212,14 @@ void GenComment(const std::vector& dc, std::string* code_ptr, ? config->content_line_prefix : "///"); for (auto it = dc.begin(); it != dc.end(); ++it) { - code += line_prefix + *it + "\n"; + std::string sanitized = *it; + // Sanitize comment content: escape block comment closing sequence + // to prevent code injection via premature comment termination. + for (size_t pos = sanitized.find("*/"); pos != std::string::npos; + pos = sanitized.find("*/", pos + 2)) { + sanitized.replace(pos, 2, "* /"); + } + code += line_prefix + sanitized + "\n"; } if (config != nullptr && config->last_line != nullptr) { code += std::string(prefix) + std::string(config->last_line) + "\n"; diff --git a/src/idl_gen_kotlin.cpp b/src/idl_gen_kotlin.cpp index e9a83c696e..3dc60f923a 100644 --- a/src/idl_gen_kotlin.cpp +++ b/src/idl_gen_kotlin.cpp @@ -1410,7 +1410,14 @@ class KotlinGenerator : public BaseGenerator { ? config->content_line_prefix : "///"); for (auto it = dc.begin(); it != dc.end(); ++it) { - writer += line_prefix + *it; + std::string sanitized = *it; + // Sanitize comment content: escape block comment closing sequence + // to prevent code injection via premature comment termination. + for (size_t pos = sanitized.find("*/"); pos != std::string::npos; + pos = sanitized.find("*/", pos + 2)) { + sanitized.replace(pos, 2, "* /"); + } + writer += line_prefix + sanitized; } if (config != nullptr && config->last_line != nullptr) { writer += std::string(config->last_line); diff --git a/src/idl_gen_kotlin_kmp.cpp b/src/idl_gen_kotlin_kmp.cpp index b9111eda9b..5473f027b0 100644 --- a/src/idl_gen_kotlin_kmp.cpp +++ b/src/idl_gen_kotlin_kmp.cpp @@ -1393,7 +1393,14 @@ class KotlinKMPGenerator : public BaseGenerator { ? config->content_line_prefix : "///"); for (auto it = dc.begin(); it != dc.end(); ++it) { - writer += line_prefix + *it; + std::string sanitized = *it; + // Sanitize comment content: escape block comment closing sequence + // to prevent code injection via premature comment termination. + for (size_t pos = sanitized.find("*/"); pos != std::string::npos; + pos = sanitized.find("*/", pos + 2)) { + sanitized.replace(pos, 2, "* /"); + } + writer += line_prefix + sanitized; } if (config != nullptr && config->last_line != nullptr) { writer += std::string(config->last_line);