Fix asserts with side-effects (per Keegan McAllister)
This commit is contained in:
@@ -27,5 +27,5 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
ntester_SOURCES = ntester.cc
|
ntester_SOURCES = ntester.cc
|
||||||
ntester_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I../protobufs $(BOOST_CPPFLAGS)
|
ntester_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I../protobufs $(BOOST_CPPFLAGS)
|
||||||
ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a -lutil -lm $(BOOST_LDFLAGS) $(protobuf_LIBS)
|
ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a -lutil -lm $(BOOST_LDFLAGS) $(protobuf_LIBS)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
#include "networktransport.cc"
|
#include "networktransport.cc"
|
||||||
|
|
||||||
using namespace Network;
|
using namespace Network;
|
||||||
@@ -117,7 +118,7 @@ int main( int argc, char *argv[] )
|
|||||||
|
|
||||||
if ( fds[ 0 ].revents & POLLIN ) {
|
if ( fds[ 0 ].revents & POLLIN ) {
|
||||||
char x;
|
char x;
|
||||||
assert( read( STDIN_FILENO, &x, 1 ) == 1 );
|
fatal_assert( read( STDIN_FILENO, &x, 1 ) == 1 );
|
||||||
n->get_current_state().push_back( Parser::UserByte( x ) );
|
n->get_current_state().push_back( Parser::UserByte( x ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,7 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "completeterminal.h"
|
#include "completeterminal.h"
|
||||||
#include "swrite.h"
|
#include "swrite.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "selfpipe.h"
|
#include "selfpipe.h"
|
||||||
@@ -202,7 +203,7 @@ void emulate_terminal( int fd )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert( selfpipe_trap(SIGWINCH) == 0 );
|
fatal_assert( selfpipe_trap(SIGWINCH) == 0 );
|
||||||
|
|
||||||
/* get current window size */
|
/* get current window size */
|
||||||
struct winsize window_size;
|
struct winsize window_size;
|
||||||
@@ -287,7 +288,7 @@ void emulate_terminal( int fd )
|
|||||||
}
|
}
|
||||||
} else if ( pollfds[ 2 ].revents & POLLIN ) {
|
} else if ( pollfds[ 2 ].revents & POLLIN ) {
|
||||||
/* resize */
|
/* resize */
|
||||||
assert( selfpipe_read() == SIGWINCH );
|
fatal_assert( selfpipe_read() == SIGWINCH );
|
||||||
|
|
||||||
/* get new size */
|
/* get new size */
|
||||||
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
|
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ extern "C" {
|
|||||||
#include "completeterminal.h"
|
#include "completeterminal.h"
|
||||||
#include "swrite.h"
|
#include "swrite.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
|
|
||||||
#if HAVE_PTY_H
|
#if HAVE_PTY_H
|
||||||
#include <pty.h>
|
#include <pty.h>
|
||||||
@@ -109,10 +110,10 @@ int main( int argc, char *argv[] )
|
|||||||
/* don't let signals kill us */
|
/* don't let signals kill us */
|
||||||
sigset_t signals_to_block;
|
sigset_t signals_to_block;
|
||||||
|
|
||||||
assert( sigemptyset( &signals_to_block ) == 0 );
|
fatal_assert( sigemptyset( &signals_to_block ) == 0 );
|
||||||
assert( sigaddset( &signals_to_block, SIGHUP ) == 0 );
|
fatal_assert( sigaddset( &signals_to_block, SIGHUP ) == 0 );
|
||||||
assert( sigaddset( &signals_to_block, SIGPIPE ) == 0 );
|
fatal_assert( sigaddset( &signals_to_block, SIGPIPE ) == 0 );
|
||||||
assert( sigprocmask( SIG_BLOCK, &signals_to_block, NULL ) == 0 );
|
fatal_assert( sigprocmask( SIG_BLOCK, &signals_to_block, NULL ) == 0 );
|
||||||
|
|
||||||
struct termios child_termios;
|
struct termios child_termios;
|
||||||
|
|
||||||
@@ -153,8 +154,8 @@ int main( int argc, char *argv[] )
|
|||||||
|
|
||||||
/* unblock signals */
|
/* unblock signals */
|
||||||
sigset_t signals_to_block;
|
sigset_t signals_to_block;
|
||||||
assert( sigemptyset( &signals_to_block ) == 0 );
|
fatal_assert( sigemptyset( &signals_to_block ) == 0 );
|
||||||
assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
|
fatal_assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
|
||||||
|
|
||||||
/* set TERM */
|
/* set TERM */
|
||||||
if ( setenv( "TERM", "xterm", true ) < 0 ) {
|
if ( setenv( "TERM", "xterm", true ) < 0 ) {
|
||||||
@@ -183,7 +184,7 @@ int main( int argc, char *argv[] )
|
|||||||
|
|
||||||
char *my_argv[ 2 ];
|
char *my_argv[ 2 ];
|
||||||
my_argv[ 0 ] = strdup( pw->pw_shell );
|
my_argv[ 0 ] = strdup( pw->pw_shell );
|
||||||
assert( my_argv[ 0 ] );
|
fatal_assert( my_argv[ 0 ] );
|
||||||
my_argv[ 1 ] = NULL;
|
my_argv[ 1 ] = NULL;
|
||||||
|
|
||||||
if ( execv( pw->pw_shell, my_argv ) < 0 ) {
|
if ( execv( pw->pw_shell, my_argv ) < 0 ) {
|
||||||
@@ -235,8 +236,8 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert( selfpipe_trap( SIGTERM ) == 0 );
|
fatal_assert( selfpipe_trap( SIGTERM ) == 0 );
|
||||||
assert( selfpipe_trap( SIGINT ) == 0 );
|
fatal_assert( selfpipe_trap( SIGINT ) == 0 );
|
||||||
|
|
||||||
/* prepare to poll for events */
|
/* prepare to poll for events */
|
||||||
struct pollfd pollfds[ 3 ];
|
struct pollfd pollfds[ 3 ];
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ extern "C" {
|
|||||||
#include "swrite.h"
|
#include "swrite.h"
|
||||||
#include "completeterminal.h"
|
#include "completeterminal.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
|
|
||||||
#include "networktransport.cc"
|
#include "networktransport.cc"
|
||||||
|
|
||||||
@@ -111,13 +112,13 @@ void STMClient::main_init( void )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert( selfpipe_trap( SIGWINCH ) == 0 );
|
fatal_assert( selfpipe_trap( SIGWINCH ) == 0 );
|
||||||
assert( selfpipe_trap( SIGTERM ) == 0 );
|
fatal_assert( selfpipe_trap( SIGTERM ) == 0 );
|
||||||
assert( selfpipe_trap( SIGINT ) == 0 );
|
fatal_assert( selfpipe_trap( SIGINT ) == 0 );
|
||||||
assert( selfpipe_trap( SIGHUP ) == 0 );
|
fatal_assert( selfpipe_trap( SIGHUP ) == 0 );
|
||||||
assert( selfpipe_trap( SIGPIPE ) == 0 );
|
fatal_assert( selfpipe_trap( SIGPIPE ) == 0 );
|
||||||
assert( selfpipe_trap( SIGTSTP ) == 0 );
|
fatal_assert( selfpipe_trap( SIGTSTP ) == 0 );
|
||||||
assert( selfpipe_trap( SIGCONT ) == 0 );
|
fatal_assert( selfpipe_trap( SIGCONT ) == 0 );
|
||||||
|
|
||||||
/* get initial window size */
|
/* get initial window size */
|
||||||
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
|
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "compressor.h"
|
#include "compressor.h"
|
||||||
|
#include "dos_assert.h"
|
||||||
|
|
||||||
using namespace Network;
|
using namespace Network;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -9,18 +9,18 @@ using namespace std;
|
|||||||
string Compressor::compress_str( const string input )
|
string Compressor::compress_str( const string input )
|
||||||
{
|
{
|
||||||
long unsigned int len = BUFFER_SIZE;
|
long unsigned int len = BUFFER_SIZE;
|
||||||
assert( Z_OK == compress( buffer, &len,
|
dos_assert( Z_OK == compress( buffer, &len,
|
||||||
reinterpret_cast<const unsigned char *>( input.data() ),
|
reinterpret_cast<const unsigned char *>( input.data() ),
|
||||||
input.size() ) );
|
input.size() ) );
|
||||||
return string( reinterpret_cast<char *>( buffer ), len );
|
return string( reinterpret_cast<char *>( buffer ), len );
|
||||||
}
|
}
|
||||||
|
|
||||||
string Compressor::uncompress_str( const string input )
|
string Compressor::uncompress_str( const string input )
|
||||||
{
|
{
|
||||||
long unsigned int len = BUFFER_SIZE;
|
long unsigned int len = BUFFER_SIZE;
|
||||||
assert( Z_OK == uncompress( buffer, &len,
|
dos_assert( Z_OK == uncompress( buffer, &len,
|
||||||
reinterpret_cast<const unsigned char *>( input.data() ),
|
reinterpret_cast<const unsigned char *>( input.data() ),
|
||||||
input.size() ) );
|
input.size() ) );
|
||||||
return string( reinterpret_cast<char *>( buffer ), len );
|
return string( reinterpret_cast<char *>( buffer ), len );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include "transportfragment.h"
|
#include "transportfragment.h"
|
||||||
#include "transportinstruction.pb.h"
|
#include "transportinstruction.pb.h"
|
||||||
#include "compressor.h"
|
#include "compressor.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
|
|
||||||
using namespace Network;
|
using namespace Network;
|
||||||
using namespace TransportBuffers;
|
using namespace TransportBuffers;
|
||||||
@@ -46,7 +47,7 @@ string Fragment::tostring( void )
|
|||||||
|
|
||||||
ret += network_order_string( id );
|
ret += network_order_string( id );
|
||||||
|
|
||||||
assert( !( fragment_num & 0x8000 ) ); /* effective limit on size of a terminal screen change or buffered user input */
|
fatal_assert( !( fragment_num & 0x8000 ) ); /* effective limit on size of a terminal screen change or buffered user input */
|
||||||
uint16_t combined_fragment_num = ( final << 15 ) | fragment_num;
|
uint16_t combined_fragment_num = ( final << 15 ) | fragment_num;
|
||||||
ret += network_order_string( combined_fragment_num );
|
ret += network_order_string( combined_fragment_num );
|
||||||
|
|
||||||
@@ -122,7 +123,7 @@ Instruction FragmentAssembly::get_assembly( void )
|
|||||||
}
|
}
|
||||||
|
|
||||||
Instruction ret;
|
Instruction ret;
|
||||||
assert( ret.ParseFromString( get_compressor().uncompress_str( encoded ) ) );
|
fatal_assert( ret.ParseFromString( get_compressor().uncompress_str( encoded ) ) );
|
||||||
|
|
||||||
fragments.clear();
|
fragments.clear();
|
||||||
fragments_arrived = 0;
|
fragments_arrived = 0;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
AM_CPPFLAGS = -I$(srcdir)/../terminal -I../protobufs $(BOOST_CPPFLAGS)
|
AM_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../terminal -I../protobufs $(BOOST_CPPFLAGS)
|
||||||
AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe
|
AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe
|
||||||
|
|
||||||
noinst_LIBRARIES = libmoshstatesync.a
|
noinst_LIBRARIES = libmoshstatesync.a
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <boost/lambda/lambda.hpp>
|
#include <boost/lambda/lambda.hpp>
|
||||||
|
|
||||||
#include "completeterminal.h"
|
#include "completeterminal.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
|
|
||||||
#include "hostinput.pb.h"
|
#include "hostinput.pb.h"
|
||||||
|
|
||||||
@@ -83,7 +84,7 @@ string Complete::diff_from( const Complete &existing ) const
|
|||||||
void Complete::apply_string( string diff )
|
void Complete::apply_string( string diff )
|
||||||
{
|
{
|
||||||
HostBuffers::HostMessage input;
|
HostBuffers::HostMessage input;
|
||||||
assert( input.ParseFromString( diff ) );
|
fatal_assert( input.ParseFromString( diff ) );
|
||||||
|
|
||||||
for ( int i = 0; i < input.instruction_size(); i++ ) {
|
for ( int i = 0; i < input.instruction_size(); i++ ) {
|
||||||
if ( input.instruction( i ).HasExtension( hostbytes ) ) {
|
if ( input.instruction( i ).HasExtension( hostbytes ) ) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
|
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
#include "fatal_assert.h"
|
||||||
#include "userinput.pb.h"
|
#include "userinput.pb.h"
|
||||||
|
|
||||||
using namespace Parser;
|
using namespace Parser;
|
||||||
@@ -75,6 +76,7 @@ string UserStream::diff_from( const UserStream &existing ) const
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert( false );
|
assert( false );
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
my_it++;
|
my_it++;
|
||||||
@@ -86,7 +88,7 @@ string UserStream::diff_from( const UserStream &existing ) const
|
|||||||
void UserStream::apply_string( string diff )
|
void UserStream::apply_string( string diff )
|
||||||
{
|
{
|
||||||
ClientBuffers::UserMessage input;
|
ClientBuffers::UserMessage input;
|
||||||
assert( input.ParseFromString( diff ) );
|
fatal_assert( input.ParseFromString( diff ) );
|
||||||
|
|
||||||
for ( int i = 0; i < input.instruction_size(); i++ ) {
|
for ( int i = 0; i < input.instruction_size(); i++ ) {
|
||||||
if ( input.instruction( i ).HasExtension( keystroke ) ) {
|
if ( input.instruction( i ).HasExtension( keystroke ) ) {
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ std::list<Parser::Action *> Parser::UTF8Parser::input( char c )
|
|||||||
buf[ buf_len++ ] = c;
|
buf[ buf_len++ ] = c;
|
||||||
|
|
||||||
/* This function will only work in a UTF-8 locale. */
|
/* This function will only work in a UTF-8 locale. */
|
||||||
/* This is asserted in the constructor. */
|
|
||||||
|
|
||||||
wchar_t pwc;
|
wchar_t pwc;
|
||||||
mbstate_t ps;
|
mbstate_t ps;
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ void Emulator::print( const Parser::Print *act )
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert( false );
|
assert( false );
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <boost/typeof/typeof.hpp>
|
#include <boost/typeof/typeof.hpp>
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "terminaldisplay.h"
|
#include "terminaldisplay.h"
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe
|
|||||||
|
|
||||||
noinst_LIBRARIES = libmoshutil.a
|
noinst_LIBRARIES = libmoshutil.a
|
||||||
|
|
||||||
libmoshutil_a_SOURCES = swrite.cc swrite.h dos_assert.h
|
libmoshutil_a_SOURCES = swrite.cc swrite.h dos_assert.h fatal_assert.h
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
Mosh: the mobile shell
|
||||||
|
Copyright 2012 Keith Winstein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FATAL_ASSERT_HPP
|
||||||
|
#define FATAL_ASSERT_HPP
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void fatal_error( const char *expression, const char *file, int line, const char *function )
|
||||||
|
{
|
||||||
|
char buffer[ 2048 ];
|
||||||
|
snprintf( buffer, 2048, "Fatal assertion failure in function %s at %s:%d, failed test: %s\n",
|
||||||
|
function, file, line, expression );
|
||||||
|
exit( 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
#define fatal_assert(expr) \
|
||||||
|
((expr) \
|
||||||
|
? (void)0 \
|
||||||
|
: fatal_error (__STRING(expr), __FILE__, __LINE__, __PRETTY_FUNCTION__ ))
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user