]> git.mxchange.org Git - flightgear.git/commitdiff
Proper error checks for generic protocol.
authorThorstenB <brehmt@gmail.com>
Fri, 13 Jul 2012 18:33:36 +0000 (20:33 +0200)
committerThorstenB <brehmt@gmail.com>
Fri, 13 Jul 2012 18:33:36 +0000 (20:33 +0200)
Drop FGGeneric instances which failed to initialize to avoid run-time
issues.

src/Main/fg_io.cxx
src/Network/generic.cxx
src/Network/generic.hxx

index 2014212b5faeea942061604d8a30bafc5987e263..96b2a4d6b27ad2477a5e5cccf5d6d34e3febc9a5 100644 (file)
@@ -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)" );
index 242bd881c58765cc73e88e0afcb3b5f6faad2b15..21f048c65ff20dd0088e04f8a912e51a983b932f 100644 (file)
@@ -42,7 +42,7 @@
 #include <Main/util.hxx>
 #include "generic.hxx"
 
-FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false)
+FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false), initOk(false)
 {
     size_t configToken;
     if (tokens[1] == "socket") {
@@ -53,9 +53,9 @@ FGGeneric::FGGeneric(vector<string> 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<string> 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;
 }
 
 
index 55f0ca8a5675df995d5c57cab9b67297bab167c8..d66b84e3879aa783ed1d82144543e7b0c31b5b19 100644 (file)
@@ -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<class T>
     static void updateValue(_serial_prot& prot, const T& val)