diff --git a/javascript/olm_outbound_group_session.js b/javascript/olm_outbound_group_session.js
index 0402c3c743a0ee70211035685ba23fe3e83b1bea..24ea644238132423bf44d2d6adf5f4d48bb16bee 100644
--- a/javascript/olm_outbound_group_session.js
+++ b/javascript/olm_outbound_group_session.js
@@ -83,6 +83,14 @@ OutboundGroupSession.prototype['encrypt'] = function(plaintext) {
             plaintext_buffer, plaintext_length,
             message_buffer, message_length
         );
+
+        // UTF8ToString requires a null-terminated argument, so add the
+        // null terminator.
+        Module['setValue'](
+            message_buffer+message_length,
+            0, "i8"
+        );
+
         return Module['UTF8ToString'](message_buffer);
     } finally {
         if (plaintext_buffer !== undefined) {
diff --git a/javascript/olm_post.js b/javascript/olm_post.js
index 3e80c0ba95487bbfb4264a560f1ef8bc78a1cc1b..65eab02f6455091314ab25e8e410013fa6210c61 100644
--- a/javascript/olm_post.js
+++ b/javascript/olm_post.js
@@ -335,6 +335,14 @@ Session.prototype['encrypt'] = restore_stack(function(
             random, random_length,
             message_buffer, message_length
         );
+
+        // UTF8ToString requires a null-terminated argument, so add the
+        // null terminator.
+        Module['setValue'](
+            message_buffer+message_length,
+            0, "i8"
+        );
+
         return {
             "type": message_type,
             "body": Module['UTF8ToString'](message_buffer),