From: ThorstenB Date: Fri, 13 Jul 2012 18:33:36 +0000 (+0200) Subject: Proper error checks for generic protocol. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=39a7caae15c01007b6dc636f21336e823c621f73;p=flightgear.git Proper error checks for generic protocol. Drop FGGeneric instances which failed to initialize to avoid run-time issues. --- diff --git a/src/Main/fg_io.cxx b/src/Main/fg_io.cxx index 2014212b5..96b2a4d6b 100644 --- a/src/Main/fg_io.cxx +++ b/src/Main/fg_io.cxx @@ -196,24 +196,16 @@ FGIO::parse_port_config( const string& config ) } else if ( protocol == "rul" ) { FGRUL *rul = new FGRUL; io = rul; - } else if ( protocol == "generic" ) { - size_t configToken; - if (tokens[1] == "socket") { - configToken = 7; - } else if (tokens[1] == "file") { - configToken = 5; - } else { - configToken = 6; - } - - if (configToken >= tokens.size()) { - SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol."); - return NULL; - } - - FGGeneric *generic = new FGGeneric( tokens ); - io = generic; - } else if ( protocol == "multiplay" ) { + } else if ( protocol == "generic" ) { + FGGeneric *generic = new FGGeneric( tokens ); + if (!generic->getInitOk()) + { + // failed to initialize (i.e. invalid configuration) + delete generic; + return NULL; + } + io = generic; + } else if ( protocol == "multiplay" ) { if ( tokens.size() != 5 ) { SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option " "(4 arguments expected: --multiplay=dir,hz,hostname,port)" ); diff --git a/src/Network/generic.cxx b/src/Network/generic.cxx index 242bd881c..21f048c65 100644 --- a/src/Network/generic.cxx +++ b/src/Network/generic.cxx @@ -42,7 +42,7 @@ #include
#include "generic.hxx" -FGGeneric::FGGeneric(vector tokens) : exitOnError(false) +FGGeneric::FGGeneric(vector tokens) : exitOnError(false), initOk(false) { size_t configToken; if (tokens[1] == "socket") { @@ -53,9 +53,9 @@ FGGeneric::FGGeneric(vector tokens) : exitOnError(false) configToken = 6; } - if (configToken >= tokens.size()) { + if ((configToken >= tokens.size())||(tokens[ configToken ] == "")) { SG_LOG(SG_NETWORK, SG_ALERT, - "Not enough tokens passed for generic protocol"); + "Not enough tokens passed for generic '" << tokens[1] << "' protocol. "); return; } @@ -66,6 +66,7 @@ FGGeneric::FGGeneric(vector tokens) : exitOnError(false) if (direction != "in" && direction != "out" && direction != "bi") { SG_LOG(SG_NETWORK, SG_ALERT, "Unsuported protocol direction: " << direction); + return; } reinit(); @@ -557,6 +558,8 @@ FGGeneric::reinit() } } } + + initOk = true; } diff --git a/src/Network/generic.hxx b/src/Network/generic.hxx index 55f0ca8a5..d66b84e38 100644 --- a/src/Network/generic.hxx +++ b/src/Network/generic.hxx @@ -57,6 +57,7 @@ public: void setExitOnError(bool val) { exitOnError = val; } bool getExitOnError() { return exitOnError; } + bool getInitOk(void) { return initOk; } protected: enum e_type { FG_BOOL=0, FG_INT, FG_FLOAT, FG_DOUBLE, FG_STRING, FG_FIXED }; @@ -102,6 +103,7 @@ private: bool parse_message_binary(int length); void read_config(SGPropertyNode *root, vector<_serial_prot> &msg); bool exitOnError; + bool initOk; template static void updateValue(_serial_prot& prot, const T& val)