ctrl: Pre-calculate required size before allocating msgb

This commit fixes crash when response is more than ~4096 chars.
Furthermore, we now allocate only the required memory, not 4096 for all
messages, which usually don't require it.
Test needs to be adapted since it assumed there was more available space
at the end of the msgb.

Related: OS#5169
Change-Id: I0b8f370f7b08736207f9efed13a0663b5e482824
diff --git a/tests/ctrl/ctrl_test.c b/tests/ctrl/ctrl_test.c
index b46e9ac..01fb9f7 100644
--- a/tests/ctrl/ctrl_test.c
+++ b/tests/ctrl/ctrl_test.c
@@ -117,11 +117,14 @@
 	} else {
 		struct msgb *sent_msg = msgb_dequeue(&ccon->write_queue.msg_queue);
 		OSMO_ASSERT(sent_msg);
-		msgb_put_u8(sent_msg, 0);
 
-		printf("replied: '%s'\n", osmo_escape_str((char*)msgb_l2(sent_msg), -1));
+		char *strbuf = talloc_size(sent_msg, msgb_l2len(sent_msg) + 1);
+		memcpy(strbuf, msgb_l2(sent_msg), msgb_l2len(sent_msg));
+		strbuf[msgb_l2len(sent_msg)] = '\0';
+
+		printf("replied: '%s'\n", osmo_escape_str(strbuf, -1));
 		OSMO_ASSERT(t->reply_str);
-		OSMO_ASSERT(!strcmp(t->reply_str, (char*)msgb_l2(sent_msg)));
+		OSMO_ASSERT(!strcmp(t->reply_str, strbuf));
 		msgb_free(sent_msg);
 	}
 	osmo_wqueue_clear(&ccon->write_queue);