String length normalization in manifest file 62/83862/5
authorPiotr Dabrowski <p.dabrowski2@samsung.com>
Fri, 2 Sep 2016 09:26:30 +0000 (11:26 +0200)
committerPiotr Dabrowski <p.dabrowski2@samsung.com>
Fri, 2 Sep 2016 09:26:30 +0000 (11:26 +0200)
Change-Id: Ib03d1681dedec552d7129d0506a50a6f62d4523d

src/manifest_parser/manifest.cc
src/manifest_parser/manifest.h
src/manifest_parser/manifest_parser.cc
src/manifest_parser/manifest_parser.h
src/manifest_parser/manifest_parser_impl.cc
src/manifest_parser/manifest_parser_impl.h
src/manifest_parser/manifest_util.cc
src/manifest_parser/manifest_util.h

index 3f71e2ff7315fb159cc9e23ac3f5d5845c8af111..eb23a65d63d8574cdfd2e9d066c40c8e3a3e1952 100644 (file)
@@ -89,4 +89,16 @@ bool Manifest::CanAccessKey(const std::string& /*key*/) const {
   return true;
 }
 
+ManifestConstraints::ManifestConstraints() :
+    max_attr_element_length_(0) {
+}
+
+void ManifestConstraints::set_max_attr_element_length(unsigned int value) {
+  max_attr_element_length_ = value;
+}
+
+unsigned int ManifestConstraints::max_attr_element_length() const {
+  return max_attr_element_length_;
+}
+
 }  // namespace parser
index d6c3cdcf8b58665d568f18fbaf45dbf193f708d1..5fd123b29fb7c13cd6b1d85b9f0cc0f358c6e44a 100644 (file)
@@ -79,6 +79,16 @@ class Manifest {
   Manifest& operator=(const Manifest&) = delete;
 };
 
+struct ManifestConstraints {
+ public:
+  ManifestConstraints();
+  unsigned int max_attr_element_length() const;
+  void set_max_attr_element_length(unsigned int value);
+
+ private:
+  unsigned int max_attr_element_length_;
+};
+
 }  // namespace parser
 
 #endif  // MANIFEST_PARSER_MANIFEST_H_
index c60a09f9db4e0b5aa367590f41be569f5fbbc21e..d34d16a524efa41e7597bb5919c19de125e36989 100644 (file)
@@ -28,8 +28,9 @@ const std::string& ManifestParser::GetErrorMessage() const {
   return impl_->GetErrorMessage();
 }
 
-bool ManifestParser::ParseManifest(const boost::filesystem::path& path) {
-  return impl_->ParseManifest(path);
+bool ManifestParser::ParseManifest(const boost::filesystem::path& path,
+    std::shared_ptr<ManifestConstraints> constraints) {
+  return impl_->ParseManifest(path, constraints);
 }
 
 std::shared_ptr<const ManifestData> ManifestParser::GetManifestData(
index 38e1fc6204b7131eb681dfe1bf274fcc5c69d4a0..d0e2c7f894ffa57b707fcf7e735238691cb523ff 100644 (file)
@@ -27,7 +27,8 @@ class ManifestParser final {
   virtual const std::string& GetErrorMessage() const;
 
   // Parses manifest file to manfiest_ object
-  bool ParseManifest(const boost::filesystem::path& path);
+  bool ParseManifest(const boost::filesystem::path& path,
+      std::shared_ptr<ManifestConstraints> constraints = nullptr);
 
   std::shared_ptr<const ManifestData> GetManifestData(const std::string& key);
   std::shared_ptr<ManifestData> GetMutableManifestData(const std::string& key);
index abc7032e1f9767b4e07cb4fdeb9d8adf964830a0..5813c8d8918ad9f559e2e0532a45bd038552beb9 100644 (file)
@@ -41,7 +41,8 @@ const std::string& ManifestParserImpl::GetErrorMessage() const {
   return error_;
 }
 
-bool ManifestParserImpl::ParseManifest(const bf::path& manifest_path) {
+bool ManifestParserImpl::ParseManifest(const bf::path& manifest_path,
+    std::shared_ptr<ManifestConstraints> constraints) {
   error_.clear();
   if (manifest_path.empty()) {
     SetError(kErrMsgNoPath, &error_);
@@ -52,7 +53,8 @@ bool ManifestParserImpl::ParseManifest(const bf::path& manifest_path) {
     return false;
   }
 
-  manifest_ = parser::LoadManifest(manifest_path.string(), &error_);
+  manifest_ =
+      parser::LoadManifest(manifest_path.string(), &error_, constraints);
   if (!manifest_.get())
     return false;
   if (!ParseManifestData(&error_))
index ac14f076075c13dbb3f1015c2d36d727e0496371..acc726f7a2d053ca73bcf69e92e9d11e25b69e70 100644 (file)
@@ -29,10 +29,12 @@ class ManifestParserImpl {
   /**
    * @brief ParseManifest
    * @param path
+   * @param constraints
    * @return true on success
    */
 
-  bool ParseManifest(const boost::filesystem::path& path);
+  bool ParseManifest(const boost::filesystem::path& path,
+      std::shared_ptr<ManifestConstraints> constraints = nullptr);
   /**
    * @brief GetMutableManifestData gathers manifest data
    * @param key
index bbe0170e40e65148a54d5769d63ec0c48790966b..ab82e0591801e9b8f114f0412b8604c2361b986c 100644 (file)
@@ -21,6 +21,7 @@
 namespace bf = boost::filesystem;
 
 namespace {
+
 const char kAttributePrefix[] = "@";
 
 const xmlChar kWidgetNodeKey[] = "widget";
@@ -210,7 +211,9 @@ bool IsTrimRequiredForProp(xmlNode* root, xmlAttr* prop) {
 // std::map<std::string*, std::map<std::string*,std::string>>
 
 std::unique_ptr<DictionaryValue> LoadXMLNode(
-    xmlNode* root, const std::string& inherit_dir) {
+    xmlNode* root,
+    std::shared_ptr<ManifestConstraints> constraints,
+    const std::string& inherit_dir) {
   std::unique_ptr<DictionaryValue> value(new DictionaryValue());
   if (root->type != XML_ELEMENT_NODE)
     return nullptr;
@@ -220,19 +223,22 @@ std::unique_ptr<DictionaryValue> LoadXMLNode(
   xmlAttr* prop = nullptr;
   for (prop = root->properties; prop; prop = prop->next) {
     xmlChar* value_ptr = xmlNodeListGetString(root->doc, prop->children, 1);
-    std::string prop_value(reinterpret_cast<char*>(value_ptr));
+    std::string prop_value(reinterpret_cast<const char*>(value_ptr));
+    std::string prop_name(reinterpret_cast<const char*>(prop->name));
     xmlFree(value_ptr);
 
+    if (constraints) {
+      if (prop_value.size() > constraints->max_attr_element_length())
+        prop_value.resize(constraints->max_attr_element_length());
+    }
+
     if (IsPropSupportDir(root, prop))
       prop_value = utils::GetDirTextUTF8(prop_value, current_dir);
 
     if (IsTrimRequiredForProp(root, prop))
       prop_value = utils::CollapseWhitespaceUTF8(prop_value);
 
-    value->SetString(
-        std::string(kAttributePrefix)
-        + reinterpret_cast<const char*>(prop->name),
-        prop_value);
+    value->SetString(std::string(kAttributePrefix) + prop_name, prop_value);
   }
 
   if (root->ns)
@@ -242,7 +248,7 @@ std::unique_ptr<DictionaryValue> LoadXMLNode(
   for (xmlNode* node = root->children; node; node = node->next) {
     std::string sub_node_name(reinterpret_cast<const char*>(node->name));
     std::unique_ptr<DictionaryValue> sub_value =
-        LoadXMLNode(node, current_dir);
+        LoadXMLNode(node, constraints, current_dir);
     if (!sub_value)
       continue;
 
@@ -293,7 +299,7 @@ std::unique_ptr<DictionaryValue> LoadXMLNode(
 }
 
 std::shared_ptr<Manifest> LoadManifest(const std::string& manifest_path,
-                                       std::string* error) {
+    std::string* error, std::shared_ptr<ManifestConstraints> constraints) {
   xmlDoc * doc = nullptr;
   xmlNode* root_node = nullptr;
   doc = xmlReadFile(manifest_path.c_str(), nullptr, XML_PARSE_NOENT);
@@ -302,7 +308,7 @@ std::shared_ptr<Manifest> LoadManifest(const std::string& manifest_path,
     return nullptr;
   }
   root_node = xmlDocGetRootElement(doc);
-  std::unique_ptr<DictionaryValue> dv = LoadXMLNode(root_node);
+  std::unique_ptr<DictionaryValue> dv = LoadXMLNode(root_node, constraints);
   std::unique_ptr<DictionaryValue> result(new DictionaryValue);
   if (dv)
     result->Set(reinterpret_cast<const char*>(root_node->name), dv.release());
index 93cee4de999ca4f90f1d838b667fdfeb07cc59fe..54ed82cefb174a2a7ac1b1f39cbe0f29ed8275c5 100644 (file)
@@ -21,7 +21,8 @@ namespace parser {
 // Loads an application manifest from the specified directory. Returns nullptr
 // on failure, with a description of the error in |error|.
 std::shared_ptr<Manifest> LoadManifest(
-    const std::string& file_path, std::string* error);
+    const std::string& file_path, std::string* error,
+    std::shared_ptr<ManifestConstraints> constraints = nullptr);
 std::string GetNodeDir(xmlNode* node, const std::string& inherit_dir);
 std::string GetNodeText(xmlNode* root, const std::string& inherit_dir);
 bool IsPropSupportDir(xmlNode* root, xmlAttr* prop);
@@ -29,7 +30,8 @@ bool IsElementSupportSpanAndDir(xmlNode* root);
 bool IsTrimRequiredForElement(xmlNode* root);
 bool IsTrimRequiredForProp(xmlNode* root, xmlAttr* prop);
 std::unique_ptr<DictionaryValue> LoadXMLNode(
-    xmlNode* root, const std::string& inherit_dir = "");
+    xmlNode* root, std::shared_ptr<ManifestConstraints> constraints = nullptr,
+    const std::string& inherit_dir = "");
 
 bool ValidateTizenApplicationId(const std::string& id);
 bool ValidateTizenPackageId(const std::string& id);