diff options
author | Richard van der Hoff <github@rvanderhoff.org.uk> | 2016-12-15 16:54:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-15 16:54:39 +0000 |
commit | 7fd63bcac7110abd5a1eef927abc3184da68a35c (patch) | |
tree | 4f273f56830ef78ee42e954ab514df8d0471dd34 /javascript/olm_outbound_group_session.js | |
parent | 2e04868c468ba4d0362a209f8f261df649598a07 (diff) | |
parent | 09b3e1eecdd83c738a1bcd1aca7319206eaf7091 (diff) |
Merge pull request #39 from matrix-org/rav/messages_on_heap
Allocate memory for message blobs on the heap
Diffstat (limited to 'javascript/olm_outbound_group_session.js')
-rw-r--r-- | javascript/olm_outbound_group_session.js | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/javascript/olm_outbound_group_session.js b/javascript/olm_outbound_group_session.js index 88a441d..0402c3c 100644 --- a/javascript/olm_outbound_group_session.js +++ b/javascript/olm_outbound_group_session.js @@ -63,20 +63,38 @@ OutboundGroupSession.prototype['create'] = restore_stack(function() { ); }); -OutboundGroupSession.prototype['encrypt'] = restore_stack(function(plaintext) { - var plaintext_array = array_from_string(plaintext); - var message_length = outbound_group_session_method( - Module['_olm_group_encrypt_message_length'] - )(this.ptr, plaintext_array.length); - var plaintext_buffer = stack(plaintext_array); - var message_buffer = stack(message_length + NULL_BYTE_PADDING_LENGTH); - outbound_group_session_method(Module['_olm_group_encrypt'])( - this.ptr, - plaintext_buffer, plaintext_array.length, - message_buffer, message_length - ); - return Pointer_stringify(message_buffer); -}); +OutboundGroupSession.prototype['encrypt'] = function(plaintext) { + var plaintext_buffer, message_buffer, plaintext_length; + try { + plaintext_length = Module['lengthBytesUTF8'](plaintext); + + var message_length = outbound_group_session_method( + Module['_olm_group_encrypt_message_length'] + )(this.ptr, plaintext_length); + + // need to allow space for the terminator (which stringToUTF8 always + // writes), hence + 1. + plaintext_buffer = malloc(plaintext_length + 1); + Module['stringToUTF8'](plaintext, plaintext_buffer, plaintext_length + 1); + + 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 + ); + return Module['UTF8ToString'](message_buffer); + } finally { + if (plaintext_buffer !== undefined) { + // don't leave a copy of the plaintext in the heap. + bzero(plaintext_buffer, plaintext_length + 1); + free(plaintext_buffer); + } + if (message_buffer !== undefined) { + free(message_buffer); + } + } +}; OutboundGroupSession.prototype['session_id'] = restore_stack(function() { var length = outbound_group_session_method( |