Convert new/delete to shared_ptr.

This commit is contained in:
John Hood
2017-05-11 10:07:57 -04:00
parent eef77fd5a5
commit 8f68400c13
5 changed files with 26 additions and 33 deletions
+7 -8
View File
@@ -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<UserStream, UserStream> *n;
typedef shared::shared_ptr<Transport<UserStream, UserStream> > NetworkPointer;
Transport<UserStream, UserStream> *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<UserStream, UserStream>( me, remote, key, ip, port );
raw_n = new Transport<UserStream, UserStream>( me, remote, key, ip, port );
} else {
n = new Transport<UserStream, UserStream>( me, remote, NULL, NULL );
raw_n = new Transport<UserStream, UserStream>( 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;
}
+3 -4
View File
@@ -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<ServerConnection> 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 );
+1 -2
View File
@@ -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 */
+5 -9
View File
@@ -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 & );
+10 -10
View File
@@ -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" );
}