[MessagingEmail] Make sure that resources are freed in case of error.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 26 Aug 2015 07:21:05 +0000 (09:21 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 26 Aug 2015 07:21:05 +0000 (09:21 +0200)
Prevent CID: 476172

[Verification] TCT pass rate (r35): 100% (308/308/0/0/0).

Change-Id: I7df6af7127de24b2eb712693f890c69889c956d9
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/messaging/messaging_util.cc

index 6cb875dfb58c8eeadea49d95e98de15360f48ed9..fc2cd94e65f9ebd5e21346e65bc98ce2b3c55c70 100755 (executable)
@@ -35,6 +35,7 @@
 #include "tizen/tizen.h"
 #include "common/logger.h"
 #include "common/platform_exception.h"
+#include "common/scope_exit.h"
 #include "common/assert.h"
 
 using common::ErrorCode;
@@ -317,9 +318,13 @@ std::string PerformConversion(const std::string& input, const gchar* from_charse
 
   if ((GIConv)-1 == cd) {
     LoggerE("Failed to open iconv.");
-    return "";
+    return input;
   }
 
+  SCOPE_EXIT {
+    g_iconv_close(cd);
+  };
+
   // copied from glib/gconvert.c, g_convert does not handle "//IGNORE" properly
   static const gsize kNulTerminatorLength = 4;
   const gchar* str = input.c_str();
@@ -337,11 +342,15 @@ std::string PerformConversion(const std::string& input, const gchar* from_charse
 
   outp = dest = static_cast<gchar*>(g_malloc(outbuf_size));
 
-  if (!outp) {
+  if (!dest) {
     LoggerE("Failed to allocate memory.");
     return input;
   }
 
+  SCOPE_EXIT {
+    g_free(dest);
+  };
+
   while (!done && !have_error) {
     gsize err = 0;
 
@@ -404,8 +413,6 @@ std::string PerformConversion(const std::string& input, const gchar* from_charse
     have_error = TRUE;
   }
 
-  g_iconv_close(cd);
-
   std::string result;
 
   if (!have_error) {
@@ -414,8 +421,6 @@ std::string PerformConversion(const std::string& input, const gchar* from_charse
     LoggerE("Conversion error");
   }
 
-  g_free(dest);
-
   return result;
 }