Fix exception handlings of getter 28/265228/2
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Oct 2021 04:36:58 +0000 (13:36 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Oct 2021 04:40:06 +0000 (13:40 +0900)
Even though the type is not equal to the request type, the method
returns the value with the error result. After this patch is applied,
the method checks whether the size of the raw data greater than the size or not.
If the type is boolean, the method checks the size of the raw data is
greater than the integer size or not.(This is for backward compatibility.)

Change-Id: I6288710d9ad79d22f8fa45fc50f77fc7245811a8
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
preference/preference-internal.cc

index 88040230a87e6c5c51823d90fa3144f907188dbf..b1b29c89eb965436179aefe48f5becbaaac53200 100644 (file)
@@ -63,6 +63,9 @@ int Preference::GetInt(const std::string& key) {
     _E("The type(%d) of keynode(%s) is not integer",
         static_cast<int>(type), key.c_str());
     set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+    auto& raw = d->GetRaw();
+    if (raw.size() == 0 || raw.size() > sizeof(int))
+      THROW(PREFERENCE_ERROR_INVALID_PARAMETER);
   }
 
   return d->GetInt();
@@ -80,6 +83,9 @@ double Preference::GetDouble(const std::string& key) {
     _E("The type(%d) of keynode(%s) is not double",
         static_cast<int>(type), key.c_str());
     set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+    auto& raw = d->GetRaw();
+    if (raw.size() == 0 || raw.size() > sizeof(double))
+      THROW(PREFERENCE_ERROR_INVALID_PARAMETER);
   }
 
   return d->GetDouble();
@@ -96,7 +102,7 @@ std::string Preference::GetString(const std::string& key) {
   } else {
     _E("The type(%d) of keynode(%s) is not string",
         static_cast<int>(type), key.c_str());
-    set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+    THROW(PREFERENCE_ERROR_INVALID_PARAMETER);
   }
 
   return d->GetString();
@@ -114,6 +120,9 @@ bool Preference::GetBoolean(const std::string& key) {
     _E("The type(%d) of keynode(%s) is not boolean",
         static_cast<int>(type), key.c_str());
     set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+    auto& raw = d->GetRaw();
+    if (raw.size() == 0 || raw.size() > sizeof(int))
+      THROW(PREFERENCE_ERROR_INVALID_PARAMETER);
   }
 
   return d->GetBoolean();