Fix static analyzer issues 95/174595/3
authorJunghoon Park <jh9216.park@samsung.com>
Tue, 3 Apr 2018 06:32:24 +0000 (15:32 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Tue, 3 Apr 2018 07:53:08 +0000 (16:53 +0900)
Change-Id: Ibbe6afa15c5c48ec374f01305f5ebe4b3664c7b3
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
src/rpc-port-parcel.cc
src/stub-internal.h
unit_tests/src/rpc_port_parcel_test.cc

index c5272a7..1a266d8 100755 (executable)
@@ -21,6 +21,8 @@
 #include "parcel-internal.h"
 #include "port-internal.h"
 
+#define MAX_PARCEL_SIZE   (1024 * 1024 * 10)
+
 #undef RPC_API
 #define RPC_API extern "C" __attribute__((visibility("default")))
 
@@ -46,6 +48,8 @@ RPC_API int rpc_port_parcel_create_from_port(rpc_port_parcel_h* h,
     if (ret != 0)
       return ret;
 
+    if (len <= 0 || len > MAX_PARCEL_SIZE)
+      return RPC_PORT_ERROR_IO_ERROR;
     buf = new unsigned char[len];
     ret = rpc_port_read(port, buf, len);
     if (ret != 0) {
index ebbce2a..ba66bfe 100644 (file)
@@ -75,7 +75,7 @@ class Stub : private FdBroker::IEventListener {
 
  private:
   std::list<std::shared_ptr<Port>> ports_;
-  IEventListener* listener_;
+  IEventListener* listener_ = nullptr;
   FdBroker fd_broker_;
   std::string port_name_;
 };
index 12384c0..fba0b95 100644 (file)
 using namespace std;
 using ::testing::AtLeast;
 
+namespace {
+class CStringHolder {
+ public:
+  CStringHolder(char* ptr) noexcept
+      : ptr_(ptr) {}
+  ~CStringHolder() {
+    if (ptr_)
+      free(ptr_);
+  }
+
+  const char* Get() const {
+    return ptr_;
+  }
+
+ private:
+  char* ptr_;
+};
+}
+
 class ParcelTest : public ::testing::Test {
  public:
   virtual void SetUp() {
@@ -58,9 +77,9 @@ class ParcelTest : public ::testing::Test {
   static void ReadParcelable(rpc_port_parcel_h h, void* data) {
     char* s = nullptr;
     int ret = rpc_port_parcel_read_string(h, &s);
+    ::CStringHolder str(s);
     ASSERT_EQ(ret, 0);
-    ASSERT_STREQ(s, "abcdef");
-    free(s);
+    ASSERT_STREQ(str.Get(), "abcdef");
 
     double b;
     ret = rpc_port_parcel_read_double(h, &b);
@@ -161,9 +180,9 @@ TEST_F(ParcelTest, read_write_string) {
 
   char* s = nullptr;
   ret = rpc_port_parcel_read_string(handle_, &s);
+  ::CStringHolder s1(s);
   ASSERT_EQ(ret, 0);
-  ASSERT_STREQ(s, str);
-  free(s);
+  ASSERT_STREQ(s1.Get(), str);
 }
 
 TEST_F(ParcelTest, read_write_bool) {