fix crash on registry access
authorYoung Ik Cho <youngik.cho@samsung.com>
Fri, 30 Aug 2013 04:28:14 +0000 (13:28 +0900)
committerYoung Ik Cho <youngik.cho@samsung.com>
Fri, 30 Aug 2013 04:29:44 +0000 (13:29 +0900)
Change-Id: I9882fb7d5ee4dfd4ca974b19375fcd54a423fe36
Signed-off-by: Young Ik Cho <youngik.cho@samsung.com>
src/app/FApp_AppRegistryImpl.cpp
src/app/FApp_AppRegistryImpl.h

index ab1623f..667a016 100644 (file)
@@ -22,9 +22,8 @@
 #include <FBaseSysLog.h>
 #include <FBaseRtMutexGuard.h>
 #include <FIoFile.h>
-#include <FIoFileAttributes.h>
 #include <FIoRegistry.h>
-#include "FAppAppRegistry.h"
+#include <FAppAppRegistry.h>
 
 #include "FApp_AppRegistryImpl.h"
 #include "FApp_AppInfo.h"
@@ -57,6 +56,19 @@ _AppRegistryImpl::Construct(void)
 
        __mutex.Create();
 
+       {
+               MutexGuard lock(__mutex);
+
+               Registry reg;
+               r = reg.Construct(__regPath, "a+");
+               SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
+
+               r = reg.AddSection(__sectionName);
+               if (r == E_SECTION_ALREADY_EXIST)
+               {
+                       r = E_SUCCESS;
+               }
+       }
        return r;
 }
 
@@ -65,7 +77,7 @@ _AppRegistryImpl::Add(const String& key, const String& value)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->AddValue(__sectionName, key, value);
@@ -81,7 +93,7 @@ _AppRegistryImpl::Add(const String& key, int value)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->AddValue(__sectionName, key, value);
@@ -97,7 +109,7 @@ _AppRegistryImpl::Add(const String& key, double value)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->AddValue(__sectionName, key, value);
@@ -113,7 +125,7 @@ _AppRegistryImpl::Set(const String& key, const String& value)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->SetValue(__sectionName, key, value);
@@ -129,7 +141,7 @@ _AppRegistryImpl::Set(const String& key, int value)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->SetValue(__sectionName, key, value);
@@ -145,7 +157,7 @@ _AppRegistryImpl::Set(const String& key, double value)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->SetValue(__sectionName, key, value);
@@ -167,7 +179,7 @@ _AppRegistryImpl::Remove(const String& key)
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_WRITE);
+       Registry* pReg = LoadN(ReadWrite);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->RemoveValue(__sectionName, key);
@@ -183,7 +195,7 @@ _AppRegistryImpl::Get(const String& key, String& value) const
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_ONLY);
+       Registry* pReg = LoadN(ReadOnly);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->GetValue(__sectionName, key, value);
@@ -199,7 +211,7 @@ _AppRegistryImpl::Get(const String& key, int& value) const
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_ONLY);
+       Registry* pReg = LoadN(ReadOnly);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->GetValue(__sectionName, key, value);
@@ -215,7 +227,7 @@ _AppRegistryImpl::Get(const String& key, double& value) const
 {
        MutexGuard lock(__mutex);
 
-       Registry* pReg = LoadN(READ_ONLY);
+       Registry* pReg = LoadN(ReadOnly);
        SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
        result r = pReg->GetValue(__sectionName, key, value);
@@ -227,32 +239,28 @@ _AppRegistryImpl::Get(const String& key, double& value) const
 }
 
 Registry*
-_AppRegistryImpl::LoadN(LoadType type) const
+_AppRegistryImpl::LoadN(ReadOnlyTag) const
 {
-       result r = E_SUCCESS;
-
        Registry* pReg = new (std::nothrow) Registry();
        SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
-       if (type == READ_ONLY)
-       {
-               r = pReg->Construct(__regPath, "r");
-       }
-       else
-       {
-               r = pReg->Construct(__regPath, "a+");
-       }
-
+       result r = pReg->Construct(__regPath, "r");
        SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
                        __regPath.GetPointer(), GetErrorMessage(r));
 
-       r = pReg->AddSection(__sectionName);
-       if (r == E_SECTION_ALREADY_EXIST)
-       {
-               r = E_SUCCESS;
-       }
+       SetLastResult(r);
+       return pReg;
+}
 
-       SysAssertf(!IsFailed(r), "Adding section to registry has failed. %s occurred.", GetErrorMessage(r));
+Registry*
+_AppRegistryImpl::LoadN(ReadWriteTag) const
+{
+       Registry* pReg = new (std::nothrow) Registry();
+       SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+
+       result r = pReg->Construct(__regPath, "a+");
+       SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
+                       __regPath.GetPointer(), GetErrorMessage(r));
 
        SetLastResult(r);
        return pReg;
index 4a1e35e..af6f5ff 100644 (file)
 
 #include <FBaseObject.h>
 #include <FAppTypes.h>
-
-namespace Tizen { namespace Base { namespace Runtime { class Mutex; } } }
-namespace Tizen { namespace Io { class Registry; } }
+#include <FBaseRtMutex.h>
 
 namespace Tizen { namespace App
 {
 
+struct ReadOnlyTag {};
+struct ReadWriteTag {};
+
+static const ReadOnlyTag ReadOnly = {};
+static const ReadWriteTag ReadWrite = {};
+
 /**
  * @class              _AppRegistryImpl
  * @brief              This class manages an application's preferences.
- * @since              1.0
  */
 class _AppRegistryImpl
-       : public Tizen::Base::Object
 {
 public:
        /**
         * This is the destructor for this class.
-        *
-        * @since               1.0
         */
-       virtual ~_AppRegistryImpl(void);
+       ~_AppRegistryImpl(void);
 
        /**
         * Adds a string value along with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   A string value
@@ -65,7 +64,6 @@ public:
        /**
         * Adds an integer value along with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   An integer value
@@ -80,7 +78,6 @@ public:
        /**
         * Adds a floating point value along with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   A floating point value
@@ -95,7 +92,6 @@ public:
        /**
         * Updates a string value associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   A string value
@@ -110,7 +106,6 @@ public:
        /**
         * Updates an integer value associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key     A key corresponding to the value
         * @param[in]   value   An integer value
@@ -125,7 +120,6 @@ public:
        /**
         * Updates a floating point value associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key     A key corresponding to the value
         * @param[in]   value   A floating point value
@@ -141,7 +135,6 @@ public:
         * Saves the values temporarily in the persistent storage. @n
         * This method is invoked internally when the instance of this class is deleted.
         *
-        * @since               1.0
         * @return      An error code
         * @exception   E_SUCCESS                       The method is successful.
         */
@@ -150,7 +143,6 @@ public:
        /**
         * Removes a preference associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             The key of the value to be removed
         * @exception   E_SUCCESS                               The method is successful.
@@ -161,7 +153,6 @@ public:
        /**
         * Retrieves a string value associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             The key of the value to retrieve
         * @param[out]  value   A string value to be retrieved
@@ -174,7 +165,6 @@ public:
        /**
         * Retrieves an integer value associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             The key of the value to retrieve
         * @param[out]  value   An integer value to be retrieved
@@ -187,7 +177,6 @@ public:
        /**
         * Retrieves a floating point value associated with the specified key.
         *
-        * @since               1.0
         * @return      An error code
         * @param[in]   key             The key of the value to retrieve
         * @param[out]  value   A floating point value to be retrieved
@@ -200,22 +189,17 @@ public:
 private:
        /**
         * This is the default constructor for this class.
-        *
-        * @since 1.0
         */
        _AppRegistryImpl(void);
 
        /**
         * This is the copy constructor for this class.
-        *
-        * @since       1.0
         */
        _AppRegistryImpl(const _AppRegistryImpl& source);
 
        /**
         * Allocates memory for an _AppRegistryImpl object.
         *
-        * @since       1.0
         * @return      An error code
         * @exception   E_SUCCESS       The method is successful.
         */
@@ -226,11 +210,14 @@ private:
         */
        _AppRegistryImpl& operator =(const _AppRegistryImpl& source);
 
-private:
-       enum LoadType {
-               READ_ONLY,
-               READ_WRITE
-       };
+       /**
+        * Loads the registry file.
+        *
+        * @since               2.2
+        * @return              The registry instance
+        * @exception   E_SUCCESS       The method is successful.
+        */
+       Tizen::Io::Registry* LoadN(ReadOnlyTag) const;
 
        /**
         * Loads the registry file.
@@ -239,7 +226,8 @@ private:
         * @return              The registry instance
         * @exception   E_SUCCESS       The method is successful.
         */
-       Tizen::Io::Registry* LoadN(LoadType type) const;
+       Tizen::Io::Registry* LoadN(ReadWriteTag) const;
+
 
 private:
        const Tizen::Base::String __sectionName;