From ebc156e7c272e08e375c3e704651b179541e078b Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Mon, 8 Apr 2019 15:54:02 -0400 Subject: re-add null termination in javascript because older versions of emscripten don't support the length argument to UTF8ToString. --- javascript/olm_outbound_group_session.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'javascript/olm_outbound_group_session.js') diff --git a/javascript/olm_outbound_group_session.js b/javascript/olm_outbound_group_session.js index e4427cc..e4852c1 100644 --- a/javascript/olm_outbound_group_session.js +++ b/javascript/olm_outbound_group_session.js @@ -28,7 +28,7 @@ OutboundGroupSession.prototype['pickle'] = restore_stack(function(key) { Module['_olm_pickle_outbound_group_session_length'] )(this.ptr); var key_buffer = stack(key_array); - var pickle_buffer = stack(pickle_length); + var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH); try { outbound_group_session_method(Module['_olm_pickle_outbound_group_session'])( this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length @@ -86,13 +86,20 @@ OutboundGroupSession.prototype['encrypt'] = function(plaintext) { plaintext_buffer = malloc(plaintext_length + 1); stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1); - message_buffer = malloc(message_length); + message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH); outbound_group_session_method(Module['_olm_group_encrypt'])( this.ptr, plaintext_buffer, plaintext_length, message_buffer, message_length ); + // UTF8ToString requires a null-terminated argument, so add the + // null terminator. + setValue( + message_buffer+message_length, + 0, "i8" + ); + return UTF8ToString(message_buffer, message_length); } finally { if (plaintext_buffer !== undefined) { @@ -110,7 +117,7 @@ OutboundGroupSession.prototype['session_id'] = restore_stack(function() { var length = outbound_group_session_method( Module['_olm_outbound_group_session_id_length'] )(this.ptr); - var session_id = stack(length); + var session_id = stack(length + NULL_BYTE_PADDING_LENGTH); outbound_group_session_method(Module['_olm_outbound_group_session_id'])( this.ptr, session_id, length ); @@ -121,7 +128,7 @@ OutboundGroupSession.prototype['session_key'] = restore_stack(function() { var key_length = outbound_group_session_method( Module['_olm_outbound_group_session_key_length'] )(this.ptr); - var key = stack(key_length); + var key = stack(key_length + NULL_BYTE_PADDING_LENGTH); outbound_group_session_method(Module['_olm_outbound_group_session_key'])( this.ptr, key, key_length ); -- cgit v1.2.3