Error in case of vlan out of range in config file
authorIdo Barnea <[email protected]>
Wed, 22 Mar 2017 09:33:38 +0000 (11:33 +0200)
committerIdo Barnea <[email protected]>
Wed, 22 Mar 2017 09:33:38 +0000 (11:33 +0200)
Signed-off-by: Ido Barnea <[email protected]>
src/platform_cfg.cpp
src/utl_yaml.cpp
src/utl_yaml.h

index 8a2a948..3acc993 100755 (executable)
@@ -255,10 +255,8 @@ void operator >> (const YAML::Node& node, CMacYamlInfo & mac_info) {
     if (! utl_yaml_read_ip_addr(node, "mask", mac_info.m_mask)) {
         mac_info.m_mask = 0;
     }
-    if (! utl_yaml_read_uint16(node, "vlan", mac_info.m_vlan)) {
+    if (! utl_yaml_read_uint16(node, "vlan", mac_info.m_vlan, 0, 0xfff)) {
         mac_info.m_vlan = 0;
-    } else {
-        mac_info.m_vlan &= 0x0fff; // we only care about the vlan ID
     }
 }
 
index df60596..0073e6f 100755 (executable)
@@ -43,7 +43,7 @@ bool utl_yaml_read_ip_addr(const YAML::Node& node,
             res=true;
         }else{
             printf(" Error: non valid ip %s \n",(char *)tmp.c_str());
-            exit(-1);
+            exit(1);
         }
     }
     return (res);
@@ -60,6 +60,21 @@ bool utl_yaml_read_uint32(const YAML::Node& node,
     return (res);
 }
 
+bool utl_yaml_read_uint16(const YAML::Node& node,
+                          const std::string &name,
+                          uint16_t & val, uint16_t min, uint16_t max) {
+    bool res = utl_yaml_read_uint16(node, name, val);
+
+    if ((val < min) || (val > max)) {
+        fprintf(stderr
+                , "Parsing error: value of field '%s' must be between %d and %d\n"
+                , name.c_str(), min, max);
+        exit(1);
+    }
+
+    return res;
+}
+
 bool utl_yaml_read_uint16(const YAML::Node& node,
                           const std::string &name,
                           uint16_t & val){
index 004e82d..88fc847 100755 (executable)
@@ -6,7 +6,7 @@
 */
 
 /*
-Copyright (c) 2015-2015 Cisco Systems, Inc.
+Copyright (c) 2015-2017 Cisco Systems, Inc.
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -28,18 +28,22 @@ limitations under the License.
 
 
 /* static methods - please prefer the wrapper over those */
-bool utl_yaml_read_ip_addr(const YAML::Node& node, 
+bool utl_yaml_read_ip_addr(const YAML::Node& node,
                            const std::string &name,
                            uint32_t & val);
 
-bool utl_yaml_read_uint32(const YAML::Node& node, 
+bool utl_yaml_read_uint32(const YAML::Node& node,
                           const std::string &name,
                           uint32_t & val);
 
-bool utl_yaml_read_uint16(const YAML::Node& node, 
+bool utl_yaml_read_uint16(const YAML::Node& node,
                           const std::string &name,
                           uint16_t & val);
 
+bool utl_yaml_read_uint16(const YAML::Node& node,
+                          const std::string &name,
+                          uint16_t & val, uint16_t min, uint16_t max);
+
 bool mac2vect(const std::string &mac_str, std::vector<uint8_t> &mac);
 
 /* a thin wrapper to customize errors */
@@ -51,7 +55,7 @@ public:
 
     /**
      * loads the file (while parsing it)
-     * 
+     *
      */
     void load(YAML::Node &root);
 
@@ -76,7 +80,7 @@ public:
     void parse_err(const std::string &err, const YAML::Node &node) const;
     void parse_err(const std::string &err) const;
 
-    
+
 private:
     std::string m_filename;
 };