Repository: godotengine/godot (language: cpp)

Issue:
Print Chinese character cause debug instance unexpectedly closed
### Tested versions

4.4.1-stable

### System information

Windows 11 24H2

### Issue description

Currently using vscode with dap to debug godot projects. (launch setting from godot docs: https://docs.godotengine.org/en/stable/tutorials/editor/external_editor.html#lsp-dap-support)
When trying to print debug infos in Chinese and debug find that the game instance closes unexpectedly.
I did a custom build to get more info from dap, found out that vscode send disconnect command and godot closes the instance.

![Image](https://github.com/user-attachments/assets/f3c1c14c-56b0-48b5-88d8-ddb646bac35e).

After I disabled all prints, it worked fine.
I did a further test trying to make a mrp, but only found it's related to Chinese characters.






### Steps to reproduce

1. open the mrp, set a break point after print
2. press F5 to debug with dap

### Minimal reproduction project (MRP)

[mrp.zip](https://github.com/user-attachments/files/19561154/mrp.zip)


Discussion / hints:
CC @rsubtil 

Fix patch:
diff --git a/editor/debugger/debug_adapter/debug_adapter_protocol.cpp b/editor/debugger/debug_adapter/debug_adapter_protocol.cpp
index 35d841849c93..49249d856043 100644
--- a/editor/debugger/debug_adapter/debug_adapter_protocol.cpp
+++ b/editor/debugger/debug_adapter/debug_adapter_protocol.cpp
@@ -116,12 +116,12 @@ Error DAPeer::send_data() {
 		if (!data.has("seq")) {
 			data["seq"] = ++seq;
 		}
-		String formatted_data = format_output(data);
+		const Vector<uint8_t> &formatted_data = format_output(data);
 
 		int data_sent = 0;
-		while (data_sent < formatted_data.length()) {
+		while (data_sent < formatted_data.size()) {
 			int curr_sent = 0;
-			Error err = connection->put_partial_data((const uint8_t *)formatted_data.utf8().get_data(), formatted_data.size() - data_sent - 1, curr_sent);
+			Error err = connection->put_partial_data(formatted_data.ptr() + data_sent, formatted_data.size() - data_sent, curr_sent);
 			if (err != OK) {
 				return err;
 			}
@@ -132,15 +132,12 @@ Error DAPeer::send_data() {
 	return OK;
 }
 
-String DAPeer::format_output(const Dictionary &p_params) const {
-	String response = Variant(p_params).to_json_string();
-	String header = "Content-Length: ";
-	CharString charstr = response.utf8();
-	size_t len = charstr.length();
-	header += itos(len);
-	header += "\r\n\r\n";
+Vector<uint8_t> DAPeer::format_output(const Dictionary &p_params) const {
+	const Vector<uint8_t> &content = Variant(p_params).to_json_string().to_utf8_buffer();
+	Vector<uint8_t> response = vformat("Content-Length: %d\r\n\r\n", content.size()).to_utf8_buffer();
 
-	return header + response;
+	response.append_array(content);
+	return response;
 }
 
 Error DebugAdapterProtocol::on_client_connected() {
diff --git a/editor/debugger/debug_adapter/debug_adapter_protocol.h b/editor/debugger/debug_adapter/debug_adapter_protocol.h
index 23d1bdb1544b..9cd5741b19f9 100644
--- a/editor/debugger/debug_adapter/debug_adapter_protocol.h
+++ b/editor/debugger/debug_adapter/debug_adapter_protocol.h
@@ -67,7 +67,7 @@ struct DAPeer : RefCounted {
 
 	Error handle_data();
 	Error send_data();
-	String format_output(const Dictionary &p_params) const;
+	Vector<uint8_t> format_output(const Dictionary &p_params) const;
 };
 
 class DebugAdapterProtocol : public Object {


Test patch: