handle json exception 46/255046/1 accepted/tizen/unified/20210312.142430 submit/tizen/20210311.111505
authorYoungjae Shin <yj99.shin@samsung.com>
Thu, 11 Mar 2021 10:12:54 +0000 (19:12 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 11 Mar 2021 10:50:09 +0000 (19:50 +0900)
Change-Id: I75d3d869601e60f7d3e7edc1b73d08f3ddffa49b

src/trigger/CustomTemplate.cpp
src/trigger/context_trigger.cpp

index 72ab4f6..c09459a 100644 (file)
@@ -47,10 +47,19 @@ int CustomTemplate::match(const std::string& fact)
 
        std::string error;
        Json::CharReaderBuilder builder;
+       builder["collectComments"] = false;
        std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
 
+       bool result;
+       try {
+               result = reader->parse(fact.c_str(), (fact.c_str() + fact.size()), &factJson, &error);
+       } catch (Json::Exception const&) {
+               _E("Template: invalid json(Json::Exception)");
+               return CONTEXT_TRIGGER_ERROR_INVALID_PARAMETER;
+       }
+
        // Error: Invalid Json
-       if (!reader->parse(fact.c_str(), (fact.c_str()+fact.size()), &factJson, &error)) {
+       if (!result) {
                _E("Fact: invalid json, error [%s]", error.c_str());
                return CONTEXT_TRIGGER_ERROR_INVALID_PARAMETER;
        }
@@ -63,7 +72,14 @@ int CustomTemplate::match(const std::string& fact)
 
 bool CustomTemplate::isValidFact(const Json::Value& tmplJson, const Json::Value& factJson)
 {
-       Json::Value::Members keys = factJson.getMemberNames();
+       Json::Value::Members keys;
+
+       try {
+               keys = factJson.getMemberNames();
+       } catch (Json::Exception const&) {
+               _E("Custom fact: Invalid value(Json::Exception)");
+               return false;
+       }
 
        std::string dataType;
        for (auto& key : keys) {
@@ -109,7 +125,14 @@ bool CustomTemplate::isValidFact(const Json::Value& tmplJson, const Json::Value&
 bool CustomTemplate::isValidTemplate(const Json::Value& tmplJson)
 {
        bool success = false;
-       Json::Value::Members keys = tmplJson.getMemberNames();
+       Json::Value::Members keys;
+
+       try {
+               keys = tmplJson.getMemberNames();
+       } catch (Json::Exception const&) {
+               _E("Invalid template(Json::Exception)");
+               return false;
+       }
 
        std::string dataType;
        for (auto& key : keys) {
@@ -146,7 +169,14 @@ bool CustomTemplate::checkTemplateInt(const Json::Value& elem)
        int minVal = 0;
        int maxVal = 0;
 
-       Json::Value::Members elemKeys = elem.getMemberNames();
+       Json::Value::Members elemKeys;
+
+       try {
+               elemKeys = elem.getMemberNames();
+       } catch (Json::Exception const&) {
+               _E("invalid int(Json::Exception)");
+               return false;
+       }
 
        for (auto& elemKey : elemKeys) {
                if (elemKey == CT_KEY_MIN) {
@@ -169,7 +199,13 @@ bool CustomTemplate::checkTemplateInt(const Json::Value& elem)
 
 bool CustomTemplate::checkTemplateString(const Json::Value& elem)
 {
-       Json::Value::Members elemKeys = elem.getMemberNames();
+       Json::Value::Members elemKeys;
+       try {
+               elemKeys = elem.getMemberNames();
+       } catch (Json::Exception const&) {
+               _E("invalid string(Json::Exception)");
+               return false;
+       }
 
        for (auto& elemKey : elemKeys) {
                IF_FAIL_RETURN_TAG(elemKey == CT_KEY_TYPE, false, _E, "invalid key");
@@ -180,7 +216,13 @@ bool CustomTemplate::checkTemplateString(const Json::Value& elem)
 
 bool CustomTemplate::checkTemplateEnum(const Json::Value& elem)
 {
-       Json::Value::Members elemKeys = elem.getMemberNames();
+       Json::Value::Members elemKeys;
+       try {
+               elemKeys = elem.getMemberNames();
+       } catch (Json::Exception const&) {
+               _E("invalid enum(Json::Exception)");
+               return false;
+       }
 
        for (auto& elemKey : elemKeys) {
                if (elemKey == CT_TYPE_ENUM) {
@@ -206,11 +248,19 @@ int CustomTemplate::add(const std::string& name, const std::string& attrTmpl)
 
        std::string error;
        Json::CharReaderBuilder builder;
+       builder["collectComments"] = false;
        std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
 
+       bool result;
+       try {
+               result = reader->parse(attrTmpl.c_str(), attrTmpl.c_str() + attrTmpl.size(), &tmplJson, &error);
+       } catch (Json::Exception const&) {
+               _E("Template: invalid json(Json::Exception)");
+               return CONTEXT_TRIGGER_ERROR_INVALID_PARAMETER;
+       }
+
        // Error: Invalid Json
-       if (!reader->parse(attrTmpl.c_str(), \
-                       (attrTmpl.c_str()+attrTmpl.size()), &tmplJson, &error)) {
+       if (!result) {
                _E("Template: invalid json, error [%s]", error.c_str());
                return CONTEXT_TRIGGER_ERROR_INVALID_PARAMETER;
        }
@@ -237,9 +287,9 @@ int CustomTemplate::add(const std::string& name, const std::string& attrTmpl)
 void CustomTemplate::remove(const std::string& name)
 {
        __instances.remove_if(
-                       [&name](const CustomTemplate& tmpl)->bool {
-                               return tmpl.getName() == name;
-                       });
+               [&name](const CustomTemplate& tmpl)->bool {
+               return tmpl.getName() == name;
+       });
 }
 
 CustomTemplate* CustomTemplate::get(const std::string& name)
index 001581c..928246f 100644 (file)
@@ -59,8 +59,8 @@
        } while (0)
 
 enum EntryCategory {
-       CATEGORY_EVENT          = 1,
-       CATEGORY_CONDITION      = 2
+       CATEGORY_EVENT = 1,
+       CATEGORY_CONDITION = 2
 };
 
 enum class OpType {
@@ -97,7 +97,6 @@ typedef struct _context_trigger_rule_s {
        }
 } _context_trigger_rule_h;
 
-
 typedef struct _context_trigger_rule_entry_s {
        int category;
        int type;
@@ -145,28 +144,29 @@ typedef struct _context_trigger_rule_entry_s {
        }
 } _context_trigger_rule_entry_h;
 
-
 namespace {
-       class Scheduler {
-       private:
-               ctx_sched_h __scheduler;
+class Scheduler {
+private:
+       ctx_sched_h __scheduler;
 
-       public:
-               Scheduler() : __scheduler(NULL) {}
+public:
+       Scheduler() : __scheduler(NULL)
+       {
+       }
 
-               ~Scheduler()
-               {
-                       ctx_sched_destroy(__scheduler);
-               }
+       ~Scheduler()
+       {
+               ctx_sched_destroy(__scheduler);
+       }
 
-               ctx_sched_h get()
-               {
-                       if (__scheduler == NULL)
-                               ctx_sched_create(&__scheduler);
+       ctx_sched_h get()
+       {
+               if (__scheduler == NULL)
+                       ctx_sched_create(&__scheduler);
 
-                       return __scheduler;
-               }
-       };
+               return __scheduler;
+       }
+};
 }
 
 static OpType __getOpType(const char* opStr)
@@ -231,7 +231,6 @@ static ContextItem* __get_context_item(context_trigger_rule_entry_h entry)
 
 static Scheduler __scheduler;
 
-
 EXPORT_API int context_trigger_add_rule(context_trigger_rule_h rule, int* rule_id)
 {
        INIT_SCHED;
@@ -463,7 +462,7 @@ EXPORT_API int context_trigger_rule_add_entry(context_trigger_rule_h rule, conte
        CHECK_CONTEXT_TRIGGER_SUPPORTED(CONTEXT_TRIGGER_FEATURE);
        ASSERT_NOT_NULL(rule && entry);
        IF_FAIL_RETURN_TAG(!rule->readOnly, E_PARAM,
-                       _E, "A rule acquired by context_trigger_get_rule_by_id() is not allowed to be modified.");
+               _E, "A rule acquired by context_trigger_get_rule_by_id() is not allowed to be modified.");
 
        if (entry->category == CATEGORY_EVENT)
                return __set_event(rule, entry);
@@ -518,7 +517,7 @@ EXPORT_API int context_trigger_rule_set_action_app_control(context_trigger_rule_
        }
 
        if (__is_call_operation(app_control) &&
-                       !PrivilegeChecker::hasPrivilege("http://tizen.org/privilege/call")) {
+               !PrivilegeChecker::hasPrivilege("http://tizen.org/privilege/call")) {
                _E("Privilege denied");
                return E_ACCESS;
        }
@@ -559,7 +558,7 @@ static bool __is_valid_app_control(app_control_h ctrl)
 }
 
 EXPORT_API int context_trigger_rule_set_action_notification(context_trigger_rule_h rule,
-               const char* title, const char* content, const char* icon_path, app_control_h app_control)
+       const char* title, const char* content, const char* icon_path, app_control_h app_control)
 {
        CHECK_CONTEXT_TRIGGER_SUPPORTED(CONTEXT_TRIGGER_FEATURE);
        INIT_SCHED;
@@ -643,7 +642,6 @@ EXPORT_API int context_trigger_rule_event_create(context_trigger_event_e event_i
 
 static bool __is_valid_pkg_id(const char* pkgId)
 {
-       CHECK_CONTEXT_TRIGGER_SUPPORTED(CONTEXT_TRIGGER_FEATURE);
        IF_FAIL_RETURN(pkgId, false);
 
        pkgmgrinfo_pkginfo_h pkgInfo;