From 8f68400c13b44a4e819d703c2be2bf3dd6198fa5 Mon Sep 17 00:00:00 2001 From: John Hood Date: Thu, 11 May 2017 10:07:57 -0400 Subject: [PATCH] Convert new/delete to shared_ptr. --- src/examples/ntester.cc | 15 +++++++-------- src/frontend/mosh-server.cc | 7 +++---- src/frontend/stmclient.cc | 3 +-- src/frontend/stmclient.h | 14 +++++--------- src/tests/ocb-aes.cc | 20 ++++++++++---------- 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/examples/ntester.cc b/src/examples/ntester.cc index 7fec181..5db1d1c 100644 --- a/src/examples/ntester.cc +++ b/src/examples/ntester.cc @@ -38,6 +38,7 @@ #include "pty_compat.h" #include "networktransport-impl.h" #include "select.h" +#include "shared.h" using namespace Network; @@ -49,9 +50,8 @@ int main( int argc, char *argv[] ) char *port; UserStream me, remote; - - Transport *n; - + typedef shared::shared_ptr > NetworkPointer; + Transport *raw_n; try { if ( argc > 1 ) { server = false; @@ -61,15 +61,16 @@ int main( int argc, char *argv[] ) ip = argv[ 2 ]; port = argv[ 3 ]; - n = new Transport( me, remote, key, ip, port ); + raw_n = new Transport( me, remote, key, ip, port ); } else { - n = new Transport( me, remote, NULL, NULL ); + raw_n = new Transport( me, remote, NULL, NULL ); } - fprintf( stderr, "Port bound is %s, key is %s\n", n->port().c_str(), n->get_key().c_str() ); } catch ( const std::exception &e ) { fprintf( stderr, "Fatal startup error: %s\n", e.what() ); exit( 1 ); } + NetworkPointer n( raw_n ); + fprintf( stderr, "Port bound is %s, key is %s\n", n->port().c_str(), n->get_key().c_str() ); if ( server ) { Select &sel = Select::get_instance(); @@ -168,6 +169,4 @@ int main( int argc, char *argv[] ) exit( 1 ); } } - - delete n; } diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index 37a7ba7..89cd647 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -417,7 +417,8 @@ static int run_server( const char *desired_ip, const char *desired_port, /* open network */ Network::UserStream blank; - ServerConnection *network = new ServerConnection( terminal, blank, desired_ip, desired_port ); + typedef shared::shared_ptr NetworkPointer; + NetworkPointer network( new ServerConnection( terminal, blank, desired_ip, desired_port ) ); network->set_verbose( verbose ); Select::set_verbose( verbose ); @@ -521,7 +522,7 @@ static int run_server( const char *desired_ip, const char *desired_port, fatal_assert( 0 == sigaction( SIGPIPE, &sa, NULL ) ); /* close server-related file descriptors */ - delete network; + network.reset(); /* set IUTF8 if available */ #ifdef HAVE_IUTF8 @@ -628,8 +629,6 @@ static int run_server( const char *desired_ip, const char *desired_port, perror( "close" ); exit( 1 ); } - - delete network; } fputs( "\n[mosh-server is exiting.]\n", stdout ); diff --git a/src/frontend/stmclient.cc b/src/frontend/stmclient.cc index 450d068..b7e848e 100644 --- a/src/frontend/stmclient.cc +++ b/src/frontend/stmclient.cc @@ -252,8 +252,7 @@ void STMClient::main_init( void ) /* open network */ Network::UserStream blank; Terminal::Complete local_terminal( window_size.ws_col, window_size.ws_row ); - network = new Network::Transport< Network::UserStream, Terminal::Complete >( blank, local_terminal, - key.c_str(), ip.c_str(), port.c_str() ); + network = NetworkPointer( new NetworkType( blank, local_terminal, key.c_str(), ip.c_str(), port.c_str() ) ); network->set_send_delay( 1 ); /* minimal delay on outgoing keystrokes */ diff --git a/src/frontend/stmclient.h b/src/frontend/stmclient.h index 4d98b53..c1440c3 100644 --- a/src/frontend/stmclient.h +++ b/src/frontend/stmclient.h @@ -40,6 +40,7 @@ #include "completeterminal.h" #include "networktransport.h" #include "user.h" +#include "shared.h" #include "terminaloverlay.h" class STMClient { @@ -60,7 +61,9 @@ private: Terminal::Framebuffer local_framebuffer, new_state; Overlay::OverlayManager overlays; - Network::Transport< Network::UserStream, Terminal::Complete > *network; + typedef Network::Transport< Network::UserStream, Terminal::Complete > NetworkType; + typedef shared::shared_ptr< NetworkType > NetworkPointer; + NetworkPointer network; Terminal::Display display; std::wstring connecting_notification; @@ -94,7 +97,7 @@ public: local_framebuffer( 1, 1 ), new_state( 1, 1 ), overlays(), - network( NULL ), + network(), display( true ), /* use TERM environment var to initialize display */ connecting_notification(), repaint_requested( false ), @@ -126,13 +129,6 @@ public: void shutdown( void ); bool main( void ); - ~STMClient() - { - if ( network != NULL ) { - delete network; - } - } - /* unused */ STMClient( const STMClient & ); STMClient & operator=( const STMClient & ); diff --git a/src/tests/ocb-aes.cc b/src/tests/ocb-aes.cc index 60542c2..f6c817a 100644 --- a/src/tests/ocb-aes.cc +++ b/src/tests/ocb-aes.cc @@ -46,6 +46,7 @@ #include "prng.h" #include "fatal_assert.h" #include "test_utils.h" +#include "shared.h" #define KEY_LEN 16 #define NONCE_LEN 12 @@ -60,6 +61,8 @@ static bool equal( const AlignedBuffer &a, const AlignedBuffer &b ) { && !memcmp( a.data(), b.data(), a.len() ); } +typedef shared::shared_ptr< AlignedBuffer > AlignedPointer; + static AlignedBuffer *get_ctx( const AlignedBuffer &key ) { AlignedBuffer *ctx_buf = new AlignedBuffer( ae_ctx_sizeof() ); fatal_assert( ctx_buf ); @@ -67,15 +70,14 @@ static AlignedBuffer *get_ctx( const AlignedBuffer &key ) { return ctx_buf; } -static void scrap_ctx( AlignedBuffer *ctx_buf ) { - fatal_assert( AE_SUCCESS == ae_clear( (ae_ctx *)ctx_buf->data() ) ); - delete ctx_buf; +static void scrap_ctx( AlignedBuffer &ctx_buf ) { + fatal_assert( AE_SUCCESS == ae_clear( (ae_ctx *)ctx_buf.data() ) ); } static void test_encrypt( const AlignedBuffer &key, const AlignedBuffer &nonce, const AlignedBuffer &plaintext, const AlignedBuffer &assoc, const AlignedBuffer &expected_ciphertext ) { - AlignedBuffer *ctx_buf = get_ctx( key ); + AlignedPointer ctx_buf( get_ctx( key ) ); ae_ctx *ctx = (ae_ctx *)ctx_buf->data(); AlignedBuffer observed_ciphertext( plaintext.len() + TAG_LEN ); @@ -94,14 +96,14 @@ static void test_encrypt( const AlignedBuffer &key, const AlignedBuffer &nonce, fatal_assert( ret == int( expected_ciphertext.len() ) ); fatal_assert( equal( expected_ciphertext, observed_ciphertext ) ); - scrap_ctx( ctx_buf ); + scrap_ctx( *ctx_buf ); } static void test_decrypt( const AlignedBuffer &key, const AlignedBuffer &nonce, const AlignedBuffer &ciphertext, const AlignedBuffer &assoc, const AlignedBuffer &expected_plaintext, bool valid ) { - AlignedBuffer *ctx_buf = get_ctx( key ); + AlignedPointer ctx_buf( get_ctx( key ) ); ae_ctx *ctx = (ae_ctx *)ctx_buf->data(); AlignedBuffer observed_plaintext( ciphertext.len() - TAG_LEN ); @@ -126,7 +128,7 @@ static void test_decrypt( const AlignedBuffer &key, const AlignedBuffer &nonce, fatal_assert( ret == AE_INVALID ); } - scrap_ctx( ctx_buf ); + scrap_ctx( *ctx_buf ); } static void test_vector( const char *key_p, const char *nonce_p, @@ -468,7 +470,7 @@ static void test_iterative( void ) { AlignedBuffer key( KEY_LEN ); memset( key.data(), 0, KEY_LEN ); - AlignedBuffer *ctx_buf = get_ctx( key ); + AlignedPointer ctx_buf( get_ctx( key ) ); ae_ctx *ctx = (ae_ctx *)ctx_buf->data(); AlignedBuffer nonce( NONCE_LEN ); @@ -531,8 +533,6 @@ static void test_iterative( void ) { AlignedBuffer correct( TAG_LEN, "\xB2\xB4\x1C\xBF\x9B\x05\x03\x7D\xA7\xF1\x6C\x24\xA3\x5C\x1C\x94" ); fatal_assert( equal( out, correct ) ); - scrap_ctx( ctx_buf ); - if ( verbose ) { printf( "iterative PASSED\n\n" ); }