From b482321213e6e896d0981c266bed12f4e1f67441 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 4 Oct 2019 11:43:40 +0100 Subject: [PATCH] Pass in a buffer to olm_session_describe instead of having a static one, as that could end up taking up a lot of memory if your app keeps olm sessions hanging about. --- include/olm/olm.h | 8 +++----- include/olm/session.hh | 14 +++++--------- javascript/olm_post.js | 14 ++++++++++---- src/olm.cpp | 6 +++--- src/session.cpp | 16 ++++++++-------- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/include/olm/olm.h b/include/olm/olm.h index 242277a..a0ef46a 100644 --- a/include/olm/olm.h +++ b/include/olm/olm.h @@ -319,12 +319,10 @@ int olm_session_has_received_message( ); /** - * Return a string describing the internal state of an olm session, - * for debugging and logging purposes. The pointer returned points - * to an internal buffer and is valid until the next call to - * olm_session_describe on the same olm session object. + * Write a string describing the internal state of an olm session + * to the buffer provided for debugging and logging purposes. */ -const char * olm_session_describe(OlmSession * session); +void olm_session_describe(OlmSession * session, char *buf, size_t buflen); /** Checks if the PRE_KEY message is for this in-bound session. This can happen * if multiple messages are sent to this account before this account sends a diff --git a/include/olm/session.hh b/include/olm/session.hh index 86598dd..ce05fc8 100644 --- a/include/olm/session.hh +++ b/include/olm/session.hh @@ -17,8 +17,6 @@ #include "olm/ratchet.hh" -#define DESCRIBE_BUFFER_LEN 256 - namespace olm { struct Account; @@ -37,8 +35,6 @@ struct Session { bool received_message; - char describe_buffer[DESCRIBE_BUFFER_LEN]; - _olm_curve25519_public_key alice_identity_key; _olm_curve25519_public_key alice_base_key; _olm_curve25519_public_key bob_one_time_key; @@ -137,12 +133,12 @@ struct Session { ); /** - * Return a string describing this session and its state (not including the - * private key) - * The pointer returned refers to an internal buffer which will be valid - * up until the next time describe() is called. + * Write a string describing this session and its state (not including the + * private key) into the buffer provided. + * + * Takes a buffer to write to and the length of that buffer */ - const char *describe(); + void describe(char *buf, size_t buflen); }; diff --git a/javascript/olm_post.js b/javascript/olm_post.js index c0498db..e204f30 100644 --- a/javascript/olm_post.js +++ b/javascript/olm_post.js @@ -460,10 +460,16 @@ Session.prototype['decrypt'] = restore_stack(function( }); Session.prototype['describe'] = restore_stack(function() { - var description_buf = session_method(Module['_olm_session_describe'])( - this.ptr - ); - return UTF8ToString(description_buf); + var description_buf; + try { + description_buf = malloc(256); + session_method(Module['_olm_session_describe'])( + this.ptr, description_buf, 256 + ); + return UTF8ToString(description_buf); + } finally { + if (description_buf !== undefined) free(description_buf); + } }); function Utility() { diff --git a/src/olm.cpp b/src/olm.cpp index 65e506d..0333b10 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -535,10 +535,10 @@ int olm_session_has_received_message( return from_c(session)->received_message; } -const char * olm_session_describe( - OlmSession * session +void olm_session_describe( + OlmSession * session, char *buf, size_t buflen ) { - return from_c(session)->describe(); + from_c(session)->describe(buf, buflen); } size_t olm_matches_inbound_session( diff --git a/src/session.cpp b/src/session.cpp index bad19de..d6761d5 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -398,32 +398,32 @@ std::size_t olm::Session::decrypt( return result; } -const char * olm::Session::describe() { +void olm::Session::describe(char *describe_buffer, size_t buflen) { + if (buflen == 0) return; + describe_buffer[0] = '\0'; char *buf_pos = describe_buffer; buf_pos += snprintf( - buf_pos, DESCRIBE_BUFFER_LEN - (buf_pos - describe_buffer), + buf_pos, buflen - (buf_pos - describe_buffer), "sender chain index: %d ", ratchet.sender_chain[0].chain_key.index ); - buf_pos += snprintf(buf_pos, DESCRIBE_BUFFER_LEN - (buf_pos - describe_buffer), "receiver chain indcies:"); + buf_pos += snprintf(buf_pos, buflen - (buf_pos - describe_buffer), "receiver chain indcies:"); for (size_t i = 0; i < ratchet.receiver_chains.size(); ++i) { buf_pos += snprintf( - buf_pos, DESCRIBE_BUFFER_LEN - (buf_pos - describe_buffer), + buf_pos, buflen - (buf_pos - describe_buffer), " %d", ratchet.receiver_chains[i].chain_key.index ); } - buf_pos += snprintf(buf_pos, DESCRIBE_BUFFER_LEN - (buf_pos - describe_buffer), " skipped message keys:"); + buf_pos += snprintf(buf_pos, buflen - (buf_pos - describe_buffer), " skipped message keys:"); for (size_t i = 0; i < ratchet.skipped_message_keys.size(); ++i) { buf_pos += snprintf( - buf_pos, DESCRIBE_BUFFER_LEN - (buf_pos - describe_buffer), + buf_pos, buflen - (buf_pos - describe_buffer), " %d", ratchet.skipped_message_keys[i].message_key.index ); } - - return describe_buffer; } namespace {