Fix static analysis issues 33/283033/4
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 17 Oct 2022 04:25:14 +0000 (13:25 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Tue, 18 Oct 2022 01:11:14 +0000 (10:11 +0900)
Change-Id: Ica815a67211bee0d76bb35f64e15b1cb84124d2c
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
parcel/parcel.cc
parcel/stub.cc
src/bundle-internal.cc
src/bundle-internal.h

index 439e212..9322d49 100644 (file)
@@ -130,7 +130,7 @@ int Parcel::Impl::ReadSize(uint32_t* size) {
 
 template <typename T>
 int Parcel::Impl::Read(T* d) {
-  uint32_t size = sizeof(T);
+  uint32_t size = static_cast<uint32_t>(sizeof(T));
   if (reader_ + size > data_size_)
     return TIZEN_ERROR_ILLEGAL_BYTE_SEQ;
 
index f6b6299..245e3ac 100644 (file)
@@ -390,7 +390,7 @@ extern "C" EXPORT int parcel_get_raw(parcel_h parcel, void** raw,
 
   auto* h = static_cast<Parcel*>(parcel);
   *raw = reinterpret_cast<void*>(h->GetData());
-  *size = h->GetDataSize();
+  *size = static_cast<uint32_t>(h->GetDataSize() & UINT32_MAX);
   return PARCEL_ERROR_NONE;
 }
 
index 8088c60..44942ac 100644 (file)
@@ -19,7 +19,9 @@
 #include <errno.h>
 #include <glib.h>
 
+#include <algorithm>
 #include <cstring>
+#include <utility>
 
 #include "include/bundle.h"
 
@@ -31,11 +33,14 @@ namespace internal {
 static const int CHECKSUM_LENGTH = 32;
 
 Bundle::Bundle(unsigned char* raw, int size, bool base64) {
+  if (size < 0)
+    THROW(BUNDLE_ERROR_INVALID_PARAMETER);
+
   int ret;
   if (base64)
-    ret = Decode(raw, size);
+    ret = Decode(raw, static_cast<size_t>(size));
   else
-    ret = DecodeRaw(raw, size);
+    ret = DecodeRaw(raw, static_cast<size_t>(size));
   if (ret != BUNDLE_ERROR_NONE)
     THROW(ret);
 }
@@ -161,7 +166,7 @@ unsigned char* Bundle::Encode() {
   return reinterpret_cast<unsigned char*>(encoded_data);
 }
 
-int Bundle::Decode(unsigned char* raw, int size) {
+int Bundle::Decode(unsigned char* raw, size_t size) {
   unsigned char* d_str = new (std::nothrow) unsigned char[(size / 4) * 3 + 3];
   if (d_str == nullptr)
     return BUNDLE_ERROR_OUT_OF_MEMORY;
@@ -169,8 +174,8 @@ int Bundle::Decode(unsigned char* raw, int size) {
   std::unique_ptr<unsigned char[]> d_ptr(d_str);
   gint state = 0;
   guint save = 0;
-  unsigned int d_len_raw = g_base64_decode_step(reinterpret_cast<char*>(raw),
-      size, d_str, &state, &save);
+  size_t d_len_raw = static_cast<size_t>(g_base64_decode_step(
+      reinterpret_cast<char*>(raw), size, d_str, &state, &save));
   if (d_len_raw < CHECKSUM_LENGTH)
     return BUNDLE_ERROR_OUT_OF_MEMORY;
 
@@ -206,14 +211,14 @@ unsigned char* Bundle::EncodeRaw(int* size) {
   return raw;
 }
 
-int Bundle::DecodeRaw(unsigned char* raw, int size) {
+int Bundle::DecodeRaw(unsigned char* raw, size_t size) {
   char* extract_checksum = new (std::nothrow) char[CHECKSUM_LENGTH + 1];
   if (extract_checksum == nullptr)
     return BUNDLE_ERROR_OUT_OF_MEMORY;
 
   std::unique_ptr<char[]> extract_ptr(extract_checksum);
   unsigned char* d_str = raw;
-  unsigned int d_len_raw = size;
+  size_t d_len_raw = size;
   strncpy(extract_checksum, reinterpret_cast<char*>(d_str), CHECKSUM_LENGTH);
   extract_checksum[CHECKSUM_LENGTH] = '\0';
 
@@ -229,7 +234,7 @@ int Bundle::DecodeRaw(unsigned char* raw, int size) {
     return BUNDLE_ERROR_INVALID_PARAMETER;
 
   unsigned char* d_r = d_str + CHECKSUM_LENGTH;
-  unsigned int d_len = d_len_raw - CHECKSUM_LENGTH;
+  size_t d_len = d_len_raw - CHECKSUM_LENGTH;
 
   unsigned int reader = 0;
   std::vector<unsigned char> bytes(d_r, d_r + d_len);
index 19813dd..f8d0fb1 100644 (file)
@@ -71,12 +71,12 @@ class Bundle {
 
   unsigned char* Encode();
   unsigned char* EncodeRaw(int* size);
-  int DecodeRaw(unsigned char* raw, int size);
+  int DecodeRaw(unsigned char* raw, size_t size);
   const std::unordered_map<std::string, std::shared_ptr<KeyInfo>>& GetMap() const;
   std::vector<std::string> Export();
 
  private:
-  int Decode(unsigned char* raw, int size);
+  int Decode(unsigned char* raw, size_t size);
   int Import(int argc, char** argv);
 
  private: