Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit a3a9f0f

Browse files
committed
Save heap by avoiding a temporary copy of the pub./priv. keys.
The public and private keys are in order of ~0,7KB in size, so we better try to avoid copying them unnecessarily. Use the new overloaded version of resource_value_buffer() and pass just the reference of these buffers to mbedtls, as it will copy them.
1 parent 8c436e5 commit a3a9f0f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

source/m2mconnectionsecuritypimpl.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ int M2MConnectionSecurityPimpl::init(const M2MSecurity *security)
147147
M2MSecurity::SecurityModeType cert_mode =
148148
(M2MSecurity::SecurityModeType)security->resource_value_int(M2MSecurity::SecurityMode);
149149

150-
uint8_t *srv_public_key = 0;
151-
uint8_t *public_key = 0;
152-
uint8_t *sec_key = 0;
150+
// Note: these are relatively large buffers, no point to make copy of them here as mbedtls will make a copy of them.
151+
const uint8_t *srv_public_key = NULL;
152+
const uint8_t *public_key = NULL;
153+
const uint8_t *sec_key = NULL;
153154

154155
uint32_t srv_public_key_size = security->resource_value_buffer(M2MSecurity::ServerPublicKey, srv_public_key);
155156
uint32_t public_key_size = security->resource_value_buffer(M2MSecurity::PublicKey, public_key);
@@ -189,10 +190,6 @@ int M2MConnectionSecurityPimpl::init(const M2MSecurity *security)
189190
/* Enable following two lines to get traces from mbedtls */
190191
/*mbedtls_ssl_conf_dbg( &_conf, mbedtls_debug, stdout );
191192
mbedtls_debug_set_threshold(3);*/
192-
193-
free(srv_public_key);
194-
free(public_key);
195-
free(sec_key);
196193
}
197194

198195
if( ret == 0 ){

test/mbed-client-mbed-tls/unittest/stub/m2msecurity_stub.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ uint32_t M2MSecurity::resource_value_buffer(SecurityResource,
8585
return m2msecurity_stub::int_value;
8686
}
8787

88+
uint32_t M2MSecurity::resource_value_buffer(SecurityResource,
89+
const uint8_t *&value) const
90+
{
91+
if( m2msecurity_stub::has_value ){
92+
value = (const uint8_t*)"dummy";
93+
return 6;
94+
}
95+
return m2msecurity_stub::int_value;
96+
}
8897

8998
uint32_t M2MSecurity::resource_value_int(SecurityResource) const
9099
{

0 commit comments

Comments
 (0)