Fix asserts with side-effects (per Keegan McAllister)

This commit is contained in:
Keith Winstein
2012-03-08 10:50:19 -05:00
parent 4a29ab9d70
commit df5d163f9c
15 changed files with 80 additions and 35 deletions
+1 -1
View File
@@ -27,5 +27,5 @@ else
endif
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)
+2 -1
View File
@@ -21,6 +21,7 @@
#include <poll.h>
#include "user.h"
#include "fatal_assert.h"
#include "networktransport.cc"
using namespace Network;
@@ -117,7 +118,7 @@ int main( int argc, char *argv[] )
if ( fds[ 0 ].revents & POLLIN ) {
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 ) );
}
+3 -2
View File
@@ -49,6 +49,7 @@
#include "parser.h"
#include "completeterminal.h"
#include "swrite.h"
#include "fatal_assert.h"
extern "C" {
#include "selfpipe.h"
@@ -202,7 +203,7 @@ void emulate_terminal( int fd )
return;
}
assert( selfpipe_trap(SIGWINCH) == 0 );
fatal_assert( selfpipe_trap(SIGWINCH) == 0 );
/* get current window size */
struct winsize window_size;
@@ -287,7 +288,7 @@ void emulate_terminal( int fd )
}
} else if ( pollfds[ 2 ].revents & POLLIN ) {
/* resize */
assert( selfpipe_read() == SIGWINCH );
fatal_assert( selfpipe_read() == SIGWINCH );
/* get new size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
+10 -9
View File
@@ -46,6 +46,7 @@ extern "C" {
#include "completeterminal.h"
#include "swrite.h"
#include "user.h"
#include "fatal_assert.h"
#if HAVE_PTY_H
#include <pty.h>
@@ -109,10 +110,10 @@ int main( int argc, char *argv[] )
/* don't let signals kill us */
sigset_t signals_to_block;
assert( sigemptyset( &signals_to_block ) == 0 );
assert( sigaddset( &signals_to_block, SIGHUP ) == 0 );
assert( sigaddset( &signals_to_block, SIGPIPE ) == 0 );
assert( sigprocmask( SIG_BLOCK, &signals_to_block, NULL ) == 0 );
fatal_assert( sigemptyset( &signals_to_block ) == 0 );
fatal_assert( sigaddset( &signals_to_block, SIGHUP ) == 0 );
fatal_assert( sigaddset( &signals_to_block, SIGPIPE ) == 0 );
fatal_assert( sigprocmask( SIG_BLOCK, &signals_to_block, NULL ) == 0 );
struct termios child_termios;
@@ -153,8 +154,8 @@ int main( int argc, char *argv[] )
/* unblock signals */
sigset_t signals_to_block;
assert( sigemptyset( &signals_to_block ) == 0 );
assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
fatal_assert( sigemptyset( &signals_to_block ) == 0 );
fatal_assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
/* set TERM */
if ( setenv( "TERM", "xterm", true ) < 0 ) {
@@ -183,7 +184,7 @@ int main( int argc, char *argv[] )
char *my_argv[ 2 ];
my_argv[ 0 ] = strdup( pw->pw_shell );
assert( my_argv[ 0 ] );
fatal_assert( my_argv[ 0 ] );
my_argv[ 1 ] = NULL;
if ( execv( pw->pw_shell, my_argv ) < 0 ) {
@@ -235,8 +236,8 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
return;
}
assert( selfpipe_trap( SIGTERM ) == 0 );
assert( selfpipe_trap( SIGINT ) == 0 );
fatal_assert( selfpipe_trap( SIGTERM ) == 0 );
fatal_assert( selfpipe_trap( SIGINT ) == 0 );
/* prepare to poll for events */
struct pollfd pollfds[ 3 ];
+8 -7
View File
@@ -46,6 +46,7 @@ extern "C" {
#include "swrite.h"
#include "completeterminal.h"
#include "user.h"
#include "fatal_assert.h"
#include "networktransport.cc"
@@ -111,13 +112,13 @@ void STMClient::main_init( void )
return;
}
assert( selfpipe_trap( SIGWINCH ) == 0 );
assert( selfpipe_trap( SIGTERM ) == 0 );
assert( selfpipe_trap( SIGINT ) == 0 );
assert( selfpipe_trap( SIGHUP ) == 0 );
assert( selfpipe_trap( SIGPIPE ) == 0 );
assert( selfpipe_trap( SIGTSTP ) == 0 );
assert( selfpipe_trap( SIGCONT ) == 0 );
fatal_assert( selfpipe_trap( SIGWINCH ) == 0 );
fatal_assert( selfpipe_trap( SIGTERM ) == 0 );
fatal_assert( selfpipe_trap( SIGINT ) == 0 );
fatal_assert( selfpipe_trap( SIGHUP ) == 0 );
fatal_assert( selfpipe_trap( SIGPIPE ) == 0 );
fatal_assert( selfpipe_trap( SIGTSTP ) == 0 );
fatal_assert( selfpipe_trap( SIGCONT ) == 0 );
/* get initial window size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
+3 -3
View File
@@ -1,7 +1,7 @@
#include <zlib.h>
#include <assert.h>
#include "compressor.h"
#include "dos_assert.h"
using namespace Network;
using namespace std;
@@ -9,7 +9,7 @@ using namespace std;
string Compressor::compress_str( const string input )
{
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() ),
input.size() ) );
return string( reinterpret_cast<char *>( buffer ), len );
@@ -18,7 +18,7 @@ string Compressor::compress_str( const string input )
string Compressor::uncompress_str( const string input )
{
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() ),
input.size() ) );
return string( reinterpret_cast<char *>( buffer ), len );
+3 -2
View File
@@ -22,6 +22,7 @@
#include "transportfragment.h"
#include "transportinstruction.pb.h"
#include "compressor.h"
#include "fatal_assert.h"
using namespace Network;
using namespace TransportBuffers;
@@ -46,7 +47,7 @@ string Fragment::tostring( void )
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;
ret += network_order_string( combined_fragment_num );
@@ -122,7 +123,7 @@ Instruction FragmentAssembly::get_assembly( void )
}
Instruction ret;
assert( ret.ParseFromString( get_compressor().uncompress_str( encoded ) ) );
fatal_assert( ret.ParseFromString( get_compressor().uncompress_str( encoded ) ) );
fragments.clear();
fragments_arrived = 0;
+1 -1
View File
@@ -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
noinst_LIBRARIES = libmoshstatesync.a
+2 -1
View File
@@ -20,6 +20,7 @@
#include <boost/lambda/lambda.hpp>
#include "completeterminal.h"
#include "fatal_assert.h"
#include "hostinput.pb.h"
@@ -83,7 +84,7 @@ string Complete::diff_from( const Complete &existing ) const
void Complete::apply_string( string diff )
{
HostBuffers::HostMessage input;
assert( input.ParseFromString( diff ) );
fatal_assert( input.ParseFromString( diff ) );
for ( int i = 0; i < input.instruction_size(); i++ ) {
if ( input.instruction( i ).HasExtension( hostbytes ) ) {
+3 -1
View File
@@ -20,6 +20,7 @@
#include <typeinfo>
#include "user.h"
#include "fatal_assert.h"
#include "userinput.pb.h"
using namespace Parser;
@@ -75,6 +76,7 @@ string UserStream::diff_from( const UserStream &existing ) const
break;
default:
assert( false );
break;
}
my_it++;
@@ -86,7 +88,7 @@ string UserStream::diff_from( const UserStream &existing ) const
void UserStream::apply_string( string diff )
{
ClientBuffers::UserMessage input;
assert( input.ParseFromString( diff ) );
fatal_assert( input.ParseFromString( diff ) );
for ( int i = 0; i < input.instruction_size(); i++ ) {
if ( input.instruction( i ).HasExtension( keystroke ) ) {
-1
View File
@@ -68,7 +68,6 @@ std::list<Parser::Action *> Parser::UTF8Parser::input( char c )
buf[ buf_len++ ] = c;
/* This function will only work in a UTF-8 locale. */
/* This is asserted in the constructor. */
wchar_t pwc;
mbstate_t ps;
+1
View File
@@ -123,6 +123,7 @@ void Emulator::print( const Parser::Print *act )
break;
default:
assert( false );
break;
}
}
-1
View File
@@ -17,7 +17,6 @@
*/
#include <boost/typeof/typeof.hpp>
#include <assert.h>
#include <stdio.h>
#include "terminaldisplay.h"
+1 -1
View File
@@ -2,4 +2,4 @@ AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe
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
+38
View File
@@ -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