From 7800719551000b340924a25ccb7cfc7a6573201a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teppo=20J=C3=A4rvelin?= Date: Fri, 25 Oct 2019 14:30:33 +0300 Subject: [PATCH] Adjusted buf allocated from heap in ssl_client2 to be as small as possible --- programs/ssl/ssl_client2.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index be64f6ab5c33..a9c06f40481f 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1522,9 +1522,23 @@ int main( int argc, char *argv[] ) goto usage; } - main_buf_len = MAX_REQUEST_SIZE + 1; - buf = mbedtls_calloc( 1, MAX_REQUEST_SIZE + 1 ); - if( buf == NULL ) { + /* try to use as small buf from the heap as possible */ + if( opt.request_size <= 0 ) + { + main_buf_len = MBEDTLS_SSL_MAX_CONTENT_LEN + 1; + } + else if( opt.request_size < (int)sizeof(GET_REQUEST) ) + { + main_buf_len = sizeof(GET_REQUEST) + 1; + } + else + { + main_buf_len = opt.request_size + 1; + } + + buf = mbedtls_calloc( 1, main_buf_len ); + if( buf == NULL ) + { mbedtls_printf( "buf allocation failed!\n" ); goto exit; }